Files
@ r28520:f9aebe299cae
Branch filter:
Location: cpp/openttd-patchpack/source/src/video/video_driver.cpp - annotation
r28520:f9aebe299cae
5.7 KiB
text/x-c
Codechange: MacOS already has MIN/MAX macros defined
This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
This is caused because we use PreCompile Headers, and one of them
includes a system headers which defines MIN/MAX.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24940:46781aedfe1c r24970:1ff44d19fa4a r25015:8deed239577a r25014:e1f1bf3a062e r27498:6d2aaf65c3e6 r25015:8deed239577a r24855:196c88971d0d r25015:8deed239577a r24855:196c88971d0d r27544:ff436bece3b8 r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r25003:fdd8312ceea6 r25161:560705f96b3e r25003:fdd8312ceea6 r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25029:37f02ef4446d r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25655:1030dcb7eb52 r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25011:61d28a13bb41 r24855:196c88971d0d r25014:e1f1bf3a062e r25014:e1f1bf3a062e r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r25014:e1f1bf3a062e r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r25014:e1f1bf3a062e r25014:e1f1bf3a062e r24855:196c88971d0d r24855:196c88971d0d r25014:e1f1bf3a062e r24855:196c88971d0d r25368:ce512f9277c7 r25368:ce512f9277c7 r25368:ce512f9277c7 r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25363:aeaaaf1c6dce r25015:8deed239577a r25014:e1f1bf3a062e r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25712:79f3a6be4b7c r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25011:61d28a13bb41 r24855:196c88971d0d r25011:61d28a13bb41 r25014:e1f1bf3a062e r25014:e1f1bf3a062e r27498:6d2aaf65c3e6 r27498:6d2aaf65c3e6 r27498:6d2aaf65c3e6 r27498:6d2aaf65c3e6 r27498:6d2aaf65c3e6 r27498:6d2aaf65c3e6 r27498:6d2aaf65c3e6 r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r24855:196c88971d0d r25014:e1f1bf3a062e r24970:1ff44d19fa4a r24855:196c88971d0d r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r25014:e1f1bf3a062e r24970:1ff44d19fa4a r24970:1ff44d19fa4a r24855:196c88971d0d r24855:196c88971d0d r27544:ff436bece3b8 r27544:ff436bece3b8 r27544:ff436bece3b8 r27544:ff436bece3b8 r27544:ff436bece3b8 r27544:ff436bece3b8 r27544:ff436bece3b8 r27544:ff436bece3b8 r27544:ff436bece3b8 | /*
* This file is part of OpenTTD.
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file video_driver.cpp Common code between video driver implementations. */
#include "../stdafx.h"
#include "../core/random_func.hpp"
#include "../network/network.h"
#include "../blitter/factory.hpp"
#include "../debug.h"
#include "../driver.h"
#include "../fontcache.h"
#include "../gfx_func.h"
#include "../gfxinit.h"
#include "../progress.h"
#include "../rev.h"
#include "../thread.h"
#include "../window_func.h"
#include "video_driver.hpp"
bool _video_hw_accel; ///< Whether to consider hardware accelerated video drivers.
bool _video_vsync; ///< Whether we should use vsync (only if _video_hw_accel is enabled).
void VideoDriver::GameLoop()
{
this->next_game_tick += this->GetGameInterval();
/* Avoid next_game_tick getting behind more and more if it cannot keep up. */
auto now = std::chrono::steady_clock::now();
if (this->next_game_tick < now - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = now;
{
std::lock_guard<std::mutex> lock(this->game_state_mutex);
::GameLoop();
}
}
void VideoDriver::GameThread()
{
while (!_exit_game) {
this->GameLoop();
auto now = std::chrono::steady_clock::now();
if (this->next_game_tick > now) {
std::this_thread::sleep_for(this->next_game_tick - now);
} else {
/* Ensure we yield this thread if drawings wants to take a lock on
* the game state. This is mainly because most OSes have an
* optimization that if you unlock/lock a mutex in the same thread
* quickly, it will never context switch even if there is another
* thread waiting to take the lock on the same mutex. */
std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
}
}
}
/**
* Pause the game-loop for a bit, releasing the game-state lock. This allows,
* if the draw-tick requested this, the drawing to happen.
*/
void VideoDriver::GameLoopPause()
{
/* If we are not called from the game-thread, ignore this request. */
if (std::this_thread::get_id() != this->game_thread.get_id()) return;
this->game_state_mutex.unlock();
{
/* See GameThread() for more details on this lock. */
std::lock_guard<std::mutex> lock(this->game_thread_wait_mutex);
}
this->game_state_mutex.lock();
}
/* static */ void VideoDriver::GameThreadThunk(VideoDriver *drv)
{
drv->GameThread();
}
void VideoDriver::StartGameThread()
{
if (this->is_game_threaded) {
this->is_game_threaded = StartNewThread(&this->game_thread, "ottd:game", &VideoDriver::GameThreadThunk, this);
}
Debug(driver, 1, "using {}thread for game-loop", this->is_game_threaded ? "" : "no ");
}
void VideoDriver::StopGameThread()
{
if (!this->is_game_threaded) return;
this->game_thread.join();
}
void VideoDriver::Tick()
{
if (!this->is_game_threaded && std::chrono::steady_clock::now() >= this->next_game_tick) {
this->GameLoop();
/* For things like dedicated server, don't run a separate draw-tick. */
if (!this->HasGUI()) {
::InputLoop();
::UpdateWindows();
this->next_draw_tick = this->next_game_tick;
}
}
auto now = std::chrono::steady_clock::now();
if (this->HasGUI() && now >= this->next_draw_tick) {
this->next_draw_tick += this->GetDrawInterval();
/* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
if (this->next_draw_tick < now - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = now;
/* Locking video buffer can block (especially with vsync enabled), do it before taking game state lock. */
this->LockVideoBuffer();
{
/* Tell the game-thread to stop so we can have a go. */
std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
std::lock_guard<std::mutex> lock_state(this->game_state_mutex);
/* Keep the interactive randomizer a bit more random by requesting
* new values when-ever we can. */
InteractiveRandom();
this->DrainCommandQueue();
while (this->PollEvent()) {}
this->InputLoop();
/* Check if the fast-forward button is still pressed. */
if (fast_forward_key_pressed && !_networking && _game_mode != GM_MENU) {
ChangeGameSpeed(true);
this->fast_forward_via_key = true;
} else if (this->fast_forward_via_key) {
ChangeGameSpeed(false);
this->fast_forward_via_key = false;
}
::InputLoop();
/* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
if (_game_mode == GM_BOOTSTRAP || _switch_mode == SM_NONE || HasModalProgress()) {
::UpdateWindows();
}
this->PopulateSystemSprites();
}
this->CheckPaletteAnim();
this->Paint();
this->UnlockVideoBuffer();
/* Wait till the first successful drawing tick before marking the driver as operational. */
static bool first_draw_tick = true;
if (first_draw_tick) {
first_draw_tick = false;
DriverFactoryBase::MarkVideoDriverOperational();
}
}
}
void VideoDriver::SleepTillNextTick()
{
auto next_tick = this->next_draw_tick;
auto now = std::chrono::steady_clock::now();
if (!this->is_game_threaded) {
next_tick = min(next_tick, this->next_game_tick);
}
if (next_tick > now) {
std::this_thread::sleep_for(next_tick - now);
}
}
/**
* Get the caption to use for the game's title bar.
* @return The caption.
*/
/* static */ std::string VideoDriver::GetCaption()
{
return fmt::format("OpenTTD {}", _openttd_revision);
}
|