X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FApplication.cpp;h=b84d7248da1a465b7eafa557cc85c0d393b6789f;hb=2ccc2369d32fb680a3047519d79c17de34c4e10a;hp=29eaee781e9ba20780fafa2cc4bde331777d059a;hpb=25d7499f7923291d7287123320e7563a367e92e7;p=l2e.git diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 29eaee7..b84d724 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -13,11 +13,11 @@ 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); } @@ -28,10 +28,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,57 +47,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()->OnPauseState(*this, screen.Screen()); + states.top()->OnExitState(*this, screen.Screen()); states.pop(); } states.push(s); - s->EnterState(*this, screen->Screen()); - s->ResumeState(*this, screen->Screen()); + s->OnEnterState(*this, screen.Screen()); + s->OnResumeState(*this, screen.Screen()); } void Application::RealPushState(State *s) { if (!states.empty()) { - states.top()->PauseState(*this, screen->Screen()); + states.top()->OnPauseState(*this, screen.Screen()); } states.push(s); - s->EnterState(*this, screen->Screen()); - s->ResumeState(*this, screen->Screen()); + s->OnEnterState(*this, screen.Screen()); + s->OnResumeState(*this, screen.Screen()); } void Application::RealPopState() { if (states.empty()) return; - states.top()->PauseState(*this, screen->Screen()); - states.top()->ExitState(*this, screen->Screen()); + states.top()->OnPauseState(*this, screen.Screen()); + states.top()->OnExitState(*this, screen.Screen()); delete states.top(); states.pop(); if (!states.empty()) { - states.top()->ResumeState(*this, screen->Screen()); + states.top()->OnResumeState(*this, screen.Screen()); } } @@ -106,7 +120,10 @@ void Application::Quit() { void Application::PopAllStates() { while (!states.empty()) { - RealPopState(); + states.top()->OnPauseState(*this, screen.Screen()); + states.top()->OnExitState(*this, screen.Screen()); + delete states.top(); + states.pop(); } } @@ -120,8 +137,12 @@ void Application::Run() { void Application::Loop() { Uint32 now(SDL_GetTicks()); Uint32 deltaT(now - last); - if (deltaT > 34) deltaT = 34; + GlobalTimers().Update(deltaT); + if (deltaT > 30) deltaT = 30; + if (CurrentState()) { + CurrentState()->GraphicsTimers().Update(deltaT); + } HandleEvents(); if (!StateChangePending()) { UpdateWorld(deltaT); @@ -143,8 +164,8 @@ void Application::HandleEvents() { PopAllStates(); break; case SDL_VIDEORESIZE: - screen->Resize(event.resize.w, event.resize.h); - CurrentState()->Resize(event.resize.w, event.resize.h); + screen.Resize(event.resize.w, event.resize.h); + CurrentState()->OnResize(event.resize.w, event.resize.h); break; case SDL_KEYDOWN: case SDL_KEYUP: @@ -155,20 +176,21 @@ 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); } } void Application::Render(void) { if (!CurrentState()) return; - CurrentState()->Render(screen->Screen()); - screen->Flip(); + CurrentState()->Render(screen.Screen()); + screen.Flip(); } }