Last active
May 11, 2018 12:06
-
-
Save Rydgel/fcfe989d5083bea724a75635584b0cc9 to your computer and use it in GitHub Desktop.
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
// 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