diff options
author | Patric Stout <truebrain@openttd.org> | 2021-02-17 14:46:19 +0100 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-02-19 10:43:15 +0100 |
commit | 25f6851ca184346297c1d953e9e649311a0dc083 (patch) | |
tree | 5fa298bd14e8c92eefa69d05ec69cf37f90fbd59 /src/video/dedicated_v.cpp | |
parent | d437445c67bb76713614ae5a4242168c891b9978 (diff) | |
download | openttd-25f6851ca184346297c1d953e9e649311a0dc083.tar.xz |
Codechange: switch all video drivers to std::chrono for keeping time
On all OSes we tested the std::chrono::steady_clock is of a high
enough resolution to do millisecond measurements, which is all we
need.
By accident, this fixes a Win32 driver bug, where we would never
hit our targets, as the resolution of the clock was too low to
do accurate millisecond measurements with (it was ~16ms resolution
instead).
Diffstat (limited to 'src/video/dedicated_v.cpp')
-rw-r--r-- | src/video/dedicated_v.cpp | 27 |
1 files changed, 7 insertions, 20 deletions
diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index d237e2d9e..60459099b 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -195,14 +195,6 @@ static bool InputWaiting() return select(STDIN + 1, &readfds, nullptr, nullptr, &tv) > 0; } -static uint32 GetTime() -{ - struct timeval tim; - - gettimeofday(&tim, nullptr); - return tim.tv_usec / 1000 + tim.tv_sec * 1000; -} - #else static bool InputWaiting() @@ -210,11 +202,6 @@ static bool InputWaiting() return WaitForSingleObject(_hInputReady, 1) == WAIT_OBJECT_0; } -static uint32 GetTime() -{ - return GetTickCount(); -} - #endif static void DedicatedHandleKeyInput() @@ -248,8 +235,9 @@ static void DedicatedHandleKeyInput() void VideoDriver_Dedicated::MainLoop() { - uint32 cur_ticks = GetTime(); - uint32 next_tick = cur_ticks + MILLISECONDS_PER_TICK; + auto cur_ticks = std::chrono::steady_clock::now(); + auto last_cur_ticks = cur_ticks; + auto next_tick = cur_ticks; /* Signal handlers */ #if defined(UNIX) @@ -290,15 +278,14 @@ void VideoDriver_Dedicated::MainLoop() } while (!_exit_game) { - uint32 prev_cur_ticks = cur_ticks; // to check for wrapping InteractiveRandom(); // randomness if (!_dedicated_forks) DedicatedHandleKeyInput(); - cur_ticks = GetTime(); - _realtime_tick += cur_ticks - prev_cur_ticks; - if (cur_ticks >= next_tick || cur_ticks < prev_cur_ticks || _ddc_fastforward) { - next_tick = cur_ticks + MILLISECONDS_PER_TICK; + cur_ticks = std::chrono::steady_clock::now(); + _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count(); + if (cur_ticks >= next_tick || _ddc_fastforward) { + next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK); GameLoop(); UpdateWindows(); |