X-Git-Url: http://git.localhorst.tv/?a=blobdiff_plain;f=src%2Fapp%2FApplication.cpp;h=19aa06a18543de3e1c7662a490098d794e8e0aca;hb=628b3a7276d0b330719e05504b23bafcf88f8fca;hp=e06e231d785535488554f9b09975410f8942f667;hpb=4a1816af30dcfe53181a25355bd51cc7b24a83f1;p=l2e.git diff --git a/src/app/Application.cpp b/src/app/Application.cpp index e06e231..19aa06a 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -16,28 +16,41 @@ namespace app { Application::Application(sdl::InitScreen *screen, State *initialState) : screen(screen) , states() -, last(SDL_GetTicks()) { +, last(SDL_GetTicks()) +, toPop(0) { assert(screen && "cannot create application without screen"); assert(initialState && "cannot create application without initial state"); RealPushState(initialState); } -Application::~Application(void) { +Application::~Application() { PopAllStates(); } -State *Application::CurrentState(void) { +State *Application::CurrentState() { return states.top(); } +void Application::UpdateState() { + while (toPop > 0) { + RealPopState(); + --toPop; + } + while (!toPush.empty()) { + State *s(toPush.front()); + toPush.pop(); + RealPushState(s); + } +} + void Application::ChangeState(State *s) { - RealPopState(); - RealPushState(s); + PopState(); + PushState(s); } void Application::PushState(State *s) { - RealPushState(s); + toPush.push(s); } void Application::RealPushState(State *s) { @@ -45,35 +58,35 @@ void Application::RealPushState(State *s) { s->EnterState(*this, screen->Screen()); } -void Application::PopState(void) { - RealPopState(); +void Application::PopState() { + ++toPop; } -void Application::RealPopState(void) { +void Application::RealPopState() { if (states.empty()) return; states.top()->ExitState(); delete states.top(); states.pop(); } -void Application::Quit(void) { +void Application::Quit() { PopAllStates(); } -void Application::PopAllStates(void) { +void Application::PopAllStates() { while (!states.empty()) { RealPopState(); } } -void Application::Run(void) { +void Application::Run() { while (CurrentState()) { Loop(); } } -void Application::Loop(void) { +void Application::Loop() { Uint32 now(SDL_GetTicks()); Uint32 deltaT(now - last); if (deltaT > 34) deltaT = 34; @@ -83,11 +96,13 @@ void Application::Loop(void) { Render(); last = now; + UpdateState(); } -void Application::HandleEvents(void) { +void Application::HandleEvents() { if (!CurrentState()) return; + input.ResetInteractiveState(); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { @@ -98,11 +113,16 @@ void Application::HandleEvents(void) { screen->Resize(event.resize.w, event.resize.h); CurrentState()->Resize(event.resize.w, event.resize.h); break; + case SDL_KEYDOWN: + case SDL_KEYUP: + input.HandleKeyboardEvent(event.key); + break; default: - CurrentState()->HandleEvent(event); + // skip event break; } } + CurrentState()->HandleInput(input); } void Application::UpdateWorld(Uint32 deltaT) {