From 3845efbbffe6afefcc2bb0c1bf0e64dabcd702e3 Mon Sep 17 00:00:00 2001 From: Piotr Dziwinski Date: Thu, 25 Oct 2012 20:27:40 +0200 Subject: Main loop enhancement Frame updates were posted one frame behind in event queue --- src/app/app.cpp | 26 ++++++++++++++------------ src/app/app.h | 5 ++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/app/app.cpp b/src/app/app.cpp index 81d874d..4af3870 100644 --- a/src/app/app.cpp +++ b/src/app/app.cpp @@ -829,16 +829,22 @@ int CApplication::Run() m_robotMain->EventProcess(event); } + // Prepare and process step simulation event + event = CreateUpdateEvent(); + if (event.type != EVENT_NULL && m_robotMain != nullptr) + { + m_engine->FrameUpdate(); + m_sound->FrameMove(m_relTime); + + m_robotMain->EventProcess(event); + } + /* Update mouse position explicitly right before rendering * because mouse events are usually way behind */ UpdateMouse(); - // Update game and render a frame during idle time (no messages are waiting) Render(); - // Update simulation state - StepSimulation(); - if (m_lowCPU) { usleep(20000); // should still give plenty of fps @@ -1167,10 +1173,10 @@ void CApplication::SetSimulationSpeed(float speed) GetLogger()->Info("Simulation speed = %.2f\n", speed); } -void CApplication::StepSimulation() +Event CApplication::CreateUpdateEvent() { if (m_simulationSuspended) - return; + return Event(EVENT_NULL); CopyTimeStamp(m_lastTimeStamp, m_curTimeStamp); GetCurrentTimeStamp(m_curTimeStamp); @@ -1185,11 +1191,6 @@ void CApplication::StepSimulation() m_exactRelTime = m_simulationSpeed * m_realRelTime; m_relTime = (m_simulationSpeed * m_realRelTime) / 1e9f; - - m_engine->FrameUpdate(); - m_sound->FrameMove(m_relTime); - - Event frameEvent(EVENT_FRAME); frameEvent.systemEvent = true; frameEvent.trackedKeysState = m_trackedKeys; @@ -1197,7 +1198,8 @@ void CApplication::StepSimulation() frameEvent.mousePos = m_mousePos; frameEvent.mouseButtonsState = m_mouseButtonsState; frameEvent.rTime = m_relTime; - m_eventQueue->AddEvent(frameEvent); + + return frameEvent; } float CApplication::GetSimulationSpeed() diff --git a/src/app/app.h b/src/app/app.h index 32f03f8..66b5848 100644 --- a/src/app/app.h +++ b/src/app/app.h @@ -199,9 +199,6 @@ public: //! Returns whether simulation is suspended bool GetSimulationSuspended(); - //! Updates the simulation state - void StepSimulation(); - //@{ //! Management of simulation speed void SetSimulationSpeed(float speed); @@ -312,6 +309,8 @@ protected: Event ProcessSystemEvent(); //! If applicable, creates a virtual event to match the changed state as of new event Event CreateVirtualEvent(const Event& sourceEvent); + //! Prepares a simulation update event + Event CreateUpdateEvent(); //! Handles some incoming events bool ProcessEvent(const Event& event); //! Renders the image in window -- cgit v1.2.3-1-g7c22