From: Daniel Karbach Date: Tue, 20 Aug 2013 07:42:30 +0000 (+0200) Subject: added option for sped up integration X-Git-Url: https://git.localhorst.tv/?a=commitdiff_plain;h=60e0b9e8ec660a4f6b8e500bf0b77a31bb817b45;p=l2e.git added option for sped up integration --- diff --git a/src/app/Application.cpp b/src/app/Application.cpp index 64a8602..830223e 100644 --- a/src/app/Application.cpp +++ b/src/app/Application.cpp @@ -11,6 +11,7 @@ Application::Application(sdl::InitScreen &screen, State *initialState) : screen(screen) , states() , last(SDL_GetTicks()) +, remaining(0) , inStateChage(false) { assert(initialState && "cannot create application without initial state"); RealPushState(initialState); @@ -152,7 +153,7 @@ void Application::HandleEvents() { if (!CurrentState()) return; input.ResetInteractiveState(); SDL_Event event; - while (SDL_PollEvent(&event)) { + while (!StateChangePending() && SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: PopAllStates(); @@ -175,9 +176,16 @@ void Application::HandleEvents() { void Application::UpdateWorld(Uint32 deltaT) { if (!CurrentState()) return; - for (Uint32 i(0); i < deltaT && !StateChangePending(); ++i) { - CurrentState()->PhysicsTimers().Update(1); - CurrentState()->UpdateWorld(1); + remaining += deltaT; + Uint32 step = CurrentState()->Timestep(); + if (step > 0) { + for (; remaining >= step && !StateChangePending(); remaining -= step) { + CurrentState()->PhysicsTimers().Update(step); + CurrentState()->UpdateWorld(step); + } + } else { + CurrentState()->PhysicsTimers().Update(deltaT); + CurrentState()->UpdateWorld(deltaT); } } diff --git a/src/app/Application.h b/src/app/Application.h index 817f79f..db089e0 100644 --- a/src/app/Application.h +++ b/src/app/Application.h @@ -82,6 +82,7 @@ private: Timers globalTimers; Input input; Uint32 last; + Uint32 remaining; bool inStateChage; }; diff --git a/src/app/State.h b/src/app/State.h index 84082ae..31a4ae0 100644 --- a/src/app/State.h +++ b/src/app/State.h @@ -39,6 +39,9 @@ public: /// Draw a picture of the world. virtual void Render(SDL_Surface *) = 0; + /// Number of milliseconds per simulation frame. + Uint32 Timestep() const { return timestep; } + protected: /// Get a handle to the application this state is running on. /// Do not call this while the state is off the stack (e.g. in c'tor/d'tor) @@ -46,6 +49,9 @@ protected: Application &Ctrl(); const Application &Ctrl() const; + /// Set the number of milliseconds per simulation frame. + void Timestep(Uint32 t) { timestep = t; } + private: /// Do some setup that needs an application and/or screen handle and thus /// can not be done by the constructor. @@ -63,23 +69,24 @@ private: /// Adapt the state's graphics to given dimensions. /// NOTE: currently, this is only called for the stack top and not /// propagated on stack changes. - /// Will be fixed soom ;). + /// Will be fixed sometime ;). virtual void OnResize(int width, int height) = 0; public: /// Timers handle intended for graphics, sync'ed with render time. /// These timers are only updated for the stack top and thus appear paused - /// when the state is visible (roughly). + /// when the state is invisible (roughly). Timers &GraphicsTimers() { return graphicsTimers; } /// Timers handle intended for graphics, sync'ed with world time. /// These timers are only updated for the stack top and thus appear paused - /// when the state is visible (roughly). + /// when the state is invisible (roughly). Timers &PhysicsTimers() { return physicsTimers; } private: Application *ctrl; Timers graphicsTimers; Timers physicsTimers; + Uint32 timestep; };