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"
20 #include "../thread.h"
21 #include "../window_func.h"
27 void VideoDriver::GameLoop()
29 this->next_game_tick += this->GetGameInterval();
32 auto now = std::chrono::steady_clock::now();
33 if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
36 std::lock_guard<std::mutex>
lock(this->game_state_mutex);
42 void VideoDriver::GameThread()
47 auto now = std::chrono::steady_clock::now();
48 if (this->next_game_tick > now) {
49 std::this_thread::sleep_for(this->next_game_tick - now);
56 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
68 if (std::this_thread::get_id() != this->game_thread.get_id())
return;
70 this->game_state_mutex.unlock();
74 std::lock_guard<std::mutex>
lock(this->game_thread_wait_mutex);
77 this->game_state_mutex.lock();
87 if (this->is_game_threaded) {
88 this->is_game_threaded =
StartNewThread(&this->game_thread,
"ottd:game", &VideoDriver::GameThreadThunk,
this);
91 Debug(driver, 1,
"using {}thread for game-loop", this->is_game_threaded ?
"" :
"no ");
96 if (!this->is_game_threaded)
return;
98 this->game_thread.join();
103 if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
110 this->next_draw_tick = this->next_game_tick;
114 auto now = std::chrono::steady_clock::now();
115 if (this->
HasGUI() && now >= this->next_draw_tick) {
116 this->next_draw_tick += this->GetDrawInterval();
118 if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
125 std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
126 std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
139 ChangeGameSpeed(
true);
142 ChangeGameSpeed(
false);
162 static bool first_draw_tick =
true;
163 if (first_draw_tick) {
164 first_draw_tick =
false;
172 auto next_tick = this->next_draw_tick;
173 auto now = std::chrono::steady_clock::now();
175 if (!this->is_game_threaded) {
176 next_tick = min(next_tick, this->next_game_tick);
179 if (next_tick > now) {
180 std::this_thread::sleep_for(next_tick - now);