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);