OpenTTD Source  1.11.0-beta2
video_driver.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../debug.h"
12 #include "../core/random_func.hpp"
13 #include "../network/network.h"
14 #include "../gfx_func.h"
15 #include "../progress.h"
16 #include "../thread.h"
17 #include "../window_func.h"
18 #include "video_driver.hpp"
19 
21 {
22  auto cur_ticks = std::chrono::steady_clock::now();
23 
24  if (cur_ticks >= this->next_game_tick) {
25  this->next_game_tick += this->GetGameInterval();
26  /* Avoid next_game_tick getting behind more and more if it cannot keep up. */
27  if (this->next_game_tick < cur_ticks - ALLOWED_DRIFT * this->GetGameInterval()) this->next_game_tick = cur_ticks;
28 
29  /* The game loop is the part that can run asynchronously.
30  * The rest except sleeping can't. */
31  this->UnlockVideoBuffer();
32  ::GameLoop();
33  this->LockVideoBuffer();
34 
35  /* For things like dedicated server, don't run a separate draw-tick. */
36  if (!this->HasGUI()) {
37  ::InputLoop();
38  UpdateWindows();
39  this->next_draw_tick = this->next_game_tick;
40  }
41  }
42 
43  /* Prevent drawing when switching mode, as windows can be removed when they should still appear. */
44  if (this->HasGUI() && cur_ticks >= this->next_draw_tick && (_switch_mode == SM_NONE || HasModalProgress())) {
45  this->next_draw_tick += this->GetDrawInterval();
46  /* Avoid next_draw_tick getting behind more and more if it cannot keep up. */
47  if (this->next_draw_tick < cur_ticks - ALLOWED_DRIFT * this->GetDrawInterval()) this->next_draw_tick = cur_ticks;
48 
49  /* Keep the interactive randomizer a bit more random by requesting
50  * new values when-ever we can. */
51  InteractiveRandom();
52 
53  while (this->PollEvent()) {}
54  this->InputLoop();
55 
56  /* Check if the fast-forward button is still pressed. */
57  if (fast_forward_key_pressed && !_networking && _game_mode != GM_MENU) {
58  ChangeGameSpeed(true);
59  this->fast_forward_via_key = true;
60  } else if (this->fast_forward_via_key) {
61  ChangeGameSpeed(false);
62  this->fast_forward_via_key = false;
63  }
64 
65  ::InputLoop();
66  UpdateWindows();
67  this->CheckPaletteAnim();
68 
69  return true;
70  }
71 
72  return false;
73 }
74 
76 {
77  /* See how much time there is till we have to process the next event, and try to hit that as close as possible. */
78  auto next_tick = std::min(this->next_draw_tick, this->next_game_tick);
79  auto now = std::chrono::steady_clock::now();
80 
81  if (next_tick > now) {
82  this->UnlockVideoBuffer();
83  std::this_thread::sleep_for(next_tick - now);
84  this->LockVideoBuffer();
85  }
86 }
VideoDriver::Tick
bool Tick()
Run the game for a single tick, processing boththe game-tick and draw-tick.
Definition: video_driver.cpp:20
VideoDriver::CheckPaletteAnim
virtual void CheckPaletteAnim()
Process any pending palette animation.
Definition: video_driver.hpp:251
VideoDriver::HasGUI
virtual bool HasGUI() const
Whether the driver has a graphical user interface with the end user.
Definition: video_driver.hpp:111
UpdateWindows
void UpdateWindows()
Update the continuously changing contents of the windows, such as the viewports.
Definition: window.cpp:3140
VideoDriver::InputLoop
virtual void InputLoop()
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: video_driver.hpp:223
_networking
bool _networking
are we in networking mode?
Definition: network.cpp:52
VideoDriver::UnlockVideoBuffer
virtual void UnlockVideoBuffer()
Unlock a previously locked video buffer.
Definition: video_driver.hpp:236
_switch_mode
SwitchMode _switch_mode
The next mainloop command.
Definition: gfx.cpp:46
video_driver.hpp
VideoDriver::fast_forward_via_key
bool fast_forward_via_key
The fast-forward was enabled by key press.
Definition: video_driver.hpp:289
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:75
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:288
VideoDriver::LockVideoBuffer
virtual bool LockVideoBuffer()
Make sure the video buffer is ready for drawing.
Definition: video_driver.hpp:229
HasModalProgress
static bool HasModalProgress()
Check if we are currently in a modal progress state.
Definition: progress.h:21
VideoDriver::PollEvent
virtual bool PollEvent()
Process a single system event.
Definition: video_driver.hpp:257