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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
init_done = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -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;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
17
main.cpp
17
main.cpp
|
@ -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();
|
||||||
try {
|
|
||||||
state->execute();
|
|
||||||
} catch (StateExit& e) {
|
|
||||||
|
|
||||||
// remove the old state
|
|
||||||
state_stack.pop();
|
|
||||||
delete state;
|
|
||||||
|
|
||||||
// init the new state, discarding it if we fail
|
// init the new state, discarding it if we fail
|
||||||
while (!(state_stack.empty() || state_stack.top()->init()))
|
if (!(state->initted() || state->init()))
|
||||||
{
|
{
|
||||||
state_stack.pop();
|
state_stack.pop();
|
||||||
delete state;
|
delete state;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
state->execute();
|
||||||
|
} catch (StateExit& e) {
|
||||||
|
// remove the old state
|
||||||
|
state_stack.pop();
|
||||||
|
delete state;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,6 +42,7 @@ bool TitleScreen::init()
|
||||||
|
|
||||||
DrawUtils::transpare(title_banner);
|
DrawUtils::transpare(title_banner);
|
||||||
|
|
||||||
|
init_done = true;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user