diff options
author | Kirk Klobe <kklobe@gmail.com> | 2022-10-19 01:40:41 +0300 |
---|---|---|
committer | Kirk Klobe <kklobe@gmail.com> | 2022-11-09 00:23:40 +0300 |
commit | d040376616074c12e1db6302a6e1506681f9c451 (patch) | |
tree | 4fd8840244b864fd9b86d86d2293b16b672e6f10 | |
parent | 718db1d4a63242561fdcf8b9ed7671990ee6eb32 (diff) |
Compensate for excess sleep time in increasetickskk/increaseticks-sleep-adjust-1
-rw-r--r-- | src/dosbox.cpp | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/src/dosbox.cpp b/src/dosbox.cpp index 07f998985..723bfa91e 100644 --- a/src/dosbox.cpp +++ b/src/dosbox.cpp @@ -166,7 +166,11 @@ static Bitu Normal_Loop() { } } -void increaseticks() { //Make it return ticksRemain and set it in the function above to remove the global variable. +static auto extraSleptUs = std::chrono::microseconds(0); + +void increaseticks() +{ // Make it return ticksRemain and set it in the function above to remove the + // global variable. ZoneScoped if (GCC_UNLIKELY(ticksLocked)) { // For Fast Forward Mode ticksRemain=5; @@ -184,12 +188,25 @@ void increaseticks() { //Make it return ticksRemain and set it in the function a ticksAdded = 0; constexpr auto duration = std::chrono::milliseconds(1); + + const auto sleepStart = std::chrono::steady_clock::now(); std::this_thread::sleep_for(duration); + const auto sleepEnd = std::chrono::steady_clock::now(); + const auto sleptUs = std::chrono::duration_cast<std::chrono::microseconds>( + sleepEnd - sleepStart); + + extraSleptUs += sleptUs % duration; - const auto timeslept = GetTicksSince(ticksNew); + auto timeslept = GetTicksSince(ticksNew); + + if (extraSleptUs >= duration) { + timeslept++; + extraSleptUs -= duration; + } // Update ticksDone with the time spent sleeping ticksDone -= timeslept; + if (ticksDone < 0) ticksDone = 0; return; //0 |