Reworked initting code so new states get initted properly in main loop
This commit is contained in:
parent
303f1d4511
commit
b50ed649f5
1
game.cpp
1
game.cpp
|
@ -31,6 +31,7 @@ bool Game::init()
|
|||
return false;
|
||||
}
|
||||
|
||||
init_done = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
||||
|
||||
|
|
17
main.cpp
17
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ bool TitleScreen::init()
|
|||
|
||||
DrawUtils::transpare(title_banner);
|
||||
|
||||
init_done = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user