Thread: organizing classes in a game

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    organizing classes in a game

    Hi,

    I have written several games in C++, but have always had difficulty to figure out how the classes should be structured. Now I'm writing a horizontal scroller game. The main character is a mage and several different spells. These spells are actually particle systems. To make the game more realistic, I have written a simple collision engine, so that my particles now collide with walls.

    I chose to have a following class structure:

    Code:
    class GLWorld			// contains: main loop and such
    							// a texture pool for loading textures (class GLTexturePool)
    							// one of the members is class Level
    					
    class Level				// is mainly responsible for loading level description files
    							// needs to have a link to the texture pool in GLWorld
    								therefore the constructor takes a pointer of type GLTexturePool*
    							// each level consists of several stages (class Stage)
    								therefor contains vector<Stage*> Stages
    						
    class Stage				// responsible for loading a stage from a stage description file
    							// also loads different objects
    							// needs to have a link to the texture pool in GLWorld
    								therefore the constructor takes a pointer of type GLTexturePool*
    							// the scene is split up in several layers:
    								- Background
    								- Background objects
    								- Solid objects
    								- Front objects
    						
    								They are rendered in order which is indicated above.
    								Therefore Stage contains vector<GLObject*> Bkg, BkgObj, SolObj, FrObj;
    							// Stage holds the collision engine calculated for FrObj
    							// objects can be added from outside with the void registerObject(GLObject*)
    						
    class GLObject			// responsible for drawing the object
    							// texture ID is set by class Stage
    							// holds such members as Position, Size etc
    							// inherits class GameObject
    					
    class GameObject		// holds only a few things
    								- Name
    								- spesifies if this is an Enemy, Friend or Neutral object
    								
    
    class GLParticleSystem// is the class which represents particle systems
    							// has a vector<GLParticle> Particles
    							// needs to have a link to the texture pool in GLWorld
    								therefore the constructor takes a pointer of type GLTexturePool*
    							// registers each particle as an object in current Stage
    								therefore needs a pointer Stage* CurrentStage to be set
    Now I am planning to implement a new class, Unit. Unit will inherit GLObject. Each unit will be loaded from a description file from within Unit class. Unit will also contain vector<GLParticleSystem*> Spells. It needs to have a pointer to TexturePool in GLWorld, since it will load frames for the animation of the unit.

    It is class Stage which will hold a vector<Unit*> Units.

    Now my first problem is that Unit needs to have a pointer to the current Stage to be able to initialise GLParticleSystem class for the spells. That means that unit.h will include stage.h, but at the same time stage.h must include unit.h. That leads to compiler errors. How do I solve this problem?

    My other question is as following: Is the structure of classes which I chose for my game good, and if not, what can you suggest I do with it?

    Thanks a lot.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    That means that unit.h will include stage.h, but at the same time stage.h must include unit.h. That leads to compiler errors. How do I solve this problem?
    Mutual inclusion can be a bit tricky and often lead to errors, but a general approach that I've been using is this:

    File1.h:
    Code:
    #include "File2.h"
    
    class Class1
    {
      ...
    };
    File2.h:
    Code:
    class Class1;
    
    class Class2
    {
      ...
    };
    
    #include "File1.h"
    Of course you cannot do anything in File2.h that requires knowledge of the size of Class1, like a member object in Class2 (the other way around works of course). If you need the classes to contain each others, use pointers and dynamic allocation.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Well,ok, thanks a lot for the advise!

    But I am also wondering if the way I have structured (that is how I have split the game into classes and the interaction between those classes) is optimal . I know that programming style is kind of a personal thing, but somehow I have doubts that my approach is good enough. Any suggestions or corrections are welcome.

    Anyway, thanks
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do the game engine and the api interact?
    By Shadow12345 in forum Game Programming
    Replies: 9
    Last Post: 12-08-2010, 12:08 AM
  2. C# - Building a SCUMM-like game.. questions
    By Iyouboushi in forum Game Programming
    Replies: 0
    Last Post: 05-24-2008, 10:54 PM
  3. Replies: 12
    Last Post: 05-23-2006, 11:49 PM
  4. Engine <=> DX/OGL | c++ ?
    By darkcloud in forum Game Programming
    Replies: 6
    Last Post: 05-13-2005, 12:19 AM
  5. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM