From 9aeaaa94952978661633d87d4ecff007ed5273a0 Mon Sep 17 00:00:00 2001 From: SirLynix Date: Sat, 6 May 2023 15:44:52 +0200 Subject: [PATCH] Core/Application: Fix updater elapsed time --- include/Nazara/Core/ApplicationBase.inl | 2 +- src/Nazara/Core/ApplicationBase.cpp | 2 +- tests/UnitTests/Engine/Core/ApplicationTest.cpp | 16 ++++++++++++---- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/include/Nazara/Core/ApplicationBase.inl b/include/Nazara/Core/ApplicationBase.inl index 60c612304..5dca39b4a 100644 --- a/include/Nazara/Core/ApplicationBase.inl +++ b/include/Nazara/Core/ApplicationBase.inl @@ -21,7 +21,7 @@ namespace Nz inline void ApplicationBase::AddUpdater(std::unique_ptr&& functor) { auto& updaterEntry = m_updaters.emplace_back(); - updaterEntry.lastUpdate = Time::Zero(); + updaterEntry.lastUpdate = -Time::Nanosecond(); updaterEntry.nextUpdate = Time::Zero(); updaterEntry.updater = std::move(functor); } diff --git a/src/Nazara/Core/ApplicationBase.cpp b/src/Nazara/Core/ApplicationBase.cpp index e2522450b..2b6e3e018 100644 --- a/src/Nazara/Core/ApplicationBase.cpp +++ b/src/Nazara/Core/ApplicationBase.cpp @@ -65,7 +65,7 @@ namespace Nz Time timeSinceLastUpdate = m_currentTime - updaterEntry.lastUpdate; - if NAZARA_UNLIKELY(updaterEntry.nextUpdate == updaterEntry.lastUpdate) + if NAZARA_UNLIKELY(updaterEntry.lastUpdate < Time::Zero()) { // First call timeSinceLastUpdate = Time::Zero(); diff --git a/tests/UnitTests/Engine/Core/ApplicationTest.cpp b/tests/UnitTests/Engine/Core/ApplicationTest.cpp index 1650e3701..be4cceb99 100644 --- a/tests/UnitTests/Engine/Core/ApplicationTest.cpp +++ b/tests/UnitTests/Engine/Core/ApplicationTest.cpp @@ -16,15 +16,16 @@ SCENARIO("Application", "[CORE][ABSTRACTHASH]") INFO("First update should have elapsed time as zero"); CHECK(elapsedTime == Nz::Time::Zero()); } + else + CHECK(elapsedTime == Nz::Time::Milliseconds(10)); triggerCount++; }); - app.Update(Nz::Time::Milliseconds(10)); - app.Update(Nz::Time::Milliseconds(10)); - app.Update(Nz::Time::Milliseconds(10)); + for (std::size_t i = 0; i < 10; ++i) + app.Update(Nz::Time::Milliseconds(10)); - CHECK(triggerCount == 3); + CHECK(triggerCount == 10); } WHEN("Using interval") @@ -40,7 +41,11 @@ SCENARIO("Application", "[CORE][ABSTRACTHASH]") CHECK(elapsedTime == Nz::Time::Zero()); } else + { + INFO("Following update should have elapsed time >= interval"); + CHECK(elapsedTime >= Nz::Time::Milliseconds(100)); CHECK(elapsedTime < Nz::Time::Milliseconds(200)); + } triggerCount++; }); @@ -57,6 +62,9 @@ SCENARIO("Application", "[CORE][ABSTRACTHASH]") CHECK(triggerCount == 3); app.Update(Nz::Time::Milliseconds(100)); CHECK(triggerCount == 4); + for (std::size_t i = 0; i < 10; ++i) + app.Update(Nz::Time::Milliseconds(10)); + CHECK(triggerCount == 5); } WHEN("Using fixed-time interval")