From 074a8e4895d6cbbfba5bbc5e5226304566d5ac5c Mon Sep 17 00:00:00 2001 From: "alexey.lysiuk" Date: Sat, 25 Nov 2017 11:03:37 +0200 Subject: [PATCH] Fixed issue with endless waiting for next tic https://forum.zdoom.org/viewtopic.php?t=58426&p=1028622#p1028547 --- src/i_time.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/i_time.cpp b/src/i_time.cpp index 516ecfad1e..bea0272432 100644 --- a/src/i_time.cpp +++ b/src/i_time.cpp @@ -130,11 +130,22 @@ int I_WaitForTic(int prevtic) int time; while ((time = I_GetTime()) <= prevtic) { + // Windows-specific note: // The minimum amount of time a thread can sleep is controlled by timeBeginPeriod. // We set this to 1 ms in DoMain. - uint64_t sleepTime = NSToMS(FirstFrameStartTime + TicToNS(prevtic + 1) - I_nsTime()); - if (sleepTime > 2) - std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime - 2)); + + const uint64_t next = FirstFrameStartTime + TicToNS(prevtic + 1); + const uint64_t now = I_nsTime(); + + if (next > now) + { + const uint64_t sleepTime = NSToMS(next - now); + + if (sleepTime > 2) + { + std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime - 2)); + } + } I_SetFrameTime(); }