From fb3eb9443e9263aa7f71127b25c73482e6f2088c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Leclercq?= Date: Wed, 7 Feb 2018 15:02:44 +0100 Subject: [PATCH] Core/Clock: Restart now returns the elapsed time in milliseconds --- ChangeLog.md | 1 + SDK/src/NDK/Application.cpp | 3 +-- examples/FirstScene/main.cpp | 4 +--- include/Nazara/Core/Clock.hpp | 4 ++-- src/Nazara/Core/Clock.cpp | 17 ++++++++++++++--- tests/Engine/Platform/EventHandler.cpp | 3 +-- 6 files changed, 20 insertions(+), 12 deletions(-) diff --git a/ChangeLog.md b/ChangeLog.md index da2a0c98e..1446257a5 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -59,6 +59,7 @@ Nazara Engine: - Fix LuaClass not working correctly when Lua stack wasn't empty - Add RigidBody2D simulation control (via EnableSimulation and IsSimulationEnabled), which allows to disable physics and collisions at will. - ⚠️ LuaInstance no longer load all lua libraries on construction, this is done in the new LoadLibraries method which allows you to excludes some libraries +- Clock::Restart now returns the elapsed microseconds since construction or last Restart call Nazara Development Kit: - Added ImageWidget (#139) diff --git a/SDK/src/NDK/Application.cpp b/SDK/src/NDK/Application.cpp index 5e0f07d00..a4fca055a 100644 --- a/SDK/src/NDK/Application.cpp +++ b/SDK/src/NDK/Application.cpp @@ -107,8 +107,7 @@ namespace Ndk if (m_shouldQuit) return false; - m_updateTime = m_updateClock.GetSeconds(); - m_updateClock.Restart(); + m_updateTime = m_updateClock.Restart() / 1'000'000.f; for (World& world : m_worlds) world.Update(m_updateTime); diff --git a/examples/FirstScene/main.cpp b/examples/FirstScene/main.cpp index 00fbebae1..2aec4861e 100644 --- a/examples/FirstScene/main.cpp +++ b/examples/FirstScene/main.cpp @@ -325,9 +325,7 @@ int main() while (application.Run()) { - Nz::UInt64 elapsedUS = updateClock.GetMicroseconds(); - // On relance l'horloge - updateClock.Restart(); + Nz::UInt64 elapsedUS = updateClock.Restart() / 1'000'000; // Mise à jour (Caméra) const Nz::UInt64 updateRate = 1000000 / 60; // 60 fois par seconde diff --git a/include/Nazara/Core/Clock.hpp b/include/Nazara/Core/Clock.hpp index 699f1a78c..a6b464db6 100644 --- a/include/Nazara/Core/Clock.hpp +++ b/include/Nazara/Core/Clock.hpp @@ -32,7 +32,7 @@ namespace Nz bool IsPaused() const; void Pause(); - void Restart(); + UInt64 Restart(); void Unpause(); Clock& operator=(const Clock& clock) = default; @@ -46,7 +46,7 @@ namespace Nz bool m_paused; }; - typedef UInt64 (*ClockFunction)(); + using ClockFunction = UInt64 (*)(); extern NAZARA_CORE_API ClockFunction GetElapsedMicroseconds; extern NAZARA_CORE_API ClockFunction GetElapsedMilliseconds; diff --git a/src/Nazara/Core/Clock.cpp b/src/Nazara/Core/Clock.cpp index 92183674c..13554a1fa 100644 --- a/src/Nazara/Core/Clock.cpp +++ b/src/Nazara/Core/Clock.cpp @@ -67,7 +67,7 @@ namespace Nz */ float Clock::GetSeconds() const { - return GetMicroseconds()/1000000.f; + return GetMicroseconds()/1'000'000.f; } /*! @@ -132,15 +132,26 @@ namespace Nz /*! * \brief Restart the clock + * \return Microseconds elapsed + * * Restarts the clock, putting it's time counter back to zero (as if the clock got constructed). + * It also compute the elapsed microseconds since the last Restart() call without any time loss (a problem that the combination of GetElapsedMicroseconds and Restart have). */ - void Clock::Restart() + UInt64 Clock::Restart() { NazaraLock(m_mutex); + Nz::UInt64 now = GetElapsedMicroseconds(); + + Nz::UInt64 elapsedTime = m_elapsedTime; + if (!m_paused) + elapsedTime += (now - m_refTime); + m_elapsedTime = 0; - m_refTime = GetElapsedMicroseconds(); + m_refTime = now; m_paused = false; + + return elapsedTime; } /*! diff --git a/tests/Engine/Platform/EventHandler.cpp b/tests/Engine/Platform/EventHandler.cpp index b143b949a..a2f64e9f4 100644 --- a/tests/Engine/Platform/EventHandler.cpp +++ b/tests/Engine/Platform/EventHandler.cpp @@ -51,9 +51,8 @@ SCENARIO("EventHandler", "[PLATFORM][EVENTHANDLER][INTERACTIVE][.]") while (app.Run()) { window.Display(); - float elapsedTime = elapsedTimeClock.GetSeconds(); - elapsedTimeClock.Restart(); + float elapsedTime = elapsedTimeClock.Restart() / 1'000'000; if (!fsm.Update(elapsedTime)) { NazaraError("Failed to update state machine.");