From: Daniel Karbach Date: Mon, 6 Aug 2012 13:55:49 +0000 (+0200) Subject: delayed state pushing/popping till the end of each loop X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=c8bfdbd3b824fdd71bca1335a145aa19c42df1db;p=l2e.git delayed state pushing/popping till the end of each loop --- diff --git a/src/app/Application.cpp b/src/app/Application.cpp index d3b00a1..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,10 +96,11 @@ void Application::Loop(void) { Render(); last = now; + UpdateState(); } -void Application::HandleEvents(void) { +void Application::HandleEvents() { if (!CurrentState()) return; input.ResetInteractiveState(); SDL_Event event; diff --git a/src/app/Application.h b/src/app/Application.h index f70dd69..dcc5e00 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -12,6 +12,7 @@ #include "../sdl/InitScreen.h" #include +#include #include @@ -23,39 +24,42 @@ class Application { public: Application(sdl::InitScreen *screen, State *initialState); - ~Application(void); + ~Application(); private: Application(const Application &); Application &operator =(const Application &); public: - void Run(void); - void Loop(void); + void Run(); + void Loop(); public: void ChangeState(State *); void PushState(State *); - void PopState(void); - void Quit(void); + void PopState(); + void Quit(); Input &Buttons() { return input; } const Input &Buttons() const { return input; } private: - State *CurrentState(void); + State *CurrentState(); + void UpdateState(); void RealPushState(State *); - void RealPopState(void); - void PopAllStates(void); + void RealPopState(); + void PopAllStates(); private: - void HandleEvents(void); + void HandleEvents(); void UpdateWorld(Uint32 deltaT); - void Render(void); + void Render(); private: sdl::InitScreen *screen; std::stack states; + std::queue toPush; Input input; Uint32 last; + int toPop; };