Structure of my first C++ game

Started by
12 comments, last by mmakrzem 12 years, 9 months ago
Hi everyone!

I've decided to create my first very, very basic game in C++ with DirectX. It's name will be Avoid Ball, were you are a green ball who's supposed to avoid other enemy balls that behave different, like bouncing and following etc. I've done this game in C# and XNA before, but as I've found C++ much more complicated, I'm not sure of how I should put up the structure of my game.

The below image is my thoughts so far. As you can see in the image, Main.cpp contains the entry point and the windows message handler. Main.cpp also initializes the GraphicsEnigne, InputEngine and GameObjects classes.

My thought was that GraphicsEngine, InputEngine and GameObejcts should be static classes, so I can reach all methods and variables from them without having to create a object/instance of them. Then it will also be possible to call the GraphicEngines Draw method from other sub-classes.

What do you think about this?
Is this a good structure?
Would you have made the 3 earlier mentioned classes static?
How would you have done?


2lvk7wz.jpg
Advertisement

Hi everyone!

I've decided to create my first very, very basic game in C++ with DirectX. It's name will be Avoid Ball, were you are a green ball who's supposed to avoid other enemy balls that behave different, like bouncing and following etc. I've done this game in C# and XNA before, but as I've found C++ much more complicated, I'm not sure of how I should put up the structure of my game.

The below image is my thoughts so far. As you can see in the image, Main.cpp contains the entry point and the windows message handler. Main.cpp also initializes the GraphicsEnigne, InputEngine and GameObjects classes.

My thought was that GraphicsEngine, InputEngine and GameObejcts should be static classes, so I can reach all methods and variables from them without having to create a object/instance of them. Then it will also be possible to call the GraphicEngines Draw method from other sub-classes.

What do you think about this?
Is this a good structure?
Would you have made the 3 earlier mentioned classes static?
How would you have done?


2lvk7wz.jpg


Perhaps you should have a static Game class that holds your stuff (I think actually you have called this Main), also Managers are a good paradgim for games. I use the following class as a base:

class IManager
{
public:
virtual void Update() = 0; //New managers use this to update themselves
virtual void Draw() = 0; //Use this to draw
virtual void Load() = 0; //Load all resources this manager needs.
};

And would make my GraphicsEngine and InputEngine inherit from it. I wouldn't make them static but would have them as a static member of a 'Game' class. Also, you should try and make a resource manager for loading your assets. This is what mine looks like and I use it to get my resource to the managers:

[font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]class[/font][/font][/font][font="Consolas"][font="Consolas"] ResourceManager

{

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]private[/font][/font][/font][font="Consolas"][font="Consolas"]:

[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//A dictionary of all the resources

[/font][/font][/font][font="Consolas"][font="Consolas"]std::map<std::string, IResource*> resource;

[/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]public[/font][/font][/font][font="Consolas"][font="Consolas"]:

ResourceManager();

~ResourceManager(); [/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//Should release(DELETE) all resources

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]/* Attempts to load the resource from the file

TYPE = an enum from IResource.h */

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]void[/font][/font][/font][font="Consolas"][font="Consolas"] LoadResourceFromFile(std::string name, std::string file, [/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] TYPE);

[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]/* Returns a pointer to the requested resource. If it doesn't exist

then a NULL pointer is returned.*/

[/font][/font][/font][font="Consolas"][font="Consolas"]IResource *GetResource(std::string name);

[/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//Utilities:

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]int[/font][/font][/font][font="Consolas"][font="Consolas"] GetMemmoryFootprint(); [/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//Return the amount of memeory used in bytes

[/font][/font][/font][font="Consolas"][font="Consolas"][/font][/font][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"][font="Consolas"][color="#0000ff"]bool[/font][/font][/font][font="Consolas"][font="Consolas"] HasResource(std::string name); [/font][/font][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"][font="Consolas"][color="#008000"]//Return true if that resource exists

[/font][/font][/font][font="Consolas"][font="Consolas"]};[/font][/font]









Hi everyone!

I've decided to create my first very, very basic game in C++ with DirectX. It's name will be Avoid Ball, were you are a green ball who's supposed to avoid other enemy balls that behave different, like bouncing and following etc. I've done this game in C# and XNA before, but as I've found C++ much more complicated, I'm not sure of how I should put up the structure of my game.

The below image is my thoughts so far. As you can see in the image, Main.cpp contains the entry point and the windows message handler. Main.cpp also initializes the GraphicsEnigne, InputEngine and GameObjects classes.

My thought was that GraphicsEngine, InputEngine and GameObejcts should be static classes, so I can reach all methods and variables from them without having to create a object/instance of them. Then it will also be possible to call the GraphicEngines Draw method from other sub-classes.

What do you think about this?
Is this a good structure?
Would you have made the 3 earlier mentioned classes static?
How would you have done?


2lvk7wz.jpg


Perhaps you should have a static Game class that holds your stuff (I think actually you have called this Main), also Managers are a good paradgim for games. I use the following class as a base:

class IManager
{
public:
virtual void Update() = 0; //New managers use this to update themselves
virtual void Draw() = 0; //Use this to draw
virtual void Load() = 0; //Load all resources this manager needs.
};

And would make my GraphicsEngine and InputEngine inherit from it. I wouldn't make them static but would have them as a static member of a 'Game' class. Also, you should try and make a resource manager for loading your assets. This is what mine looks like and I use it to get my resource to the managers:

class ResourceManager
{
private:
//A dictionary of all the resources
std::map<std::string, IResource*> resource;

public:
ResourceManager();
~ResourceManager(); //Should release(DELETE) all resources

/* Attempts to load the resource from the file
TYPE = an enum from IResource.h */
void LoadResourceFromFile(std::string name, std::string file, int TYPE);

/* Returns a pointer to the requested resource. If it doesn't exist
then a NULL pointer is returned.*/
IResource *GetResource(std::string name);

//Utilities:
int GetMemmoryFootprint(); //Return the amount of memeory used in bytes
bool HasResource(std::string name); //Return true if that resource exists
};










Thanks very much for your answer, anttoo. I'll keep that structure in mind smile.gif



Anyone else that have any ideas?
Its fun how everyone forgets about this when thinking about game engines... AUDIO!

I would also recommend you to think about collision detection or physics engine and space partitioning structures

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


Its fun how everyone forgets about this when thinking about game engines... AUDIO!

I would also recommend you to think about collision detection or physics engine and space partitioning structures




Hehe, no, I havn't forgot, it's just my first game wink.gif Collision detection would maybe fit in the GameObjects class? What do you think about making the engines static?


----------------------

Anyone else that have any idea?
No, collision detection inside the game objects gets messy and inefficient really fast, you should create abstract bounding bodies and a collision checking system that does not check everything against everything.

I have a tendency to make my engines, or as I prefer to call them Systems (I think the term Engine is meant for something of a wider spectrum than a single system or feature) static, but I've been told many times its a bad idea.

Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272



[color=#1C2837][size=2]but I've been told many times its a bad idea.
[/quote]

Why? Would you like to explain? smile.gif


------------------------------

Anyone else that got any ideas?
The thing is, it hasn't yet been properly explained to me, all arguments are either relative or purely phylosofical
Game making is godlike

LinkedIn profile: http://ar.linkedin.com/pub/andres-ricardo-chamarra/2a/28a/272


Aha ok :) Well, I guess it doesn't matter that much as the game isn't going to be either big or advanced ;)


-------------------


Please, doesn't anyone more have their opinion to share? That would be very kind unsure.gif

This topic is closed to new replies.

Advertisement