diff options
author | Patric Stout <truebrain@openttd.org> | 2021-02-17 14:49:45 +0100 |
---|---|---|
committer | Patric Stout <github@truebrain.nl> | 2021-02-19 10:43:15 +0100 |
commit | 5bfa0145057f2b67ed05d491abea82df4cc10d6b (patch) | |
tree | 21c9cf95878fae10f244834fc054e11f6784f94f /src | |
parent | 25f6851ca184346297c1d953e9e649311a0dc083 (diff) | |
download | openttd-5bfa0145057f2b67ed05d491abea82df4cc10d6b.tar.xz |
Codechange: track _realtime_tick more accurate
_realtime_tick was reset every time the diff was calculated. This
means if it would trigger, say, every N.9 milliseconds, it would
after two iterations already drift a millisecond. This adds up
pretty quick.
Diffstat (limited to 'src')
-rw-r--r-- | src/video/allegro_v.cpp | 12 | ||||
-rw-r--r-- | src/video/cocoa/cocoa_v.mm | 12 | ||||
-rw-r--r-- | src/video/dedicated_v.cpp | 11 | ||||
-rw-r--r-- | src/video/sdl2_v.cpp | 12 | ||||
-rw-r--r-- | src/video/sdl2_v.h | 2 | ||||
-rw-r--r-- | src/video/sdl_v.cpp | 12 | ||||
-rw-r--r-- | src/video/win32_v.cpp | 12 |
7 files changed, 55 insertions, 18 deletions
diff --git a/src/video/allegro_v.cpp b/src/video/allegro_v.cpp index 20f0e65ed..493e5a81d 100644 --- a/src/video/allegro_v.cpp +++ b/src/video/allegro_v.cpp @@ -448,7 +448,7 @@ void VideoDriver_Allegro::Stop() void VideoDriver_Allegro::MainLoop() { auto cur_ticks = std::chrono::steady_clock::now(); - auto last_cur_ticks = cur_ticks; + auto last_realtime_tick = cur_ticks; auto next_tick = cur_ticks; CheckPaletteAnim(); @@ -473,9 +473,15 @@ void VideoDriver_Allegro::MainLoop() } cur_ticks = std::chrono::steady_clock::now(); + + /* If more than a millisecond has passed, increase the _realtime_tick. */ + if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) { + auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick); + _realtime_tick += delta.count(); + last_realtime_tick += delta; + } + 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 + std::chrono::milliseconds(MILLISECONDS_PER_TICK); bool old_ctrl_pressed = _ctrl_pressed; diff --git a/src/video/cocoa/cocoa_v.mm b/src/video/cocoa/cocoa_v.mm index 122a38be5..c9e6768a8 100644 --- a/src/video/cocoa/cocoa_v.mm +++ b/src/video/cocoa/cocoa_v.mm @@ -635,7 +635,7 @@ bool VideoDriver_Cocoa::PollEvent() void VideoDriver_Cocoa::GameLoop() { auto cur_ticks = std::chrono::steady_clock::now(); - auto last_cur_ticks = cur_ticks; + auto last_realtime_tick = cur_ticks; auto next_tick = cur_ticks; for (;;) { @@ -664,9 +664,15 @@ void VideoDriver_Cocoa::GameLoop() } cur_ticks = std::chrono::steady_clock::now(); + + /* If more than a millisecond has passed, increase the _realtime_tick. */ + if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) { + auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick); + _realtime_tick += delta.count(); + last_realtime_tick += delta; + } + 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 + std::chrono::milliseconds(MILLISECONDS_PER_TICK); bool old_ctrl_pressed = _ctrl_pressed; diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp index 60459099b..06b359880 100644 --- a/src/video/dedicated_v.cpp +++ b/src/video/dedicated_v.cpp @@ -236,7 +236,7 @@ static void DedicatedHandleKeyInput() void VideoDriver_Dedicated::MainLoop() { auto cur_ticks = std::chrono::steady_clock::now(); - auto last_cur_ticks = cur_ticks; + auto last_realtime_tick = cur_ticks; auto next_tick = cur_ticks; /* Signal handlers */ @@ -283,7 +283,14 @@ void VideoDriver_Dedicated::MainLoop() if (!_dedicated_forks) DedicatedHandleKeyInput(); cur_ticks = std::chrono::steady_clock::now(); - _realtime_tick += std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_cur_ticks).count(); + + /* If more than a millisecond has passed, increase the _realtime_tick. */ + if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) { + auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick); + _realtime_tick += delta.count(); + last_realtime_tick += delta; + } + if (cur_ticks >= next_tick || _ddc_fastforward) { next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK); diff --git a/src/video/sdl2_v.cpp b/src/video/sdl2_v.cpp index 0b2e257bd..6db220470 100644 --- a/src/video/sdl2_v.cpp +++ b/src/video/sdl2_v.cpp @@ -768,9 +768,15 @@ void VideoDriver_SDL::LoopOnce() } cur_ticks = std::chrono::steady_clock::now(); + + /* If more than a millisecond has passed, increase the _realtime_tick. */ + if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) { + auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick); + _realtime_tick += delta.count(); + last_realtime_tick += delta; + } + 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 + std::chrono::milliseconds(MILLISECONDS_PER_TICK); bool old_ctrl_pressed = _ctrl_pressed; @@ -826,7 +832,7 @@ void VideoDriver_SDL::LoopOnce() void VideoDriver_SDL::MainLoop() { cur_ticks = std::chrono::steady_clock::now(); - last_cur_ticks = cur_ticks; + last_realtime_tick = cur_ticks; next_tick = cur_ticks; this->CheckPaletteAnim(); diff --git a/src/video/sdl2_v.h b/src/video/sdl2_v.h index 61d607d56..6b5d3dd14 100644 --- a/src/video/sdl2_v.h +++ b/src/video/sdl2_v.h @@ -63,7 +63,7 @@ private: bool edit_box_focused; std::chrono::steady_clock::time_point cur_ticks; - std::chrono::steady_clock::time_point last_cur_ticks; + std::chrono::steady_clock::time_point last_realtime_tick; std::chrono::steady_clock::time_point next_tick; int startup_display; diff --git a/src/video/sdl_v.cpp b/src/video/sdl_v.cpp index cc6555461..fa0294200 100644 --- a/src/video/sdl_v.cpp +++ b/src/video/sdl_v.cpp @@ -649,7 +649,7 @@ void VideoDriver_SDL::Stop() void VideoDriver_SDL::MainLoop() { auto cur_ticks = std::chrono::steady_clock::now(); - auto last_cur_ticks = cur_ticks; + auto last_realtime_tick = cur_ticks; auto next_tick = cur_ticks; uint32 mod; int numkeys; @@ -719,9 +719,15 @@ void VideoDriver_SDL::MainLoop() } cur_ticks = std::chrono::steady_clock::now(); + + /* If more than a millisecond has passed, increase the _realtime_tick. */ + if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) { + auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick); + _realtime_tick += delta.count(); + last_realtime_tick += delta; + } + 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 + std::chrono::milliseconds(MILLISECONDS_PER_TICK); bool old_ctrl_pressed = _ctrl_pressed; diff --git a/src/video/win32_v.cpp b/src/video/win32_v.cpp index 714e50942..57f146e5e 100644 --- a/src/video/win32_v.cpp +++ b/src/video/win32_v.cpp @@ -1134,7 +1134,7 @@ void VideoDriver_Win32::MainLoop() { MSG mesg; auto cur_ticks = std::chrono::steady_clock::now(); - auto last_cur_ticks = cur_ticks; + auto last_realtime_tick = cur_ticks; auto next_tick = cur_ticks; std::thread draw_thread; @@ -1197,9 +1197,15 @@ void VideoDriver_Win32::MainLoop() } cur_ticks = std::chrono::steady_clock::now(); + + /* If more than a millisecond has passed, increase the _realtime_tick. */ + if (cur_ticks - last_realtime_tick > std::chrono::milliseconds(1)) { + auto delta = std::chrono::duration_cast<std::chrono::milliseconds>(cur_ticks - last_realtime_tick); + _realtime_tick += delta.count(); + last_realtime_tick += delta; + } + 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 + std::chrono::milliseconds(MILLISECONDS_PER_TICK); bool old_ctrl_pressed = _ctrl_pressed; |