10 #include "../stdafx.h"
11 #include "../core/random_func.hpp"
12 #include "../network/network.h"
13 #include "../blitter/factory.hpp"
15 #include "../driver.h"
16 #include "../fontcache.h"
17 #include "../gfx_func.h"
18 #include "../gfxinit.h"
19 #include "../progress.h"
21 #include "../thread.h"
22 #include "../window_func.h"
28 void VideoDriver::GameLoop()
30 this->next_game_tick += this->GetGameInterval();
33 auto now = std::chrono::steady_clock::now();
34 if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
37 std::lock_guard<std::mutex>
lock(this->game_state_mutex);
43 void VideoDriver::GameThread()
48 auto now = std::chrono::steady_clock::now();
49 if (this->next_game_tick > now) {
50 std::this_thread::sleep_for(this->next_game_tick - now);
57 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
69 if (std::this_thread::get_id() != this->game_thread.get_id())
return;
71 this->game_state_mutex.unlock();
75 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
78 this->game_state_mutex.lock();
88 if (this->is_game_threaded) {
89 this->is_game_threaded =
StartNewThread(&this->game_thread,
"ottd:game", &VideoDriver::GameThreadThunk,
this);
92 Debug(driver, 1,
"using {}thread for game-loop", this->is_game_threaded ?
"" :
"no ");
97 if (!this->is_game_threaded)
return;
99 this->game_thread.join();
104 if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
111 this->next_draw_tick = this->next_game_tick;
115 auto now = std::chrono::steady_clock::now();
116 if (this->
HasGUI() && now >= this->next_draw_tick) {
117 this->next_draw_tick += this->GetDrawInterval();
119 if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
126 std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
127 std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
140 ChangeGameSpeed(
true);
143 ChangeGameSpeed(
false);
163 static bool first_draw_tick =
true;
164 if (first_draw_tick) {
165 first_draw_tick =
false;
173 auto next_tick = this->next_draw_tick;
174 auto now = std::chrono::steady_clock::now();
176 if (!this->is_game_threaded) {
177 next_tick = min(next_tick, this->next_game_tick);
180 if (next_tick > now) {
181 std::this_thread::sleep_for(next_tick - now);
191 return fmt::format(
"OpenTTD {}", _openttd_revision);
static void MarkVideoDriverOperational()
Mark the current video driver as operational.
The base of all video drivers.
virtual void InputLoop()
Handle input logic, is CTRL pressed, should we fast-forward, etc.
virtual void PopulateSystemSprites()
Populate all sprites in cache.
bool fast_forward_key_pressed
The fast-forward key is being pressed.
void Tick()
Give the video-driver a tick.
void SleepTillNextTick()
Sleep till the next tick is about to happen.
void StartGameThread()
Start the loop for game-tick.
virtual bool HasGUI() const
Whether the driver has a graphical user interface with the end user.
static std::string GetCaption()
Get the caption to use for the game's title bar.
void StopGameThread()
Stop the loop for the game-tick.
virtual bool PollEvent()
Process a single system event.
virtual void Paint()
Paint the window.
virtual void UnlockVideoBuffer()
Unlock a previously locked video buffer.
bool fast_forward_via_key
The fast-forward was enabled by key press.
virtual void CheckPaletteAnim()
Process any pending palette animation.
virtual bool LockVideoBuffer()
Make sure the video buffer is ready for drawing.
void GameLoopPause()
Pause the game-loop for a bit, releasing the game-state lock.
void DrainCommandQueue()
Execute all queued commands.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
SwitchMode _switch_mode
The next mainloop command.
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
bool _networking
are we in networking mode?
bool HasModalProgress()
Check if we are currently in a modal progress state.
bool StartNewThread(std::thread *thr, const char *name, TFn &&_Fx, TArgs &&... _Ax)
Start a new thread.
bool _video_vsync
Whether we should use vsync (only if active video driver supports HW acceleration).
bool _video_hw_accel
Whether to consider hardware accelerated video drivers on startup.
Base of all video drivers.
std::mutex lock
synchronization for playback status fields