From d7d8eca9c841027e9f33fc16b7987fdae94176df Mon Sep 17 00:00:00 2001 From: GoaLitiuM Date: Sun, 5 Sep 2021 03:17:08 +0300 Subject: [PATCH 1/2] Fix engine ticking to catch up faster when falling behind --- Source/Engine/Engine/Time.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Engine/Time.cpp b/Source/Engine/Engine/Time.cpp index 36db6a236..bf34bbdd6 100644 --- a/Source/Engine/Engine/Time.cpp +++ b/Source/Engine/Engine/Time.cpp @@ -103,7 +103,10 @@ bool Time::TickData::OnTickBegin(float targetFps, float maxDeltaTime) } if (targetFps > ZeroTolerance) - NextBegin += (1.0 / targetFps); + { + int skip = (int)(1 + (time - NextBegin) / (1.0 / targetFps)); + NextBegin += (1.0 / targetFps) * skip; + } } // Update data @@ -156,7 +159,10 @@ bool Time::FixedStepTickData::OnTickBegin(float targetFps, float maxDeltaTime) } if (targetFps > ZeroTolerance) - NextBegin += (1.0 / targetFps); + { + int skip = (int)(1 + (time - NextBegin) / (1.0 / targetFps)); + NextBegin += (1.0 / targetFps) * skip; + } } Samples.Add(deltaTime); From 17311e7c9e1c7ee60f214dde46b32a4274c6dd95 Mon Sep 17 00:00:00 2001 From: GoaLitiuM Date: Sun, 5 Dec 2021 16:43:24 +0200 Subject: [PATCH 2/2] Add some tolerance in checks against zero floating point values --- Source/Engine/Engine/Engine.cpp | 2 +- Source/Engine/Engine/Time.cpp | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Source/Engine/Engine/Engine.cpp b/Source/Engine/Engine/Engine.cpp index a50999d24..7b991bf8e 100644 --- a/Source/Engine/Engine/Engine.cpp +++ b/Source/Engine/Engine/Engine.cpp @@ -146,7 +146,7 @@ int32 Engine::Main(const Char* cmdLine) while (!ShouldExit()) { // Reduce CPU usage by introducing idle time if the engine is running very fast and has enough time to spend - if ((useSleep && Time::UpdateFPS > 0) || !Platform::GetHasFocus()) + if ((useSleep && Time::UpdateFPS > ZeroTolerance) || !Platform::GetHasFocus()) { double nextTick = Time::GetNextTick(); double timeToTick = nextTick - Platform::GetTimeSeconds(); diff --git a/Source/Engine/Engine/Time.cpp b/Source/Engine/Engine/Time.cpp index bf34bbdd6..81f8b7d0d 100644 --- a/Source/Engine/Engine/Time.cpp +++ b/Source/Engine/Engine/Time.cpp @@ -189,11 +189,11 @@ double Time::GetNextTick() const double nextDraw = Time::Draw.NextBegin; double nextTick = MAX_double; - if (UpdateFPS > 0 && nextUpdate < nextTick) + if (UpdateFPS > ZeroTolerance && nextUpdate < nextTick) nextTick = nextUpdate; - if (PhysicsFPS > 0 && nextPhysics < nextTick) + if (PhysicsFPS > ZeroTolerance && nextPhysics < nextTick) nextTick = nextPhysics; - if (DrawFPS > 0 && nextDraw < nextTick) + if (DrawFPS > ZeroTolerance && nextDraw < nextTick) nextTick = nextDraw; if (nextTick == MAX_double)