From 692a61c94826957073465078b54ec04e7dcb9759 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 17 Apr 2024 13:01:58 +0200 Subject: [PATCH] Add `Time.Synchronize` to stabilize engine loop --- Source/Editor/States/PlayingState.cs | 4 ++++ Source/Engine/Engine/Engine.cpp | 4 ++-- Source/Engine/Engine/Time.cpp | 10 +++++----- Source/Engine/Engine/Time.h | 8 +++++--- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Source/Editor/States/PlayingState.cs b/Source/Editor/States/PlayingState.cs index 1be2a1816..b2654f3d9 100644 --- a/Source/Editor/States/PlayingState.cs +++ b/Source/Editor/States/PlayingState.cs @@ -163,6 +163,8 @@ namespace FlaxEditor.States Editor.OnPlayBegin(); IsPlayModeStarting = false; Profiler.EndEvent(); + + Time.Synchronize(); } private void SetupEditorEnvOptions() @@ -209,6 +211,8 @@ namespace FlaxEditor.States Editor.OnPlayEnd(); IsPlayModeEnding = false; Profiler.EndEvent(); + + Time.Synchronize(); } } } diff --git a/Source/Engine/Engine/Engine.cpp b/Source/Engine/Engine/Engine.cpp index 27b35a0b7..abebbdb91 100644 --- a/Source/Engine/Engine/Engine.cpp +++ b/Source/Engine/Engine/Engine.cpp @@ -156,7 +156,7 @@ int32 Engine::Main(const Char* cmdLine) #endif Log::Logger::WriteFloor(); LOG_FLUSH(); - Time::OnBeforeRun(); + Time::Synchronize(); EngineImpl::IsReady = true; // Main engine loop @@ -465,7 +465,7 @@ void Engine::OnUnpause() LOG(Info, "App unpaused"); Unpause(); - Time::OnBeforeRun(); + Time::Synchronize(); } void Engine::OnExit() diff --git a/Source/Engine/Engine/Time.cpp b/Source/Engine/Engine/Time.cpp index 6df5275f5..7f4571b32 100644 --- a/Source/Engine/Engine/Time.cpp +++ b/Source/Engine/Engine/Time.cpp @@ -54,7 +54,7 @@ void TimeSettings::Apply() ::MaxUpdateDeltaTime = MaxUpdateDeltaTime; } -void Time::TickData::OnBeforeRun(float targetFps, double currentTime) +void Time::TickData::Synchronize(float targetFps, double currentTime) { Time = UnscaledTime = TimeSpan::Zero(); DeltaTime = UnscaledDeltaTime = targetFps > ZeroTolerance ? TimeSpan::FromSeconds(1.0f / targetFps) : TimeSpan::Zero(); @@ -240,13 +240,13 @@ void Time::SetFixedDeltaTime(bool enable, float value) FixedDeltaTimeValue = value; } -void Time::OnBeforeRun() +void Time::Synchronize() { // Initialize tick data (based on a time settings) const double time = Platform::GetTimeSeconds(); - Update.OnBeforeRun(UpdateFPS, time); - Physics.OnBeforeRun(PhysicsFPS, time); - Draw.OnBeforeRun(DrawFPS, time); + Update.Synchronize(UpdateFPS, time); + Physics.Synchronize(PhysicsFPS, time); + Draw.Synchronize(DrawFPS, time); } bool Time::OnBeginUpdate() diff --git a/Source/Engine/Engine/Time.h b/Source/Engine/Engine/Time.h index 06e0d1cc5..4bb269cb9 100644 --- a/Source/Engine/Engine/Time.h +++ b/Source/Engine/Engine/Time.h @@ -225,12 +225,14 @@ public: /// The fixed draw/update rate for the time. API_FUNCTION() static void SetFixedDeltaTime(bool enable, float value); + /// + /// Synchronizes update, fixed update and draw. Resets any pending deltas for fresh ticking in sync. + /// + API_FUNCTION() static void Synchronize(); + private: - // Methods used by the Engine class - static void OnBeforeRun(); - static bool OnBeginUpdate(); static bool OnBeginPhysics(); static bool OnBeginDraw();