O'Reilly logo

SDL Game Development by Shaun Mitchell

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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" ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required