30void VideoDriver::GameLoop()
32 this->next_game_tick += this->GetGameInterval();
35 auto now = std::chrono::steady_clock::now();
36 if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
39 std::lock_guard<std::mutex>
lock(this->game_state_mutex);
45void VideoDriver::GameThread()
50 auto now = std::chrono::steady_clock::now();
51 if (this->next_game_tick > now) {
52 std::this_thread::sleep_for(this->next_game_tick - now);
59 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
71 if (std::this_thread::get_id() != this->game_thread.get_id())
return;
73 this->game_state_mutex.unlock();
77 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
80 this->game_state_mutex.lock();
90 if (this->is_game_threaded) {
91 this->is_game_threaded =
StartNewThread(&this->game_thread,
"ottd:game", &VideoDriver::GameThreadThunk,
this);
94 Debug(driver, 1,
"using {}thread for game-loop", this->is_game_threaded ?
"" :
"no ");
99 if (!this->is_game_threaded)
return;
101 this->game_thread.join();
106 if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
113 this->next_draw_tick = this->next_game_tick;
117 auto now = std::chrono::steady_clock::now();
118 if (this->
HasGUI() && now >= this->next_draw_tick) {
119 this->next_draw_tick += this->GetDrawInterval();
121 if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
128 std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
129 std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
142 ChangeGameSpeed(
true);
145 ChangeGameSpeed(
false);
165 static bool first_draw_tick =
true;
166 if (first_draw_tick) {
167 first_draw_tick =
false;
175 auto next_tick = this->next_draw_tick;
176 auto now = std::chrono::steady_clock::now();
178 if (!this->is_game_threaded) {
179 next_tick = min(next_tick, this->next_game_tick);
182 if (next_tick > now) {
183 std::this_thread::sleep_for(next_tick - now);
193 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.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
Base for all drivers (video, sound, music, etc).
Factory to 'query' all available blitters.
Functions to read fonts from files and cache them.
SwitchMode _switch_mode
The next mainloop command.
Functions related to the gfx engine.
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Functions related to the graphics initialization.
bool _networking
are we in networking mode?
Basic functions/variables used all over the place.
Functions related to modal progress.
bool HasModalProgress()
Check if we are currently in a modal progress state.
Pseudo random number generator.
Declaration of OTTD revision dependent variables.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
bool StartNewThread(std::thread *thr, std::string_view 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
Window functions not directly related to making/drawing windows.