Skip to content

Commit

Permalink
First state on StateManager is not hardcoded anymore
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdantas committed Aug 28, 2014
1 parent 143b61a commit a25c061
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 45 deletions.
111 changes: 72 additions & 39 deletions deps/Engine/Flow/StateManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,29 @@
#include <States/GameStateGame.hpp>
#include <States/GameStateMainMenu.hpp>

StateManager::StateManager():
currentState(NULL),
void StateManager::change(GameState* newState)
{
// Yeah, right!
//
// My solution to immediately change from one
// state to another is to launch an exception!
//
// See in action on `StateManager::run()`
//
// It seems kinda hackish, but at least it makes
// StateManager independent of all user-defined
// GameStates...
throw StateManagerChangeException(newState);
}


StateManager::StateManager(GameState* initialState):
currentState(initialState),
sharedInfo(0)
{
// The first state, Hardcoded
this->currentState = new GameStateMainMenu();
if (! this->currentState)
throw "No state given to StateManager";

this->currentState->load();
}
StateManager::~StateManager()
Expand All @@ -24,59 +41,75 @@ void StateManager::run()
{
bool letsQuit = false;

while (!letsQuit)
while (! letsQuit)
{
InputManager::update();
try
{
InputManager::update();

// Updating the whole state.
// This value is returned from it tell us if
// we need to switch from the current state.
GameState::StateCode whatToDoNow;
// Updating the whole state.
// This value is returned from it tell us if
// we need to switch from the current state.
GameState::StateCode whatToDoNow;

whatToDoNow = this->currentState->update();
whatToDoNow = this->currentState->update();

switch (whatToDoNow)
{
case GameState::CONTINUE:
// Just continue on the current state.
break;
switch (whatToDoNow)
{
case GameState::CONTINUE:
// Just continue on the current state.
break;

case GameState::QUIT:
this->currentState->unload();
delete this->currentState;
this->currentState = NULL;
case GameState::QUIT:
this->currentState->unload();
SAFE_DELETE(this->currentState);
this->currentState = NULL;

letsQuit = true;
break;
letsQuit = true;
break;

case GameState::GAME_START:
{
this->currentState->unload();
delete this->currentState;
case GameState::MAIN_MENU:
{
this->currentState->unload();
SAFE_DELETE(this->currentState);

this->currentState = new GameStateGame();
this->currentState->load();
break;
this->currentState = new GameStateMainMenu();
this->currentState->load();
break;
}

default:
break;
}

if (this->currentState)
this->currentState->draw();

Utils::Time::delay_ms(100);
}

case GameState::MAIN_MENU:
// Special type of exception used to
// instantaneously change from one state
// to another.
catch (StateManagerChangeException& e)
{
this->currentState->unload();
delete this->currentState;
SAFE_DELETE(this->currentState);

this->currentState = new GameStateMainMenu();
this->currentState = e.newState;
this->currentState->load();
break;
}

default:
break;
// Continue with the loop
}
// catch (StateManagerQuitException& e)
// {
// this->currentState->unload();
// SAFE_DELETE(this->currentState);

if (this->currentState)
this->currentState->draw();
// break;
// }

Utils::Time::delay_ms(100);
// All other exceptions will keep going up
}
}

24 changes: 22 additions & 2 deletions deps/Engine/Flow/StateManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@
#define STATEMANAGER_H_DEFINED

#include <Engine/Flow/GameState.hpp>
#include <exception>

/// Custom exception that's used to instantly
/// change from one state to another.
///
class StateManagerChangeException : public std::exception
{
public:
StateManagerChangeException(GameState* newState):
newState(newState)
{ }

GameState* newState;
};

/// Giga-class that switches from game states.
///
Expand All @@ -22,8 +36,14 @@
class StateManager
{
public:
/// Initializes pretty much everything.
StateManager();
/// Immediately changes to #newState
static void change(GameState* newState);

/// Initializes pretty much everything,
/// setting #initialState to run first.
///
/// @note It only actually starts when you call #run()
StateManager(GameState* initialState);

virtual ~StateManager();

Expand Down
9 changes: 6 additions & 3 deletions src/States/GameStateMainMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <Entities/BoardParser.hpp>
#include <Entities/ScoreFile.hpp>

#include <Engine/Flow/StateManager.hpp>
#include <States/GameStateGame.hpp>

enum NamesToEasilyIdentifyTheMenuItemsInsteadOfRawNumbers
{
// Main Menu
Expand Down Expand Up @@ -122,14 +125,14 @@ GameState::StateCode GameStateMainMenu::update()
this->menuLevels->goRandom();

Globals::Game::current_level = this->menuLevels->current->label;
return GameState::GAME_START;
StateManager::change(new GameStateGame());
break;
}

default:
// Selected a level name!
Globals::Game::current_level = this->menuLevels->current->label;
return GameState::GAME_START;
StateManager::change(new GameStateGame());
break;
}
this->menuLevels->reset();
Expand Down Expand Up @@ -261,7 +264,7 @@ GameState::StateCode GameStateMainMenu::update()
case ARCADE:
// Starting game on the default level
Globals::Game::current_level = "";
return GameState::GAME_START;
StateManager::change(new GameStateGame());
break;

case LEVELS:
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <Engine/EngineGlobals.hpp>
#include <Config/Globals.hpp>
#include <Config/Arguments.hpp>
#include <States/GameStateMainMenu.hpp>

int main(int argc, char *argv[])
{
Expand All @@ -21,7 +22,7 @@ int main(int argc, char *argv[])
Colors::init();

// Actually running the game
StateManager states;
StateManager states(new GameStateMainMenu());
states.run();

// Finishing things
Expand Down

0 comments on commit a25c061

Please sign in to comment.