2021-02-20 10:54:33 +00:00
|
|
|
/*
|
|
|
|
* 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"
|
2021-02-24 13:50:52 +00:00
|
|
|
#include "../core/random_func.hpp"
|
2021-02-28 14:41:03 +00:00
|
|
|
#include "../network/network.h"
|
2021-03-08 13:50:06 +00:00
|
|
|
#include "../blitter/factory.hpp"
|
2021-02-24 14:22:23 +00:00
|
|
|
#include "../debug.h"
|
2023-06-04 14:40:17 +00:00
|
|
|
#include "../driver.h"
|
2021-03-08 13:50:06 +00:00
|
|
|
#include "../fontcache.h"
|
2021-02-20 10:54:33 +00:00
|
|
|
#include "../gfx_func.h"
|
2021-03-08 13:50:06 +00:00
|
|
|
#include "../gfxinit.h"
|
2021-02-20 10:54:33 +00:00
|
|
|
#include "../progress.h"
|
|
|
|
#include "../thread.h"
|
|
|
|
#include "../window_func.h"
|
|
|
|
#include "video_driver.hpp"
|
|
|
|
|
2021-03-08 14:42:39 +00:00
|
|
|
bool _video_hw_accel; ///< Whether to consider hardware accelerated video drivers.
|
2021-04-10 12:53:26 +00:00
|
|
|
bool _video_vsync; ///< Whether we should use vsync (only if _video_hw_accel is enabled).
|
2021-03-08 14:42:39 +00:00
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
void VideoDriver::GameLoop()
|
2021-02-20 10:54:33 +00:00
|
|
|
{
|
2021-02-24 14:22:23 +00:00
|
|
|
this->next_game_tick += this->GetGameInterval();
|
2021-02-20 10:54:33 +00:00
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
/* 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;
|
2021-02-20 10:54:33 +00:00
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
{
|
2021-04-06 02:53:01 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lock(this->game_state_mutex);
|
2021-02-20 10:54:33 +00:00
|
|
|
|
|
|
|
::GameLoop();
|
2021-02-24 14:22:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-09 13:53:51 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
|
|
|
}
|
|
|
|
|
2021-04-06 02:53:01 +00:00
|
|
|
/* static */ bool VideoDriver::EmergencyAcquireGameLock(uint tries, uint delay_ms)
|
|
|
|
{
|
|
|
|
VideoDriver *drv = VideoDriver::GetInstance();
|
|
|
|
if (drv == nullptr) return true;
|
|
|
|
|
|
|
|
|
|
|
|
for (uint i = 0; i < tries; i++) {
|
|
|
|
if (drv->game_state_mutex.try_lock()) return true;
|
|
|
|
CSleep(delay_ms);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
/* static */ void VideoDriver::GameThreadThunk(VideoDriver *drv)
|
|
|
|
{
|
2021-04-06 02:53:01 +00:00
|
|
|
SetSelfAsGameThread();
|
2021-02-24 14:22:23 +00:00
|
|
|
drv->GameThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver::StartGameThread()
|
|
|
|
{
|
|
|
|
if (this->is_game_threaded) {
|
|
|
|
this->is_game_threaded = StartNewThread(&this->game_thread, "ottd:game", &VideoDriver::GameThreadThunk, this);
|
|
|
|
}
|
|
|
|
|
2023-08-28 19:27:05 +00:00
|
|
|
if (!this->is_game_threaded) SetSelfAsGameThread();
|
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
DEBUG(driver, 1, "using %sthread 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();
|
2021-02-20 10:54:33 +00:00
|
|
|
|
|
|
|
/* For things like dedicated server, don't run a separate draw-tick. */
|
|
|
|
if (!this->HasGUI()) {
|
|
|
|
::InputLoop();
|
2021-02-24 14:22:23 +00:00
|
|
|
::UpdateWindows();
|
2021-02-20 10:54:33 +00:00
|
|
|
this->next_draw_tick = this->next_game_tick;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
|
|
|
if (this->HasGUI() && now >= this->next_draw_tick) {
|
2021-05-02 18:10:07 +00:00
|
|
|
/* Locking video buffer can block (especially with vsync enabled), do it before taking game state lock. */
|
|
|
|
this->LockVideoBuffer();
|
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
{
|
|
|
|
/* Tell the game-thread to stop so we can have a go. */
|
|
|
|
std::lock_guard<std::mutex> lock_wait(this->game_thread_wait_mutex);
|
2021-04-06 02:53:01 +00:00
|
|
|
std::lock_guard<std::recursive_mutex> lock_state(this->game_state_mutex);
|
2021-02-24 14:22:23 +00:00
|
|
|
|
2021-04-05 21:56:48 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-04-05 20:30:14 +00:00
|
|
|
/* Keep the interactive randomizer a bit more random by requesting
|
|
|
|
* new values when-ever we can. */
|
|
|
|
InteractiveRandom();
|
|
|
|
|
2021-05-01 17:39:03 +00:00
|
|
|
this->DrainCommandQueue();
|
2021-03-08 13:50:06 +00:00
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
while (this->PollEvent()) {}
|
|
|
|
::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();
|
|
|
|
}
|
2021-02-20 10:54:33 +00:00
|
|
|
|
2021-04-05 19:25:45 +00:00
|
|
|
{
|
|
|
|
extern std::mutex _cur_palette_mutex;
|
|
|
|
std::lock_guard<std::mutex> lock_state(_cur_palette_mutex);
|
|
|
|
this->CheckPaletteAnim();
|
|
|
|
}
|
|
|
|
|
2021-02-24 14:04:41 +00:00
|
|
|
this->Paint();
|
2021-02-20 10:54:33 +00:00
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
this->UnlockVideoBuffer();
|
2023-06-04 14:40:17 +00:00
|
|
|
|
|
|
|
/* 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();
|
|
|
|
}
|
2021-02-20 10:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void VideoDriver::SleepTillNextTick()
|
|
|
|
{
|
2021-02-24 14:22:23 +00:00
|
|
|
auto next_tick = this->next_draw_tick;
|
2021-02-28 14:41:03 +00:00
|
|
|
auto now = std::chrono::steady_clock::now();
|
2021-02-20 10:54:33 +00:00
|
|
|
|
2021-02-24 14:22:23 +00:00
|
|
|
if (!this->is_game_threaded) {
|
|
|
|
next_tick = min(next_tick, this->next_game_tick);
|
|
|
|
}
|
|
|
|
|
2021-02-28 14:41:03 +00:00
|
|
|
if (next_tick > now) {
|
|
|
|
std::this_thread::sleep_for(next_tick - now);
|
2021-02-20 10:54:33 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-05 16:50:39 +00:00
|
|
|
|
|
|
|
void VideoDriver::InvalidateGameOptionsWindow()
|
|
|
|
{
|
|
|
|
InvalidateWindowClassesData(WC_GAME_OPTIONS, 3);
|
|
|
|
}
|