OpenTTD Source  1.11.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 "../fontcache.h"
16 #include "../gfx_func.h"
17 #include "../gfxinit.h"
18 #include "../progress.h"
19 #include "../thread.h"
20 #include "../window_func.h"
21 #include "video_driver.hpp"
22 
25 
26 void VideoDriver::GameLoop()
27 {
28  this->next_game_tick += this->GetGameInterval();
29 
30  /* Avoid next_game_tick getting behind more and more if it cannot keep up. */
31  auto now = std::chrono::steady_clock::now();
32  if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
33 
34  {
35  std::lock_guard<std::mutex> lock(this->game_state_mutex);
36 
37  ::GameLoop();
38  }
39 }
40 
41 void VideoDriver::GameThread()
42 {
43  while (!_exit_game) {
44  this->GameLoop();
45 
46  auto now = std::chrono::steady_clock::now();
47  if (this->next_game_tick > now) {
48  std::this_thread::sleep_for(this->next_game_tick - now);
49  } else {
50  /* Ensure we yield this thread if drawings wants to take a lock on
51  * the game state. This is mainly because most OSes have an
52  * optimization that if you unlock/lock a mutex in the same thread
53  * quickly, it will never context switch even if there is another
54  * thread waiting to take the lock on the same mutex. */
55  std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
56  }
57  }
58 }
59 
65 {
66  /* If we are not called from the game-thread, ignore this request. */
67  if (std::this_thread::get_id() != this->game_thread.get_id()) return;
68 
69  this->game_state_mutex.unlock();
70 
71  {
72  /* See GameThread() for more details on this lock. */
73  std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
74  }
75 
76  this->game_state_mutex.lock();
77 }
78 
79 /* static */ void VideoDriver::GameThreadThunk(VideoDriver *drv)
80 {
81  drv->GameThread();
82 }
83 
85 {
86  if (this->is_game_threaded) {
87  this->is_game_threaded = StartNewThread(&this->game_thread, "ottd:game", &VideoDriver::GameThreadThunk, this);
88  }
89 
90  DEBUG(driver, 1, "using %sthread for game-loop", this->is_game_threaded ? "" : "no ");
91 }
92 
94 {
95  if (!this->is_game_threaded) return;
96 
97  this->game_thread.join();
98 }
99 
100 void VideoDriver::RealChangeBlitter(const char *repl_blitter)
101 {
102  const char *cur_blitter = BlitterFactory::GetCurrentBlitter()->GetName();
103 
104  DEBUG(driver, 1, "Switching blitter from '%s' to '%s'... ", cur_blitter, repl_blitter);
105  Blitter *new_blitter = BlitterFactory::SelectBlitter(repl_blitter);
106  if (new_blitter == nullptr) NOT_REACHED();
107  DEBUG(driver, 1, "Successfully switched to %s.", repl_blitter);
108 
109  if (!this->AfterBlitterChange()) {
110  /* Failed to switch blitter, let's hope we can return to the old one. */
111  if (BlitterFactory::SelectBlitter(cur_blitter) == nullptr || !this->AfterBlitterChange()) usererror("Failed to reinitialize video driver. Specify a fixed blitter in the config");
112  }
113 
114  /* Clear caches that might have sprites for another blitter. */
115  this->ClearSystemSprites();
116  ClearFontCache();
119 }
120 
122 {
123  if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
124  this->GameLoop();
125 
126  /* For things like dedicated server, don't run a separate draw-tick. */
127  if (!this->HasGUI()) {
128  ::InputLoop();
129  ::UpdateWindows();
130  this->next_draw_tick = this->next_game_tick;
131  }
132  }
133 
134  auto now = std::chrono::steady_clock::now();
135  if (this->HasGUI() && now >= this->next_draw_tick) {
136  this->next_draw_tick += this->GetDrawInterval();
137  /* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
138  if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
139 
140  /* Keep the interactive randomizer a bit more random by requesting
141  * new values when-ever we can. */
142  InteractiveRandom();
143 
144  this->InputLoop();
145 
146  /* Check if the fast-forward button is still pressed. */
147  if (fast_forward_key_pressed && !_networking && _game_mode != GM_MENU) {
148  ChangeGameSpeed(true);
149  this->fast_forward_via_key = true;
150  } else if (this->fast_forward_via_key) {
151  ChangeGameSpeed(false);
152  this->fast_forward_via_key = false;
153  }
154 
155  {
156  /* Tell the game-thread to stop so we can have a go. */
157  std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
158  std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
159 
160  this->LockVideoBuffer();
161 
162  if (this->change_blitter != nullptr) {
163  this->RealChangeBlitter(this->change_blitter);
164  this->change_blitter = nullptr;
165  }
166 
167  while (this->PollEvent()) {}
168  ::InputLoop();
169 
170  /* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
171  if (_game_mode == GM_BOOTSTRAP || _switch_mode == SM_NONE || HasModalProgress()) {
172  ::UpdateWindows();
173  }
174 
175  this->PopulateSystemSprites();
176  }
177 
178  this->CheckPaletteAnim();
179  this->Paint();
180 
181  this->UnlockVideoBuffer();
182  }
183 }
184 
186 {
187  auto next_tick = this->next_draw_tick;
188  auto now = std::chrono::steady_clock::now();
189 
190  if (!this->is_game_threaded) {
191  next_tick = min(next_tick, this->next_game_tick);
192  }
193 
194  if (next_tick > now) {
195  std::this_thread::sleep_for(next_tick - now);
196  }
197 }
VideoDriver::Tick
void Tick()
Give the video-driver a tick.
Definition: video_driver.cpp:121
ReInitAllWindows
void ReInitAllWindows()
Re-initialize all windows.
Definition: window.cpp:3456
usererror
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
Definition: openttd.cpp:102
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:34
lock
std::mutex lock
synchronization for playback status fields
Definition: win32_m.cpp:34
Blitter
How all blitters should look like.
Definition: base.hpp:28
VideoDriver::CheckPaletteAnim
virtual void CheckPaletteAnim()
Process any pending palette animation.
Definition: video_driver.hpp:272
BlitterFactory::SelectBlitter
static Blitter * SelectBlitter(const std::string &name)
Find the requested blitter and return his class.
Definition: factory.hpp:98
VideoDriver::HasGUI
virtual bool HasGUI() const
Whether the driver has a graphical user interface with the end user.
Definition: video_driver.hpp:117
VideoDriver::StartGameThread
void StartGameThread()
Start the loop for game-tick.
Definition: video_driver.cpp:84
VideoDriver::ClearSystemSprites
virtual void ClearSystemSprites()
Clear all cached sprites.
Definition: video_driver.hpp:107
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3140
VideoDriver::InputLoop
virtual void InputLoop()
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: video_driver.hpp:249
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:267
GfxClearSpriteCache
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
Definition: spritecache.cpp:976
DEBUG
#define DEBUG(name, level,...)
Output a line of debugging information.
Definition: debug.h:35
StartNewThread
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
Definition: thread.h:45
BlitterFactory::GetCurrentBlitter
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
Definition: factory.hpp:141
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
VideoDriver::UnlockVideoBuffer
virtual void UnlockVideoBuffer()
Unlock a previously locked video buffer.
Definition: video_driver.hpp:262
VideoDriver::GameLoopPause
void GameLoopPause()
Pause the game-loop for a bit, releasing the game-state lock.
Definition: video_driver.cpp:64
VideoDriver::StopGameThread
void StopGameThread()
Stop the loop for the game-tick.
Definition: video_driver.cpp:93
Blitter::GetName
virtual const char * GetName()=0
Get the name of the blitter, the same as the Factory-instance returns.
VideoDriver::PopulateSystemSprites
virtual void PopulateSystemSprites()
Populate all sprites in cache.
Definition: video_driver.hpp:102
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:46
video_driver.hpp
_video_hw_accel
bool _video_hw_accel
Whether to consider hardware accelerated video drivers.
Definition: video_driver.cpp:23
VideoDriver::fast_forward_via_key
bool fast_forward_via_key
The fast-forward was enabled by key press.
Definition: video_driver.hpp:321
_video_vsync
bool _video_vsync
Whether we should use vsync (only if _video_hw_accel is enabled).
Definition: video_driver.cpp:24
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:185
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:320
VideoDriver::change_blitter
const char * change_blitter
Request to change the blitter. nullptr if no pending request.
Definition: video_driver.hpp:335
VideoDriver::LockVideoBuffer
virtual bool LockVideoBuffer()
Make sure the video buffer is ready for drawing.
Definition: video_driver.hpp:255
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:278
VideoDriver::AfterBlitterChange
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
Definition: video_driver.hpp:80