X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FApplication.cpp;h=4de48d4f90541ad55481587c092c772015f88cd2;hb=3f9f41338d8100a719e161a3a1cdda9dd227e2b4;hp=fc1d3801d4ab6fe65f0ea8af622904e0eb01b194;hpb=bce16ed519add0d5398d504d2554395c43c74571;p=l2e.git diff --git a/src/app/Application.cpp b/src/app/Application.cpp index fc1d380..4de48d4 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -16,7 +16,8 @@ namespace app { Application::Application(sdl::InitScreen *screen, State *initialState) : screen(screen) , states() -, last(SDL_GetTicks()) { +, last(SDL_GetTicks()) +, inStateChage(false) { assert(screen && "cannot create application without screen"); assert(initialState && "cannot create application without initial state"); RealPushState(initialState); @@ -28,10 +29,11 @@ Application::~Application() { State *Application::CurrentState() { - return states.top(); + return states.empty() ? 0 : states.top(); } void Application::UpdateState() { + inStateChage = true; while (!stateChanges.empty()) { switch (stateChanges.front().type) { case StateCommand::PUSH: @@ -46,27 +48,40 @@ void Application::UpdateState() { } stateChanges.pop(); } + inStateChage = false; } void Application::ChangeState(State *s) { - StateCommand cmd; - cmd.type = StateCommand::CHANGE; - cmd.state = s; - stateChanges.push(cmd); + if (inStateChage) { + RealChangeState(s); + } else { + StateCommand cmd; + cmd.type = StateCommand::CHANGE; + cmd.state = s; + stateChanges.push(cmd); + } } void Application::PushState(State *s) { - StateCommand cmd; - cmd.type = StateCommand::PUSH; - cmd.state = s; - stateChanges.push(cmd); + if (inStateChage) { + RealPushState(s); + } else { + StateCommand cmd; + cmd.type = StateCommand::PUSH; + cmd.state = s; + stateChanges.push(cmd); + } } void Application::PopState() { - StateCommand cmd; - cmd.type = StateCommand::POP; - cmd.state = 0; - stateChanges.push(cmd); + if (inStateChage) { + RealPopState(); + } else { + StateCommand cmd; + cmd.type = StateCommand::POP; + cmd.state = 0; + stateChanges.push(cmd); + } } void Application::RealChangeState(State *s) { @@ -106,7 +121,10 @@ void Application::Quit() { void Application::PopAllStates() { while (!states.empty()) { - RealPopState(); + states.top()->PauseState(*this, screen->Screen()); + states.top()->ExitState(*this, screen->Screen()); + delete states.top(); + states.pop(); } } @@ -121,7 +139,7 @@ void Application::Loop() { Uint32 now(SDL_GetTicks()); Uint32 deltaT(now - last); GlobalTimers().Update(deltaT); - if (deltaT > 34) deltaT = 34; + if (deltaT > 30) deltaT = 30; if (CurrentState()) { CurrentState()->GraphicsTimers().Update(deltaT); @@ -159,12 +177,12 @@ void Application::HandleEvents() { break; } } - CurrentState()->HandleInput(input); + if (CurrentState()) CurrentState()->HandleEvents(input); } void Application::UpdateWorld(Uint32 deltaT) { if (!CurrentState()) return; - for (Uint32 i(0); i < deltaT; ++i) { + for (Uint32 i(0); i < deltaT && !StateChangePending(); ++i) { CurrentState()->PhysicsTimers().Update(0.001f); CurrentState()->UpdateWorld(0.001f); }