Add Time.Synchronize to stabilize engine loop

This commit is contained in:
Wojtek Figat
2024-04-17 13:01:58 +02:00
parent c83b74c85d
commit 692a61c948
4 changed files with 16 additions and 10 deletions

View File

@@ -163,6 +163,8 @@ namespace FlaxEditor.States
Editor.OnPlayBegin(); Editor.OnPlayBegin();
IsPlayModeStarting = false; IsPlayModeStarting = false;
Profiler.EndEvent(); Profiler.EndEvent();
Time.Synchronize();
} }
private void SetupEditorEnvOptions() private void SetupEditorEnvOptions()
@@ -209,6 +211,8 @@ namespace FlaxEditor.States
Editor.OnPlayEnd(); Editor.OnPlayEnd();
IsPlayModeEnding = false; IsPlayModeEnding = false;
Profiler.EndEvent(); Profiler.EndEvent();
Time.Synchronize();
} }
} }
} }

View File

@@ -156,7 +156,7 @@ int32 Engine::Main(const Char* cmdLine)
#endif #endif
Log::Logger::WriteFloor(); Log::Logger::WriteFloor();
LOG_FLUSH(); LOG_FLUSH();
Time::OnBeforeRun(); Time::Synchronize();
EngineImpl::IsReady = true; EngineImpl::IsReady = true;
// Main engine loop // Main engine loop
@@ -465,7 +465,7 @@ void Engine::OnUnpause()
LOG(Info, "App unpaused"); LOG(Info, "App unpaused");
Unpause(); Unpause();
Time::OnBeforeRun(); Time::Synchronize();
} }
void Engine::OnExit() void Engine::OnExit()

View File

@@ -54,7 +54,7 @@ void TimeSettings::Apply()
::MaxUpdateDeltaTime = MaxUpdateDeltaTime; ::MaxUpdateDeltaTime = MaxUpdateDeltaTime;
} }
void Time::TickData::OnBeforeRun(float targetFps, double currentTime) void Time::TickData::Synchronize(float targetFps, double currentTime)
{ {
Time = UnscaledTime = TimeSpan::Zero(); Time = UnscaledTime = TimeSpan::Zero();
DeltaTime = UnscaledDeltaTime = targetFps > ZeroTolerance ? TimeSpan::FromSeconds(1.0f / targetFps) : 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; FixedDeltaTimeValue = value;
} }
void Time::OnBeforeRun() void Time::Synchronize()
{ {
// Initialize tick data (based on a time settings) // Initialize tick data (based on a time settings)
const double time = Platform::GetTimeSeconds(); const double time = Platform::GetTimeSeconds();
Update.OnBeforeRun(UpdateFPS, time); Update.Synchronize(UpdateFPS, time);
Physics.OnBeforeRun(PhysicsFPS, time); Physics.Synchronize(PhysicsFPS, time);
Draw.OnBeforeRun(DrawFPS, time); Draw.Synchronize(DrawFPS, time);
} }
bool Time::OnBeginUpdate() bool Time::OnBeginUpdate()

View File

@@ -225,12 +225,14 @@ public:
/// <param name="value">The fixed draw/update rate for the time.</param> /// <param name="value">The fixed draw/update rate for the time.</param>
API_FUNCTION() static void SetFixedDeltaTime(bool enable, float value); API_FUNCTION() static void SetFixedDeltaTime(bool enable, float value);
/// <summary>
/// Synchronizes update, fixed update and draw. Resets any pending deltas for fresh ticking in sync.
/// </summary>
API_FUNCTION() static void Synchronize();
private: private:
// Methods used by the Engine class // Methods used by the Engine class
static void OnBeforeRun();
static bool OnBeginUpdate(); static bool OnBeginUpdate();
static bool OnBeginPhysics(); static bool OnBeginPhysics();
static bool OnBeginDraw(); static bool OnBeginDraw();