OpenTTD Source: src/video/video_driver.cpp Source File

OpenTTD Source 1.11.0-RC1

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
23 bool _video_hw_accel;
24
25 void VideoDriver::GameLoop()
26 {
27 this->next_game_tick += this->GetGameInterval();
28
29 /* Avoid next_game_tick getting behind more and more if it cannot keep up. */
30 auto now = std::chrono::steady_clock::now();
31 if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
32
33 {
34 std::lock_guard<std::mutex> lock(this->game_state_mutex);
35
36 ::GameLoop();
37 }
38 }
39
40 void VideoDriver::GameThread()
41 {
42 while (!_exit_game) {
43 this->GameLoop();
44
45 auto now = std::chrono::steady_clock::now();
46 if (this->next_game_tick > now) {
47 std::this_thread::sleep_for(this->next_game_tick - now);
48 } else {
49 /* Ensure we yield this thread if drawings wants to take a lock on
50 * the game state. This is mainly because most OSes have an
51 * optimization that if you unlock/lock a mutex in the same thread
52 * quickly, it will never context switch even if there is another
53 * thread waiting to take the lock on the same mutex. */
54 std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
55 }
56 }
57 }
58
63 void VideoDriver::GameLoopPause()
64 {
65 /* If we are not called from the game-thread, ignore this request. */
66 if (std::this_thread::get_id() != this->game_thread.get_id()) return;
67
68 this->game_state_mutex.unlock();
69
70 {
71 /* See GameThread() for more details on this lock. */
72 std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
73 }
74
75 this->game_state_mutex.lock();
76 }
77
78 /* static */ void VideoDriver::GameThreadThunk(VideoDriver *drv)
79 {
80 drv->GameThread();
81 }
82
83 void VideoDriver::StartGameThread()
84 {
85 if (this->is_game_threaded) {
86 this->is_game_threaded = StartNewThread(&this->game_thread, "ottd:game", &VideoDriver::GameThreadThunk, this);
87 }
88
89 DEBUG(driver, 1, "using %sthread for game-loop", this->is_game_threaded ? "" : "no ");
90 }
91
92 void VideoDriver::StopGameThread()
93 {
94 if (!this->is_game_threaded) return;
95
96 this->game_thread.join();
97 }
98
99 void VideoDriver::RealChangeBlitter(const char *repl_blitter)
100 {
101 const char *cur_blitter = BlitterFactory::GetCurrentBlitter()->GetName();
102
103 DEBUG(driver, 1, "Switching blitter from '%s' to '%s'... ", cur_blitter, repl_blitter);
104 Blitter *new_blitter = BlitterFactory::SelectBlitter(repl_blitter);
105 if (new_blitter == nullptr) NOT_REACHED();
106 DEBUG(driver, 1, "Successfully switched to %s.", repl_blitter);
107
108 if (!this->AfterBlitterChange()) {
109 /* Failed to switch blitter, let's hope we can return to the old one. */
110 if (BlitterFactory::SelectBlitter(cur_blitter) == nullptr || !this->AfterBlitterChange()) usererror("Failed to reinitialize video driver. Specify a fixed blitter in the config");
111 }
112
113 /* Clear caches that might have sprites for another blitter. */
114 this->ClearSystemSprites();
115 ClearFontCache();
116 GfxClearSpriteCache();
117 ReInitAllWindows();
118 }
119
120 void VideoDriver::Tick()
121 {
122 if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
123 this->GameLoop();
124
125 /* For things like dedicated server, don't run a separate draw-tick. */
126 if (!this->HasGUI()) {
127 ::InputLoop();
128 ::UpdateWindows();
129 this->next_draw_tick = this->next_game_tick;
130 }
131 }
132
133 auto now = std::chrono::steady_clock::now();
134 if (this->HasGUI() && now >= this->next_draw_tick) {
135 this->next_draw_tick += this->GetDrawInterval();
136 /* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
137 if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
138
139 /* Keep the interactive randomizer a bit more random by requesting
140 * new values when-ever we can. */
141 InteractiveRandom();
142
143 this->InputLoop();
144
145 /* Check if the fast-forward button is still pressed. */
146 if (fast_forward_key_pressed && !_networking && _game_mode != GM_MENU) {
147 ChangeGameSpeed(true);
148 this->fast_forward_via_key = true;
149 } else if (this->fast_forward_via_key) {
150 ChangeGameSpeed(false);
151 this->fast_forward_via_key = false;
152 }
153
154 {
155 /* Tell the game-thread to stop so we can have a go. */
156 std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
157 std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
158
159 this->LockVideoBuffer();
160
161 if (this->change_blitter != nullptr) {
162 this->RealChangeBlitter(this->change_blitter);
163 this->change_blitter = nullptr;
164 }
165
166 while (this->PollEvent()) {}
167 ::InputLoop();
168
169 /* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
170 if (_game_mode == GM_BOOTSTRAP || _switch_mode == SM_NONE || HasModalProgress()) {
171 ::UpdateWindows();
172 }
173
174 this->PopulateSystemSprites();
175 }
176
177 this->CheckPaletteAnim();
178 this->Paint();
179
180 this->UnlockVideoBuffer();
181 }
182 }
183
184 void VideoDriver::SleepTillNextTick()
185 {
186 auto next_tick = this->next_draw_tick;
187 auto now = std::chrono::steady_clock::now();
188
189 if (!this->is_game_threaded) {
190 next_tick = min(next_tick, this->next_game_tick);
191 }
192
193 if (next_tick > now) {
194 std::this_thread::sleep_for(next_tick - now);
195 }
196 }
VideoDriver::Tick
void Tick()
Give the video-driver a tick.
Definition: video_driver.cpp:120
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:33
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:265
BlitterFactory::SelectBlitter
static Blitter * SelectBlitter(const std::string &name)
Find the requested blitter and return his class.
Definition: factory.hpp:97
VideoDriver::HasGUI
virtual bool HasGUI() const
Whether the driver has a graphical user interface with the end user.
Definition: video_driver.hpp:110
VideoDriver::StartGameThread
void StartGameThread()
Start the loop for game-tick.
Definition: video_driver.cpp:83
VideoDriver::ClearSystemSprites
virtual void ClearSystemSprites()
Clear all cached sprites.
Definition: video_driver.hpp:100
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:242
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:260
GfxClearSpriteCache
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
Definition: spritecache.cpp:974
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:140
_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:255
VideoDriver::GameLoopPause
void GameLoopPause()
Pause the game-loop for a bit, releasing the game-state lock.
Definition: video_driver.cpp:63
VideoDriver::StopGameThread
void StopGameThread()
Stop the loop for the game-tick.
Definition: video_driver.cpp:92
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:95
_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:314
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:184
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:313
VideoDriver::change_blitter
const char * change_blitter
Request to change the blitter. nullptr if no pending request.
Definition: video_driver.hpp:328
VideoDriver::LockVideoBuffer
virtual bool LockVideoBuffer()
Make sure the video buffer is ready for drawing.
Definition: video_driver.hpp:248
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:271
VideoDriver::AfterBlitterChange
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
Definition: video_driver.hpp:73