X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FApplication.cpp;h=64a8602e8d9b1c551bd2940224c3a1ab72746292;hb=092a2dd175a4001a495c84ee85211734fb928c83;hp=4c8c5747ff71dd5620fe2a6d26c96fdf0efc4e03;hpb=69ed4a9c725c438079ceeed89d49656d808308ce;p=l2e.git diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 4c8c574..64a8602 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -1,23 +1,17 @@ -/* - * Application.cpp - * - * Created on: Apr 8, 2012 - * Author: holy - */ - #include "Application.h" #include "State.h" +#include "../sdl/InitScreen.h" #include namespace app { -Application::Application(sdl::InitScreen *screen, State *initialState) +Application::Application(sdl::InitScreen &screen, State *initialState) : screen(screen) , states() -, last(SDL_GetTicks()) { - assert(screen && "cannot create application without screen"); +, last(SDL_GetTicks()) +, inStateChage(false) { assert(initialState && "cannot create application without initial state"); RealPushState(initialState); } @@ -32,6 +26,7 @@ State *Application::CurrentState() { } void Application::UpdateState() { + inStateChage = true; while (!stateChanges.empty()) { switch (stateChanges.front().type) { case StateCommand::PUSH: @@ -46,57 +41,70 @@ 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) { if (!states.empty()) { - states.top()->PauseState(*this, screen->Screen()); - states.top()->ExitState(*this, screen->Screen()); + states.top()->PauseState(screen.Screen()); + states.top()->ExitState(*this, screen.Screen()); states.pop(); } states.push(s); - s->EnterState(*this, screen->Screen()); - s->ResumeState(*this, screen->Screen()); + s->EnterState(*this, screen.Screen()); + s->ResumeState(screen.Screen()); } void Application::RealPushState(State *s) { if (!states.empty()) { - states.top()->PauseState(*this, screen->Screen()); + states.top()->PauseState(screen.Screen()); } states.push(s); - s->EnterState(*this, screen->Screen()); - s->ResumeState(*this, screen->Screen()); + s->EnterState(*this, screen.Screen()); + s->ResumeState(screen.Screen()); } void Application::RealPopState() { if (states.empty()) return; - states.top()->PauseState(*this, screen->Screen()); - states.top()->ExitState(*this, screen->Screen()); + states.top()->PauseState(screen.Screen()); + states.top()->ExitState(*this, screen.Screen()); delete states.top(); states.pop(); if (!states.empty()) { - states.top()->ResumeState(*this, screen->Screen()); + states.top()->ResumeState(screen.Screen()); } } @@ -106,8 +114,8 @@ void Application::Quit() { void Application::PopAllStates() { while (!states.empty()) { - states.top()->PauseState(*this, screen->Screen()); - states.top()->ExitState(*this, screen->Screen()); + states.top()->PauseState(screen.Screen()); + states.top()->ExitState(*this, screen.Screen()); delete states.top(); states.pop(); } @@ -150,7 +158,7 @@ void Application::HandleEvents() { PopAllStates(); break; case SDL_VIDEORESIZE: - screen->Resize(event.resize.w, event.resize.h); + screen.Resize(event.resize.w, event.resize.h); CurrentState()->Resize(event.resize.w, event.resize.h); break; case SDL_KEYDOWN: @@ -167,16 +175,16 @@ void Application::HandleEvents() { void Application::UpdateWorld(Uint32 deltaT) { if (!CurrentState()) return; - for (Uint32 i(0); i < deltaT; ++i) { - CurrentState()->PhysicsTimers().Update(0.001f); - CurrentState()->UpdateWorld(0.001f); + for (Uint32 i(0); i < deltaT && !StateChangePending(); ++i) { + CurrentState()->PhysicsTimers().Update(1); + CurrentState()->UpdateWorld(1); } } void Application::Render(void) { if (!CurrentState()) return; - CurrentState()->Render(screen->Screen()); - screen->Flip(); + CurrentState()->Render(screen.Screen()); + screen.Flip(); } }