   Classes
  Hi guys!Im trying to develop a re-usable code base to create simple 2D games whilst i learn and im debating on how to organise my classes.I have a core class which sets up a new window and handles the game loop.I have another class which handles setting up a graphics device and drawing to the screen.My current setup, for example, is this:class Font{public:    Font()    {   m_font = NULL;    }    ~Font()    {   if(m_font != NULL)   {  m_font->Release();  m_font = NULL;   }    }    bool MakeFont(LPDIRECT3DDEVICE9 g_dev)    {   strcpy(fontdesc.FaceName, "Arial");   // etc...   D3DXCreateFontIndirect(g_dev, &fontdesc, &m_font);    }private:    ID3DXFont* m_font;    D3DXFONT_DESC fontdesc;};class Sprite{public:    Sprite()    {   m_sprite = NULL;    }    ~Sprite()    {   if(m_sprite != NULL)   {  m_sprite->Release();  m_sprite = NULL;   }    }    bool MakeSprite(LPDIRECT3DDEVICE9 g_dev)    {   D3DXCreateSprite(g_dev, &m_sprite);    }private:    ID3DXSprite* m_sprite;};class Renderer{public:    Renderer()    {   font = new Font();   sprite = new Sprite();    }    ~Renderer()    {   delete sprite;   delete font;    }    bool Caps();    bool SetupGraphicsDevice();    bool Init()    {   if(!Caps())  return false; if(!SetupGraphicsDevice())  return false;   if(!font->MakeFont(device))  return false;   if(!sprite->MakeSprite(device))  return false;   return true;    }private:    IDirect3D9* d3d;    IDirect3DDevice9* device;   Font* font;    Sprite* sprite;};class Core{public:    static Core& GetInstance()    {   static Core core;   return core;    }    void Init()    {   CreateParentWindow();   graphics = new Renderer();    }    void Shutdown()    {   delete graphics;   UnregisterClass(..., ...);    hwnd = NULL;    }    void CreateParentWindow(std::string caption);    int Run()    {   // Message Loop...   Shutdown();    }private:    HWND hwnd;    HINSTANCE hIns;    WNDCLASSEX wc;    MSG msg;    Renderer* graphics;};int WINAPI WinMain(..., ..., ..., ...){    Core::GetInstance().Init();    return Core::GetInstance().Run();}Just to be very brief. Im just wondering if this is the optimal way of doing things with classes? My classes contain a lot more members and methods this is just an example.Since i have more subsystems like input and sound, i was thinking of having them all follow the same pattern and inherit them all from a base class including virtual functions like Init(), Shutdown() etc... and adding them to a list. That way i can iterate through them all and initialize and shutdown each subsystem in a loop.If the ideas i have are complete rubbish then tell me i dont mind the criticism. I know it might not matter with it being for games like 2D shooters but im sure there is a better way.Lastly, if this is in the wrong section i apologise. With it being a problem related to c++ classes and not DirectX/OpenGL then i assumed this as the perfect home for this thread. :-)Thanks all!   
  Having them all inherit from an init/startup/shurdown kind of system class is a fine idea.Quote:Original post by Pedigree_DIm trying to develop a re-usable code base to create simple 2D games whilst i learnIf this is your first 2D game, don't worry about this.  Just create the game in whatever way makes sense (fine to ask for pointers obviously).  It's just that when you're done with this game you'll have learned so much that you're going to want to throw away 98% of your code because you will now realize how terrible it is. [smile]Stressing now about re-usability will slow you down tremendously and you'll never use it anyway.-me   ;  Thanks for your reply.It's not my first game i've already created a pong and asteroids clone but strictly to get it done and thats it without worrying about good code design or anything. Im just tired of re-typing the Windows and DirectX API stuff. If i can put together an "engine" then i can focus on learning further and develop new features, then at the end, ill have somthing to create a small portfolio of games developed using this "engine". Then i can move on to 3D without having to re-type everything over again.FYI, by "engine" i mean a library of code i can re-use and add to. Not something id software would create LOL.Any other ideas about how it could be achieved?   ;  You really just have to have experience with attempting to re-use code. Keep stuff simple at first, but don't be afraid to change it. The main thing to remember is to keep different parts of the program from connecting, so they're easy to change. Here is some interesting reading:Single Responsibility Principle (SRP)Liskov Substitution Principle (LSP)Dependency Inversion Principle (DIP)Open-Closed Principle (OCP)Interface Segregation Principle (ISP)Here is a page where all those can be found, and a few more: Clicky.   ;  Thank you so much! Ill read these when i get home.   