Implementing finite state machines
What we really need is the ability to define our states outside the game
class, and have the state itself take care of what it needs to load, render, and update. For this we can create what is known as an FSM. The definition of FSM, as we will use it, is a machine that can exist in a finite number of states, can exist in only one state at a time (known as the current state), and can change from one state to another (known as a transition).
A base class for game states
Let's start our implementation by creating a base class for all of our states; create a header file called
GameState.h
:
#include<string> class GameState { public: virtual void update() = 0; virtual void render() = 0; virtual bool onEnter() = 0; virtual ...
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.