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:
- Go ahead and create a new file in the project called
Game.h
:#ifndef __Game__ #define __Game__ class Game { }; #endif /* defined(__Game__) */
- Next, we can move our functions from the
main.cpp
file into theGame.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; };
- Now, we can alter the
main.cpp
file to use this newGame
class:#include "Game.h" ...
Get SDL Game Development now with the O’Reilly learning platform.
O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.