diff --git a/game.cpp b/game.cpp index 11924de..4f32d4d 100644 --- a/game.cpp +++ b/game.cpp @@ -31,6 +31,7 @@ bool Game::init() return false; } + init_done = true; return true; } diff --git a/gamestate.cpp b/gamestate.cpp index b96e3ea..7b430f1 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -1,5 +1,13 @@ #include "gamestate.h" +GameState::GameState(stack<GameState*>* state_stack, SDL_Surface* display) +{ + this->state_stack = state_stack; + this->display = display; + init_done = false; +} + + void GameState::execute() throw(StateExit) { SDL_Event event; diff --git a/gamestate.h b/gamestate.h index e974d5f..00bed1c 100644 --- a/gamestate.h +++ b/gamestate.h @@ -19,12 +19,12 @@ class StateExit : public exception {}; class GameState : public MainEvent { public: - GameState(stack<GameState*>* state_stack, SDL_Surface* display) - { this->state_stack = state_stack, this->display = display; } + GameState(stack<GameState*>* state_stack, SDL_Surface* display); virtual ~GameState() {} virtual bool init() = 0; void execute() throw(StateExit); + bool initted() { return init_done;} protected: virtual void iterate() = 0; @@ -34,6 +34,7 @@ class GameState : public MainEvent stack<GameState*>* state_stack; SDL_Surface* display; + bool init_done; }; diff --git a/main.cpp b/main.cpp index 6b5ce3b..5595e8b 100644 --- a/main.cpp +++ b/main.cpp @@ -31,20 +31,21 @@ int main(int argc, char** argv) while (!state_stack.empty()) { GameState* state = state_stack.top(); + + // init the new state, discarding it if we fail + if (!(state->initted() || state->init())) + { + state_stack.pop(); + delete state; + continue; + } + try { state->execute(); } catch (StateExit& e) { - // remove the old state state_stack.pop(); delete state; - - // init the new state, discarding it if we fail - while (!(state_stack.empty() || state_stack.top()->init())) - { - state_stack.pop(); - delete state; - } } } diff --git a/titlescreen.cpp b/titlescreen.cpp index 459e59b..84499f0 100644 --- a/titlescreen.cpp +++ b/titlescreen.cpp @@ -42,6 +42,7 @@ bool TitleScreen::init() DrawUtils::transpare(title_banner); + init_done = true; return true; }