improved framerate locking
This commit is contained in:
@@ -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();
|
||||
|
||||
|
||||
@@ -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::duration<double>>(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::duration<double>>(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<double> time_span = std::chrono::duration_cast<std::chrono::duration<double>>(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
|
||||
|
||||
@@ -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);
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user