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).
This commit is contained in:
Patric Stout
2021-02-17 14:46:19 +01:00
committed by Patric Stout
parent d437445c67
commit 25f6851ca1
7 changed files with 44 additions and 101 deletions

View File

@@ -734,7 +734,6 @@ void VideoDriver_SDL::LoopOnce()
uint32 mod;
int numkeys;
const Uint8 *keys;
uint32 prev_cur_ticks = cur_ticks; // to check for wrapping
InteractiveRandom(); // randomness
while (PollEvent() == -1) {}
@@ -768,11 +767,11 @@ void VideoDriver_SDL::LoopOnce()
_fast_forward = 0;
}
cur_ticks = SDL_GetTicks();
if (SDL_TICKS_PASSED(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;
@@ -826,9 +825,9 @@ void VideoDriver_SDL::LoopOnce()
void VideoDriver_SDL::MainLoop()
{
cur_ticks = SDL_GetTicks();
cur_ticks = std::chrono::steady_clock::now();
last_cur_ticks = cur_ticks;
next_tick = cur_ticks + MILLISECONDS_PER_TICK;
next_tick = cur_ticks;
this->CheckPaletteAnim();