Skip to content

Instantly share code, notes, and snippets.

@Rydgel
Last active May 11, 2018 12:06
Show Gist options
  • Save Rydgel/fcfe989d5083bea724a75635584b0cc9 to your computer and use it in GitHub Desktop.
Save Rydgel/fcfe989d5083bea724a75635584b0cc9 to your computer and use it in GitHub Desktop.
// in main
Game game;
game.pushState(std::make_unique<PlayState>(game));
game.gameLoop();
// Game class
using GameStatePtr = std::unique_ptr<IGameState>;
using GameStateStack = std::stack<GameStatePtr>;
using MaybeGameStatePtr = std::optional<IGameState*>;
void Game::pushState(GameStatePtr state)
{
m_states.push(std::move(state));
}
void Game::popState()
{
m_states.pop();
}
void Game::changeState(GameStatePtr state)
{
if (!m_states.empty())
popState();
pushState(std::move(state));
}
MaybeGameStatePtr Game::peekState()
{
if (m_states.empty())
return std::nullopt;
return m_states.top().get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment