-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor cleanup in the game state code
- Loading branch information
Showing
1 changed file
with
30 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,40 @@ | ||
#ifndef GAMESTATE_H | ||
#define GAMESTATE_H | ||
#pragma once | ||
|
||
class GameState | ||
{ | ||
public: | ||
virtual bool init() { return true; } | ||
virtual void update() = 0; | ||
virtual void cleanup() {} | ||
#include <cassert> | ||
|
||
protected: | ||
virtual void onEnterState() {} | ||
virtual void onLeaveState() {} | ||
|
||
friend class GameStateManager; | ||
}; | ||
class GameState { | ||
public: | ||
virtual bool init() { return true; } | ||
virtual void update() = 0; | ||
virtual void cleanup() {} | ||
|
||
protected: | ||
virtual void onEnterState() {} | ||
virtual void onLeaveState() {} | ||
|
||
class GameStateManager | ||
{ | ||
public: | ||
GameState* currentState; | ||
friend class GameStateManager; | ||
}; | ||
|
||
void changeStateTo(GameState* newState) { | ||
//assert(newState != 0); | ||
currentState->onLeaveState(); | ||
currentState = newState; | ||
currentState->onEnterState(); | ||
} | ||
|
||
static GameStateManager& instance() { | ||
static GameStateManager gsm; | ||
return gsm; | ||
} | ||
class GameStateManager { | ||
public: | ||
GameState* currentState = nullptr; | ||
|
||
private: | ||
void changeStateTo(GameState* newState) { | ||
assert(newState); | ||
currentState->onLeaveState(); | ||
currentState = newState; | ||
currentState->onEnterState(); | ||
} | ||
|
||
GameStateManager() {} | ||
GameStateManager(GameStateManager const&); | ||
void operator=(GameStateManager const&); | ||
static GameStateManager& instance() { | ||
static GameStateManager gsm; | ||
return gsm; | ||
} | ||
|
||
private: | ||
GameStateManager() {} | ||
GameStateManager(GameStateManager const&); | ||
void operator=(GameStateManager const&); | ||
}; | ||
|
||
#endif // GAMESTATE_H |