From 25f6851ca184346297c1d953e9e649311a0dc083 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Wed, 17 Feb 2021 14:46:19 +0100 Subject: 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). --- src/video/sdl_v.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/video/sdl_v.cpp') diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index f67d46801..cc6555461 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -648,9 +648,9 @@ void VideoDriver_SDL::Stop() void VideoDriver_SDL::MainLoop() { - uint32 cur_ticks = SDL_GetTicks(); - 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; uint32 mod; int numkeys; Uint8 *keys; @@ -690,7 +690,6 @@ void VideoDriver_SDL::MainLoop() DEBUG(driver, 1, "SDL: using %sthreads", _draw_threaded ? "" : "no "); for (;;) { - uint32 prev_cur_ticks = cur_ticks; // to check for wrapping InteractiveRandom(); // randomness while (PollEvent() == -1) {} @@ -719,11 +718,11 @@ void VideoDriver_SDL::MainLoop() _fast_forward = 0; } - cur_ticks = SDL_GetTicks(); - 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(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; -- cgit v1.2.3-54-g00ecf