Reworked initting code so new states get initted properly in main loop

This commit is contained in:
Anna Rose 2011-07-01 11:16:41 -04:00
parent 303f1d4511
commit b50ed649f5
5 changed files with 22 additions and 10 deletions

View File

@ -31,6 +31,7 @@ bool Game::init()
return false; return false;
} }
init_done = true;
return true; return true;
} }

View File

@ -1,5 +1,13 @@
#include "gamestate.h" #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) void GameState::execute() throw(StateExit)
{ {
SDL_Event event; SDL_Event event;

View File

@ -19,12 +19,12 @@ class StateExit : public exception {};
class GameState : public MainEvent class GameState : public MainEvent
{ {
public: public:
GameState(stack<GameState*>* state_stack, SDL_Surface* display) GameState(stack<GameState*>* state_stack, SDL_Surface* display);
{ this->state_stack = state_stack, this->display = display; }
virtual ~GameState() {} virtual ~GameState() {}
virtual bool init() = 0; virtual bool init() = 0;
void execute() throw(StateExit); void execute() throw(StateExit);
bool initted() { return init_done;}
protected: protected:
virtual void iterate() = 0; virtual void iterate() = 0;
@ -34,6 +34,7 @@ class GameState : public MainEvent
stack<GameState*>* state_stack; stack<GameState*>* state_stack;
SDL_Surface* display; SDL_Surface* display;
bool init_done;
}; };

View File

@ -31,20 +31,21 @@ int main(int argc, char** argv)
while (!state_stack.empty()) while (!state_stack.empty())
{ {
GameState* state = state_stack.top(); 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 { try {
state->execute(); state->execute();
} catch (StateExit& e) { } catch (StateExit& e) {
// remove the old state // remove the old state
state_stack.pop(); state_stack.pop();
delete state; delete state;
// init the new state, discarding it if we fail
while (!(state_stack.empty() || state_stack.top()->init()))
{
state_stack.pop();
delete state;
}
} }
} }

View File

@ -42,6 +42,7 @@ bool TitleScreen::init()
DrawUtils::transpare(title_banner); DrawUtils::transpare(title_banner);
init_done = true;
return true; return true;
} }