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 {}thread for game-loop", this->is_game_threaded ?
"" :
"no ");
95 if (!this->is_game_threaded)
return;
97 this->game_thread.join();
102 if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
109 this->next_draw_tick = this->next_game_tick;
113 auto now = std::chrono::steady_clock::now();
114 if (this->
HasGUI() && now >= this->next_draw_tick) {
115 this->next_draw_tick += this->GetDrawInterval();
117 if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
124 std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
125 std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
138 ChangeGameSpeed(
true);
141 ChangeGameSpeed(
false);
164 auto next_tick = this->next_draw_tick;
165 auto now = std::chrono::steady_clock::now();
167 if (!this->is_game_threaded) {
168 next_tick = min(next_tick, this->next_game_tick);
171 if (next_tick > now) {
172 std::this_thread::sleep_for(next_tick - now);