Sign in to follow this  

Structure of my first C++ game

This topic is 2342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 [i]static[/i]?
How would you have done?


[IMG]http://i55.tinypic.com/2lvk7wz.jpg[/IMG]

Share this post


Link to post
Share on other sites
[quote name='falcon93' timestamp='1311071926' post='4837307']
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 [i]static[/i]?
How would you have done?


[img]http://i55.tinypic.com/2lvk7wz.jpg[/img]
[/quote]

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:
[CODE]
[font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"][font="Consolas"][size="2"][color="#0000ff"]class[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"] ResourceManager

{

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

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

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

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

ResourceManager();

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

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

TYPE = an enum from IResource.h */

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

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

then a NULL pointer is returned.*/

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

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

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

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

[/color][/size][/font][/color][/size][/font][/color][/size][/font][font="Consolas"][size="2"][font="Consolas"][size="2"]};[/size][/font][/size][/font]
[/CODE]







Share this post


Link to post
Share on other sites
[quote name='falcon93' timestamp='1311071926' post='4837307']
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 [i]static[/i]?
How would you have done?


[img]http://i55.tinypic.com/2lvk7wz.jpg[/img]
[/quote]

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:
[CODE]
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
};[/CODE]









Share this post


Link to post
Share on other sites
Thanks very much for your answer, anttoo. I'll keep that structure in mind [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]



Anyone else that have any ideas?

Share this post


Link to post
Share on other sites
[quote name='NEXUSKill' timestamp='1311097579' post='4837515']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


[/quote]

Hehe, no, I havn't forgot, it's just my first game [img]http://public.gamedev.net/public/style_emoticons/default/wink.gif[/img] Collision detection would maybe fit in the GameObjects class? What do you think about making the engines static?


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

Anyone else that have any idea?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote]
[color=#1C2837][size=2]but I've been told many times its a bad idea.[/size][/color]
[/quote]

Why? Would you like to explain? [img]http://public.gamedev.net/public/style_emoticons/default/smile.gif[/img]


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

Anyone else that got any ideas?

Share this post


Link to post
Share on other sites
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 [img]http://public.gamedev.net/public/style_emoticons/default/unsure.gif[/img]

Share this post


Link to post
Share on other sites
Well your game might not be really big or advanced, but you definitely want to make your engine as reusable as possible. That way when you go to make another game you can pick parts of your old engine that would work perfectly. In that sense using singletons and deep hierarchies are BAD as they make code difficult to port due to dependencies. [url="http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/"]Here[/url] is an excellent article that describes some of the benefits of using more [u][b]aggregation[/b][/u] and less inheritance in your GameObjects/Entities.

Share this post


Link to post
Share on other sites
I find the entity component architecture to be right in concept but a little too extremist, I don't think its wrong to create a few specialization classes from entity if they are relevant to the game and potentially reusable, and then add components to that.

Components are great to create slight variations of a specialized class, or for instance add special behavior to particular entity types, for instance, you have RPG characters, you could implement the differences between a warrior and a mage with components, but I do believe the RPGChar class is still relevant and not that deep a hierarchy.

Share this post


Link to post
Share on other sites
I don't use directx (I use SDL/OpenGL) but I don't see how it could matter here. I've never used a GraphicsEngine class before. I've just used a draw() function that would call subsequent draw() functions on other things such as GameObjects and terrain or tiles. Similarly, why the InputEngine? A way that I like to handle input is by forwarding input events to my Player class to alter the players behavior. Why did you create a GameObjects container object? Why couldn't you just abstract it away and use a vector of some sort. You also shouldn't worry about code re-usability for such a small game, especially if you're trying to learn C++.

Share this post


Link to post
Share on other sites
in the game engine that i created i have a scenegraph that is used to manage all items in a level. within the scenegraph there are nodes that let you specify geometry, textures, materials, etc.

when you are ready to render, just render the root node and all the others get handled automatically.

have a look at my videos

Share this post


Link to post
Share on other sites

This topic is 2342 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this