OpenTTD Source  13.2.1
video_driver.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../core/random_func.hpp"
12 #include "../network/network.h"
13 #include "../blitter/factory.hpp"
14 #include "../debug.h"
15 #include "../driver.h"
16 #include "../fontcache.h"
17 #include "../gfx_func.h"
18 #include "../gfxinit.h"
19 #include "../progress.h"
20 #include "../thread.h"
21 #include "../window_func.h"
22 #include "video_driver.hpp"
23 
26 
27 void VideoDriver::GameLoop()
28 {
29  this->next_game_tick += this->GetGameInterval();
30 
31  /* Avoid next_game_tick getting behind more and more if it cannot keep up. */
32  auto now = std::chrono::steady_clock::now();
33  if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
34 
35  {
36  std::lock_guard<std::mutex> lock(this->game_state_mutex);
37 
38  ::GameLoop();
39  }
40 }
41 
42 void VideoDriver::GameThread()
43 {
44  while (!_exit_game) {
45  this->GameLoop();
46 
47  auto now = std::chrono::steady_clock::now();
48  if (this->next_game_tick > now) {
49  std::this_thread::sleep_for(this->next_game_tick - now);
50  } else {
51  /* Ensure we yield this thread if drawings wants to take a lock on
52  * the game state. This is mainly because most OSes have an
53  * optimization that if you unlock/lock a mutex in the same thread
54  * quickly, it will never context switch even if there is another
55  * thread waiting to take the lock on the same mutex. */
56  std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
57  }
58  }
59 }
60 
66 {
67  /* If we are not called from the game-thread, ignore this request. */
68  if (std::this_thread::get_id() != this->game_thread.get_id()) return;
69 
70  this->game_state_mutex.unlock();
71 
72  {
73  /* See GameThread() for more details on this lock. */
74  std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
75  }
76 
77  this->game_state_mutex.lock();
78 }
79 
80 /* static */ void VideoDriver::GameThreadThunk(VideoDriver *drv)
81 {
82  drv->GameThread();
83 }
84 
86 {
87  if (this->is_game_threaded) {
88  this->is_game_threaded = StartNewThread(&this->game_thread, "ottd:game", &VideoDriver::GameThreadThunk, this);
89  }
90 
91  Debug(driver, 1, "using {}thread for game-loop", this->is_game_threaded ? "" : "no ");
92 }
93 
95 {
96  if (!this->is_game_threaded) return;
97 
98  this->game_thread.join();
99 }
100 
102 {
103  if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
104  this->GameLoop();
105 
106  /* For things like dedicated server, don't run a separate draw-tick. */
107  if (!this->HasGUI()) {
108  ::InputLoop();
109  ::UpdateWindows();
110  this->next_draw_tick = this->next_game_tick;
111  }
112  }
113 
114  auto now = std::chrono::steady_clock::now();
115  if (this->HasGUI() && now >= this->next_draw_tick) {
116  this->next_draw_tick += this->GetDrawInterval();
117  /* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
118  if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
119 
120  /* Locking video buffer can block (especially with vsync enabled), do it before taking game state lock. */
121  this->LockVideoBuffer();
122 
123  {
124  /* Tell the game-thread to stop so we can have a go. */
125  std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
126  std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
127 
128  /* Keep the interactive randomizer a bit more random by requesting
129  * new values when-ever we can. */
130  InteractiveRandom();
131 
132  this->DrainCommandQueue();
133 
134  while (this->PollEvent()) {}
135  this->InputLoop();
136 
137  /* Check if the fast-forward button is still pressed. */
138  if (fast_forward_key_pressed && !_networking && _game_mode != GM_MENU) {
139  ChangeGameSpeed(true);
140  this->fast_forward_via_key = true;
141  } else if (this->fast_forward_via_key) {
142  ChangeGameSpeed(false);
143  this->fast_forward_via_key = false;
144  }
145 
146  ::InputLoop();
147 
148  /* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
149  if (_game_mode == GM_BOOTSTRAP || _switch_mode == SM_NONE || HasModalProgress()) {
150  ::UpdateWindows();
151  }
152 
153  this->PopulateSystemSprites();
154  }
155 
156  this->CheckPaletteAnim();
157  this->Paint();
158 
159  this->UnlockVideoBuffer();
160 
161  /* Wait till the first successful drawing tick before marking the driver as operational. */
162  static bool first_draw_tick = true;
163  if (first_draw_tick) {
164  first_draw_tick = false;
166  }
167  }
168 }
169 
171 {
172  auto next_tick = this->next_draw_tick;
173  auto now = std::chrono::steady_clock::now();
174 
175  if (!this->is_game_threaded) {
176  next_tick = min(next_tick, this->next_game_tick);
177  }
178 
179  if (next_tick > now) {
180  std::this_thread::sleep_for(next_tick - now);
181  }
182 }
VideoDriver::Tick
void Tick()
Give the video-driver a tick.
Definition: video_driver.cpp:101
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:35
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
VideoDriver::CheckPaletteAnim
virtual void CheckPaletteAnim()
Process any pending palette animation.
Definition: video_driver.hpp:280
VideoDriver::HasGUI
virtual bool HasGUI() const
Whether the driver has a graphical user interface with the end user.
Definition: video_driver.hpp:118
VideoDriver::StartGameThread
void StartGameThread()
Start the loop for game-tick.
Definition: video_driver.cpp:85
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3075
VideoDriver::InputLoop
virtual void InputLoop()
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: video_driver.hpp:257
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:275
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:46
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:58
VideoDriver::UnlockVideoBuffer
virtual void UnlockVideoBuffer()
Unlock a previously locked video buffer.
Definition: video_driver.hpp:270
VideoDriver::GameLoopPause
void GameLoopPause()
Pause the game-loop for a bit, releasing the game-state lock.
Definition: video_driver.cpp:65
VideoDriver::StopGameThread
void StopGameThread()
Stop the loop for the game-tick.
Definition: video_driver.cpp:94
VideoDriver::PopulateSystemSprites
virtual void PopulateSystemSprites()
Populate all sprites in cache.
Definition: video_driver.hpp:103
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:49
video_driver.hpp
_video_hw_accel
bool _video_hw_accel
Whether to consider hardware accelerated video drivers.
Definition: video_driver.cpp:24
VideoDriver::DrainCommandQueue
void DrainCommandQueue()
Execute all queued commands.
Definition: video_driver.hpp:328
VideoDriver::fast_forward_via_key
bool fast_forward_via_key
The fast-forward was enabled by key press.
Definition: video_driver.hpp:349
_video_vsync
bool _video_vsync
Whether we should use vsync (only if _video_hw_accel is enabled).
Definition: video_driver.cpp:25
Debug
#define Debug(name, level, format_string,...)
Ouptut a line of debugging information.
Definition: debug.h:37
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:170
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:348
VideoDriver::LockVideoBuffer
virtual bool LockVideoBuffer()
Make sure the video buffer is ready for drawing.
Definition: video_driver.hpp:263
HasModalProgress
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:17
VideoDriver::PollEvent
virtual bool PollEvent()
Process a single system event.
Definition: video_driver.hpp:286
DriverFactoryBase::MarkVideoDriverOperational
static void MarkVideoDriverOperational()
Mark the current video driver as operational.
Definition: driver.cpp:215