summaryrefslogtreecommitdiff
path: root/src/video/dedicated_v.cpp
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-02-17 15:14:59 +0100
committerPatric Stout <github@truebrain.nl>2021-02-19 10:43:15 +0100
commitae7a2b9f02b4becdcc7cb96f79031c840198989f (patch)
tree34e9bf7ff63f932421a28449efe28fea95e62467 /src/video/dedicated_v.cpp
parentc81c6e5eb7a0e991039b7662868e87a8d2fe3415 (diff)
downloadopenttd-ae7a2b9f02b4becdcc7cb96f79031c840198989f.tar.xz
Change: allow video-drivers to miss deadlines slightly
Before, every next frame was calculated from the current time. If for some reason the current frame was drifting a bit, the next would too, and the next more, etc etc. This meant we rarely hit the targets we would like, like 33.33fps. Instead, allow video-drivers to drift slightly, and schedule the next frame based on the time the last should have happened. Only if the drift gets too much, that deadlines are missed for longer period of times, schedule the next frame based on the current time. This makes the FPS a lot smoother, as sleeps aren't as exact as you might think.
Diffstat (limited to 'src/video/dedicated_v.cpp')
-rw-r--r--src/video/dedicated_v.cpp12
1 files changed, 9 insertions, 3 deletions
diff --git a/src/video/dedicated_v.cpp b/src/video/dedicated_v.cpp
index 057c0daa8..7b1abbadd 100644
--- a/src/video/dedicated_v.cpp
+++ b/src/video/dedicated_v.cpp
@@ -238,7 +238,7 @@ void VideoDriver_Dedicated::MainLoop()
{
auto cur_ticks = std::chrono::steady_clock::now();
auto last_realtime_tick = cur_ticks;
- auto next_tick = cur_ticks;
+ auto next_game_tick = cur_ticks;
/* Signal handlers */
#if defined(UNIX)
@@ -292,8 +292,14 @@ void VideoDriver_Dedicated::MainLoop()
last_realtime_tick += delta;
}
- if (cur_ticks >= next_tick || _ddc_fastforward) {
- next_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
+ if (cur_ticks >= next_game_tick || _ddc_fastforward) {
+ if (_ddc_fastforward) {
+ next_game_tick = cur_ticks + std::chrono::milliseconds(MILLISECONDS_PER_TICK);
+ } else {
+ next_game_tick += std::chrono::milliseconds(MILLISECONDS_PER_TICK);
+ /* Avoid next_game_tick getting behind more and more if it cannot keep up. */
+ if (next_game_tick < cur_ticks - std::chrono::milliseconds(ALLOWED_DRIFT * MILLISECONDS_PER_TICK)) next_game_tick = cur_ticks;
+ }
GameLoop();
InputLoop();