The Game class

So, now that we have an idea of what makes up a game, we can separate the functions into their own class by following these steps:

  1. Go ahead and create a new file in the project called Game.h:
    #ifndef __Game__
    #define __Game__
    
    class Game
    {
    };
    
    #endif /* defined(__Game__) */
  2. Next, we can move our functions from the main.cpp file into the Game.h header file:
    class Game
    {
    public:
    
      Game() {}
      ~Game() {}
    
      // simply set the running variable to true
      void init() { m_bRunning = true; }    
    
      void render(){}
      void update(){}
      void handleEvents(){}
      void clean(){}
    
      // a function to access the private running variable 
      bool running() { return m_bRunning; }
    
    private:
    
      bool m_bRunning;
    };
  3. Now, we can alter the main.cpp file to use this new Game class:
    #include "Game.h" ...

Get SDL Game Development now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.