Skip to content

Commit

Permalink
Minor cleanup in the game state code
Browse files Browse the repository at this point in the history
  • Loading branch information
mmatyas committed Mar 29, 2024
1 parent 5683738 commit c2e54de
Showing 1 changed file with 30 additions and 33 deletions.
63 changes: 30 additions & 33 deletions src/smw/GameState.h
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

0 comments on commit c2e54de

Please sign in to comment.