OpenTTD Source  1.11.0-beta2
video_driver.hpp
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 #ifndef VIDEO_VIDEO_DRIVER_HPP
11 #define VIDEO_VIDEO_DRIVER_HPP
12 
13 #include "../driver.h"
14 #include "../core/geometry_type.hpp"
15 #include "../core/math_func.hpp"
16 #include "../gfx_func.h"
17 #include "../settings_type.h"
18 #include "../zoom_type.h"
19 #include <chrono>
20 #include <vector>
21 
22 extern std::string _ini_videodriver;
23 extern std::vector<Dimension> _resolutions;
25 extern bool _rightclick_emulate;
26 
28 class VideoDriver : public Driver {
29  const uint DEFAULT_WINDOW_WIDTH = 640u;
30  const uint DEFAULT_WINDOW_HEIGHT = 480u;
31 
32 public:
40  virtual void MakeDirty(int left, int top, int width, int height) = 0;
41 
45  virtual void MainLoop() = 0;
46 
53  virtual bool ChangeResolution(int w, int h) = 0;
54 
60  virtual bool ToggleFullscreen(bool fullscreen) = 0;
61 
67  virtual bool AfterBlitterChange()
68  {
69  return true;
70  }
71 
76  virtual void AcquireBlitterLock() { }
77 
82  virtual void ReleaseBlitterLock() { }
83 
84  virtual bool ClaimMousePointer()
85  {
86  return true;
87  }
88 
93  virtual bool UseSystemCursor()
94  {
95  return false;
96  }
97 
101  virtual void ClearSystemSprites() {}
102 
111  virtual bool HasGUI() const
112  {
113  return true;
114  }
115 
120  virtual bool HasEfficient8Bpp() const
121  {
122  return false;
123  }
124 
129  virtual bool HasAnimBuffer()
130  {
131  return false;
132  }
133 
138  virtual uint8 *GetAnimBuffer()
139  {
140  return nullptr;
141  }
142 
146  virtual void EditBoxLostFocus() {}
147 
151  virtual void EditBoxGainedFocus() {}
152 
157  {
158  float dpi_scale = this->GetDPIScale();
159 
160  if (dpi_scale >= 3.0f) return ZOOM_LVL_NORMAL;
161  if (dpi_scale >= 1.5f) return ZOOM_LVL_OUT_2X;
162  return ZOOM_LVL_OUT_4X;
163  }
164 
170  }
171 
178  {
180  }
181 
183  {
185  }
186 
187  private:
188  bool unlock;
189  };
190 
191 protected:
192  const uint ALLOWED_DRIFT = 5;
193 
198 
203  virtual float GetDPIScale() { return 1.0f; }
204 
209  {
210  if (_cur_resolution.width == 0 || _cur_resolution.height == 0) {
211  /* Auto-detect a good resolution. We aim for 75% of the screen size.
212  * Limit width times height times bytes per pixel to fit a 32 bit
213  * integer, This way all internal drawing routines work correctly. */
214  Dimension res = this->GetScreenSize();
215  _cur_resolution.width = ClampU(res.width * 3 / 4, DEFAULT_WINDOW_WIDTH, UINT16_MAX / 2);
216  _cur_resolution.height = ClampU(res.height * 3 / 4, DEFAULT_WINDOW_HEIGHT, UINT16_MAX / 2);
217  }
218  }
219 
223  virtual void InputLoop() {}
224 
229  virtual bool LockVideoBuffer() {
230  return false;
231  }
232 
236  virtual void UnlockVideoBuffer() {}
237 
241  virtual void Paint() {}
242 
246  virtual void PaintThread() {}
247 
251  virtual void CheckPaletteAnim() {}
252 
257  virtual bool PollEvent() { return false; };
258 
263  bool Tick();
264 
268  void SleepTillNextTick();
269 
270  std::chrono::steady_clock::duration GetGameInterval()
271  {
272  /* If we are paused, run on normal speed. */
273  if (_pause_mode) return std::chrono::milliseconds(MILLISECONDS_PER_TICK);
274  /* Infinite speed, as quickly as you can. */
275  if (_game_speed == 0) return std::chrono::microseconds(0);
276 
277  return std::chrono::microseconds(MILLISECONDS_PER_TICK * 1000 * 100 / _game_speed);
278  }
279 
280  std::chrono::steady_clock::duration GetDrawInterval()
281  {
282  return std::chrono::microseconds(1000000 / _settings_client.gui.refresh_rate);
283  }
284 
285  std::chrono::steady_clock::time_point next_game_tick;
286  std::chrono::steady_clock::time_point next_draw_tick;
287 
290 };
291 
292 #endif /* VIDEO_VIDEO_DRIVER_HPP */
GUISettings::refresh_rate
uint16 refresh_rate
How often we refresh the screen (time between draw-ticks).
Definition: settings_type.h:150
VideoDriver::HasAnimBuffer
virtual bool HasAnimBuffer()
Does this video driver support a separate animation buffer in addition to the colour buffer?
Definition: video_driver.hpp:129
VideoDriver::Tick
bool Tick()
Run the game for a single tick, processing boththe game-tick and draw-tick.
Definition: video_driver.cpp:20
ZOOM_LVL_OUT_2X
@ ZOOM_LVL_OUT_2X
Zoomed 2 times out.
Definition: zoom_type.h:25
DriverFactoryBase::GetActiveDriver
static Driver ** GetActiveDriver(Driver::Type type)
Get the active driver for the given type.
Definition: driver.h:86
VideoDriver
The base of all video drivers.
Definition: video_driver.hpp:28
Dimension
Dimensions (a width and height) of a rectangle in 2D.
Definition: geometry_type.hpp:27
VideoDriver::CheckPaletteAnim
virtual void CheckPaletteAnim()
Process any pending palette animation.
Definition: video_driver.hpp:251
VideoDriver::MakeDirty
virtual void MakeDirty(int left, int top, int width, int height)=0
Mark a particular area dirty.
VideoDriver::ToggleFullscreen
virtual bool ToggleFullscreen(bool fullscreen)=0
Change the full screen setting.
_cur_resolution
Dimension _cur_resolution
The current resolution.
Definition: driver.cpp:23
ClampU
static uint ClampU(const uint a, const uint min, const uint max)
Clamp an unsigned integer between an interval.
Definition: math_func.hpp:122
ZoomLevel
ZoomLevel
All zoom levels we know.
Definition: zoom_type.h:21
_settings_client
ClientSettings _settings_client
The current settings for this game.
Definition: settings.cpp:79
VideoDriver::ReleaseBlitterLock
virtual void ReleaseBlitterLock()
Release any lock(s) required to be held when changing blitters.
Definition: video_driver.hpp:82
VideoDriver::HasGUI
virtual bool HasGUI() const
Whether the driver has a graphical user interface with the end user.
Definition: video_driver.hpp:111
VideoDriver::GetSuggestedUIZoom
virtual ZoomLevel GetSuggestedUIZoom()
Get a suggested default GUI zoom taking screen DPI into account.
Definition: video_driver.hpp:156
VideoDriver::ClearSystemSprites
virtual void ClearSystemSprites()
Clear all cached sprites.
Definition: video_driver.hpp:101
VideoDriver::GetDPIScale
virtual float GetDPIScale()
Get DPI scaling factor of the screen OTTD is displayed on.
Definition: video_driver.hpp:203
VideoDriver::DEFAULT_WINDOW_HEIGHT
const uint DEFAULT_WINDOW_HEIGHT
Default window height.
Definition: video_driver.hpp:30
VideoDriver::InputLoop
virtual void InputLoop()
Handle input logic, is CTRL pressed, should we fast-forward, etc.
Definition: video_driver.hpp:223
VideoDriver::Paint
virtual void Paint()
Paint the window.
Definition: video_driver.hpp:241
_rightclick_emulate
bool _rightclick_emulate
Whether right clicking is emulated.
Definition: driver.cpp:24
VideoDriver::ALLOWED_DRIFT
const uint ALLOWED_DRIFT
How many times videodriver can miss deadlines without it being overly compensated.
Definition: video_driver.hpp:192
_pause_mode
PauseMode _pause_mode
The current pause mode.
Definition: gfx.cpp:47
VideoDriver::AcquireBlitterLock
virtual void AcquireBlitterLock()
Acquire any lock(s) required to be held when changing blitters.
Definition: video_driver.hpp:76
VideoDriver::ChangeResolution
virtual bool ChangeResolution(int w, int h)=0
Change the resolution of the window.
VideoDriver::GetInstance
static VideoDriver * GetInstance()
Get the currently active instance of the video driver.
Definition: video_driver.hpp:168
VideoDriver::UnlockVideoBuffer
virtual void UnlockVideoBuffer()
Unlock a previously locked video buffer.
Definition: video_driver.hpp:236
VideoDriver::GetAnimBuffer
virtual uint8 * GetAnimBuffer()
Get a pointer to the animation buffer of the video back-end.
Definition: video_driver.hpp:138
VideoDriver::UseSystemCursor
virtual bool UseSystemCursor()
Get whether the mouse cursor is drawn by the video driver.
Definition: video_driver.hpp:93
VideoDriver::PaintThread
virtual void PaintThread()
Thread function for threaded drawing.
Definition: video_driver.hpp:246
VideoDriver::UpdateAutoResolution
void UpdateAutoResolution()
Apply resolution auto-detection and clamp to sensible defaults.
Definition: video_driver.hpp:208
Driver::DT_VIDEO
@ DT_VIDEO
A video driver.
Definition: driver.h:44
VideoDriver::VideoBufferLocker::unlock
bool unlock
Stores if the lock did anything that has to be undone.
Definition: video_driver.hpp:188
_ini_videodriver
std::string _ini_videodriver
The video driver a stored in the configuration file.
Definition: driver.cpp:21
VideoDriver::fast_forward_via_key
bool fast_forward_via_key
The fast-forward was enabled by key press.
Definition: video_driver.hpp:289
MILLISECONDS_PER_TICK
static const uint MILLISECONDS_PER_TICK
The number of milliseconds per game tick.
Definition: gfx_type.h:310
VideoDriver::SleepTillNextTick
void SleepTillNextTick()
Sleep till the next tick is about to happen.
Definition: video_driver.cpp:75
VideoDriver::MainLoop
virtual void MainLoop()=0
Perform the actual drawing.
VideoDriver::fast_forward_key_pressed
bool fast_forward_key_pressed
The fast-forward key is being pressed.
Definition: video_driver.hpp:288
VideoDriver::HasEfficient8Bpp
virtual bool HasEfficient8Bpp() const
Has this video driver an efficient code path for palette animated 8-bpp sprites?
Definition: video_driver.hpp:120
VideoDriver::LockVideoBuffer
virtual bool LockVideoBuffer()
Make sure the video buffer is ready for drawing.
Definition: video_driver.hpp:229
ZOOM_LVL_NORMAL
@ ZOOM_LVL_NORMAL
The normal zoom level.
Definition: zoom_type.h:24
VideoDriver::EditBoxLostFocus
virtual void EditBoxLostFocus()
An edit box lost the input focus.
Definition: video_driver.hpp:146
VideoDriver::DEFAULT_WINDOW_WIDTH
const uint DEFAULT_WINDOW_WIDTH
Default window width.
Definition: video_driver.hpp:29
Driver
A driver for communicating with the user.
Definition: driver.h:23
ZOOM_LVL_OUT_4X
@ ZOOM_LVL_OUT_4X
Zoomed 4 times out.
Definition: zoom_type.h:26
_resolutions
std::vector< Dimension > _resolutions
List of resolutions.
Definition: driver.cpp:22
VideoDriver::PollEvent
virtual bool PollEvent()
Process a single system event.
Definition: video_driver.hpp:257
VideoDriver::VideoBufferLocker
Helper struct to ensure the video buffer is locked and ready for drawing.
Definition: video_driver.hpp:176
_game_speed
uint16 _game_speed
Current game-speed; 100 is 1x, 0 is infinite.
Definition: gfx.cpp:37
ClientSettings::gui
GUISettings gui
settings related to the GUI
Definition: settings_type.h:567
VideoDriver::GetScreenSize
virtual Dimension GetScreenSize() const
Get the resolution of the main screen.
Definition: video_driver.hpp:197
VideoDriver::EditBoxGainedFocus
virtual void EditBoxGainedFocus()
An edit box gained the input focus.
Definition: video_driver.hpp:151
VideoDriver::AfterBlitterChange
virtual bool AfterBlitterChange()
Callback invoked after the blitter was changed.
Definition: video_driver.hpp:67