summaryrefslogtreecommitdiff
path: root/src/video/win32_v.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-02-17 14:46:19 +0100
committerPatric Stout <github@truebrain.nl>2021-02-19 10:43:15 +0100
commit25f6851ca184346297c1d953e9e649311a0dc083 (patch)
tree5fa298bd14e8c92eefa69d05ec69cf37f90fbd59 /src/video/win32_v.cpp
parentd437445c67bb76713614ae5a4242168c891b9978 (diff)
downloadopenttd-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/win32_v.cpp')
-rw-r--r--src/video/win32_v.cpp16
1 files changed, 7 insertions, 9 deletions
diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp
index 8ccdd803e..714e50942 100644
--- a/src/video/win32_v.cpp
+++ b/src/video/win32_v.cpp
@@ -1133,9 +1133,9 @@ static void CheckPaletteAnim()
void VideoDriver_Win32::MainLoop()
{
MSG mesg;
- uint32 cur_ticks = GetTickCount();
- uint32 last_cur_ticks = cur_ticks;
- 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;
std::thread draw_thread;
std::unique_lock<std::recursive_mutex> draw_lock;
@@ -1176,8 +1176,6 @@ void VideoDriver_Win32::MainLoop()
CheckPaletteAnim();
for (;;) {
- uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
-
while (PeekMessage(&mesg, nullptr, 0, 0, PM_REMOVE)) {
InteractiveRandom(); // randomness
/* Convert key messages to char messages if we want text input. */
@@ -1198,11 +1196,11 @@ void VideoDriver_Win32::MainLoop()
_fast_forward = 0;
}
- cur_ticks = GetTickCount();
- if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode) || cur_ticks < prev_cur_ticks) {
- _realtime_tick += cur_ticks - last_cur_ticks;
+ cur_ticks = std::chrono::steady_clock::now();
+ if (cur_ticks >= next_tick || (_fast_forward && !_pause_mode)) {
+ _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count();
last_cur_ticks = cur_ticks;
- next_tick = cur_ticks + MILLISECONDS_PER_TICK;
+ next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
bool old_ctrl_pressed = _ctrl_pressed;