From 8d4e4cd37d6d67b77ab1843657d52c4b349448be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tur=C3=A1nszki=20J=C3=A1nos?= Date: Sun, 12 Feb 2023 11:44:09 +0100 Subject: [PATCH] improved framerate locking --- WickedEngine/wiApplication.cpp | 15 ++++++++++----- WickedEngine/wiHelper.cpp | 24 +++++++++++++++++------- WickedEngine/wiHelper.h | 5 +++++ WickedEngine/wiVersion.cpp | 2 +- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/WickedEngine/wiApplication.cpp b/WickedEngine/wiApplication.cpp index 02cfdc598..dfc5156d8 100644 --- a/WickedEngine/wiApplication.cpp +++ b/WickedEngine/wiApplication.cpp @@ -140,14 +140,19 @@ namespace wi deltaTime = float(std::max(0.0, timer.record_elapsed_seconds())); + const float target_deltaTime = 1.0f / targetFrameRate; + if (framerate_lock && deltaTime < target_deltaTime) + { + wi::helper::QuickSleep((target_deltaTime - deltaTime) * 1000); + deltaTime += float(std::max(0.0, timer.record_elapsed_seconds())); + } + wi::input::Update(window, canvas); // Wake up the events that need to be executed on the main thread, in thread safe manner: wi::eventhandler::FireEvent(wi::eventhandler::EVENT_THREAD_SAFE_POINT, 0); - const float dt = framerate_lock ? (1.0f / targetFrameRate) : deltaTime; - - fadeManager.Update(dt); + fadeManager.Update(deltaTime); if (GetActivePath() != nullptr) { @@ -161,7 +166,7 @@ namespace wi { if (frameskip) { - deltaTimeAccumulator += dt; + deltaTimeAccumulator += deltaTime; if (deltaTimeAccumulator > 10) { // application probably lost control, fixed update would take too long @@ -183,7 +188,7 @@ namespace wi wi::profiler::EndRange(range); // Fixed Update // Variable-timed update: - Update(dt); + Update(deltaTime); Render(); diff --git a/WickedEngine/wiHelper.cpp b/WickedEngine/wiHelper.cpp index 816ffc530..3d9f7b8a9 100644 --- a/WickedEngine/wiHelper.cpp +++ b/WickedEngine/wiHelper.cpp @@ -1236,14 +1236,23 @@ namespace wi::helper void Spin(float milliseconds) { - milliseconds /= 1000.0f; - std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); - double ms = 0; - while (ms < milliseconds) + const std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + const double seconds = double(milliseconds) / 1000.0; + while (std::chrono::duration_cast>(std::chrono::high_resolution_clock::now() - t1).count() < seconds); + } + + void QuickSleep(float milliseconds) + { + const std::chrono::high_resolution_clock::time_point t1 = std::chrono::high_resolution_clock::now(); + const double seconds = double(milliseconds) / 1000.0; + const int sleep_millisec_accuracy = 1; + const double sleep_sec_accuracy = double(sleep_millisec_accuracy) / 1000.0; + while (std::chrono::duration_cast>(std::chrono::high_resolution_clock::now() - t1).count() < seconds) { - std::chrono::high_resolution_clock::time_point t2 = std::chrono::high_resolution_clock::now(); - std::chrono::duration time_span = std::chrono::duration_cast>(t2 - t1); - ms = time_span.count(); + if (seconds - (std::chrono::high_resolution_clock::now() - t1).count() > sleep_sec_accuracy) + { + std::this_thread::sleep_for(std::chrono::milliseconds(sleep_millisec_accuracy)); + } } } @@ -1251,6 +1260,7 @@ namespace wi::helper { #ifdef PLATFORM_UWP winrt::Windows::System::Launcher::LaunchUriAsync(winrt::Windows::Foundation::Uri(winrt::to_hstring(url))); + return; #endif // PLATFORM_UWP #ifdef PLATFORM_WINDOWS_DESKTOP diff --git a/WickedEngine/wiHelper.h b/WickedEngine/wiHelper.h index 813502339..3b54938f8 100644 --- a/WickedEngine/wiHelper.h +++ b/WickedEngine/wiHelper.h @@ -132,6 +132,11 @@ namespace wi::helper // Spins for the given time and does nothing (OS can not overtake) void Spin(float milliseconds); + // Sleeps if duration is long enough to wake up in time, Spins otherwise + // This lets OS overtake thread if duration is long enough to remain accurate + // Also spins for more accuracy if needed + void QuickSleep(float milliseconds); + // Opens URL in the default browser void OpenUrl(const std::string& url); }; diff --git a/WickedEngine/wiVersion.cpp b/WickedEngine/wiVersion.cpp index f695fbd77..2c96cadb4 100644 --- a/WickedEngine/wiVersion.cpp +++ b/WickedEngine/wiVersion.cpp @@ -9,7 +9,7 @@ namespace wi::version // minor features, major updates, breaking compatibility changes const int minor = 71; // minor bug fixes, alterations, refactors, updates - const int revision = 149; + const int revision = 150; const std::string version_string = std::to_string(major) + "." + std::to_string(minor) + "." + std::to_string(revision);