Go to the documentation of this file.
10 #include "../stdafx.h"
11 #include "../core/random_func.hpp"
12 #include "../network/network.h"
13 #include "../blitter/factory.hpp"
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"
26 void VideoDriver::GameLoop()
28 this->next_game_tick += this->GetGameInterval();
31 auto now = std::chrono::steady_clock::now();
32 if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
35 std::lock_guard<std::mutex>
lock(this->game_state_mutex);
41 void VideoDriver::GameThread()
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);
55 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
67 if (std::this_thread::get_id() != this->game_thread.get_id())
return;
69 this->game_state_mutex.unlock();
73 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
76 this->game_state_mutex.lock();
86 if (this->is_game_threaded) {
87 this->is_game_threaded =
StartNewThread(&this->game_thread,
"ottd:game", &VideoDriver::GameThreadThunk,
this);
90 DEBUG(driver, 1,
"using %sthread for game-loop", this->is_game_threaded ?
"" :
"no ");
95 if (!this->is_game_threaded)
return;
97 this->game_thread.join();
100 void VideoDriver::RealChangeBlitter(
const char *repl_blitter)
104 DEBUG(driver, 1,
"Switching blitter from '%s' to '%s'... ", cur_blitter, repl_blitter);
106 if (new_blitter ==
nullptr) NOT_REACHED();
107 DEBUG(driver, 1,
"Successfully switched to %s.", repl_blitter);
123 if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
130 this->next_draw_tick = this->next_game_tick;
134 auto now = std::chrono::steady_clock::now();
135 if (this->
HasGUI() && now >= this->next_draw_tick) {
136 this->next_draw_tick += this->GetDrawInterval();
138 if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
148 ChangeGameSpeed(
true);
151 ChangeGameSpeed(
false);
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);
187 auto next_tick = this->next_draw_tick;
188 auto now = std::chrono::steady_clock::now();
190 if (!this->is_game_threaded) {
191 next_tick = min(next_tick, this->next_game_tick);
194 if (next_tick > now) {
195 std::this_thread::sleep_for(next_tick - now);
void Tick()
Give the video-driver a tick.
void ReInitAllWindows()
Re-initialize all windows.
void CDECL usererror(const char *s,...)
Error handling for fatal user errors.
The base of all video drivers.
std::mutex lock
synchronization for playback status fields
How all blitters should look like.
virtual void CheckPaletteAnim()
Process any pending palette animation.
static Blitter * SelectBlitter(const std::string &name)
Find the requested blitter and return his class.
virtual bool HasGUI() const
Whether the driver has a graphical user interface with the end user.
void StartGameThread()
Start the loop for game-tick.
virtual void ClearSystemSprites()
Clear all cached sprites.
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
virtual void InputLoop()
Handle input logic, is CTRL pressed, should we fast-forward, etc.
virtual void Paint()
Paint the window.
void GfxClearSpriteCache()
Remove all encoded sprites from the sprite cache without discarding sprite location information.
#define DEBUG(name, level,...)
Output a line of debugging information.
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
static Blitter * GetCurrentBlitter()
Get the current active blitter (always set by calling SelectBlitter).
bool _networking
are we in networking mode?
virtual void UnlockVideoBuffer()
Unlock a previously locked video buffer.
void GameLoopPause()
Pause the game-loop for a bit, releasing the game-state lock.
void StopGameThread()
Stop the loop for the game-tick.
virtual const char * GetName()=0
Get the name of the blitter, the same as the Factory-instance returns.
virtual void PopulateSystemSprites()
Populate all sprites in cache.
SwitchMode _switch_mode
The next mainloop command.
bool _video_hw_accel
Whether to consider hardware accelerated video drivers.
bool fast_forward_via_key
The fast-forward was enabled by key press.
bool _video_vsync
Whether we should use vsync (only if _video_hw_accel is enabled).
void SleepTillNextTick()
Sleep till the next tick is about to happen.
bool fast_forward_key_pressed
The fast-forward key is being pressed.
const char * change_blitter
Request to change the blitter. nullptr if no pending request.
virtual bool LockVideoBuffer()
Make sure the video buffer is ready for drawing.
static bool HasModalProgress()
Check if we are currently in a modal progress state.
virtual bool PollEvent()
Process a single system event.
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.