OpenTTD Source 20241224-master-gf74b0cf984
VideoDriver Class Referenceabstract

The base of all video drivers. More...

#include <video_driver.hpp>

Inheritance diagram for VideoDriver:
Driver VideoDriver_Allegro VideoDriver_Cocoa VideoDriver_Dedicated VideoDriver_Null VideoDriver_SDL VideoDriver_SDL_Base VideoDriver_Win32Base VideoDriver_CocoaOpenGL VideoDriver_CocoaQuartz VideoDriver_SDL_Default VideoDriver_SDL_OpenGL VideoDriver_Win32GDI

Data Structures

struct  VideoBufferLocker
 Helper struct to ensure the video buffer is locked and ready for drawing. More...
 

Public Member Functions

 VideoDriver (bool uses_hardware_acceleration=false)
 
virtual void MakeDirty (int left, int top, int width, int height)=0
 Mark a particular area dirty.
 
virtual void MainLoop ()=0
 Perform the actual drawing.
 
virtual bool ChangeResolution (int w, int h)=0
 Change the resolution of the window.
 
virtual bool ToggleFullscreen (bool fullscreen)=0
 Change the full screen setting.
 
virtual void ToggleVsync (bool vsync)
 Change the vsync setting.
 
virtual bool AfterBlitterChange ()
 Callback invoked after the blitter was changed.
 
virtual bool ClaimMousePointer ()
 
virtual bool UseSystemCursor ()
 Get whether the mouse cursor is drawn by the video driver.
 
virtual void PopulateSystemSprites ()
 Populate all sprites in cache.
 
virtual void ClearSystemSprites ()
 Clear all cached sprites.
 
virtual bool HasGUI () const
 Whether the driver has a graphical user interface with the end user.
 
virtual bool HasEfficient8Bpp () const
 Has this video driver an efficient code path for palette animated 8-bpp sprites?
 
virtual bool HasAnimBuffer ()
 Does this video driver support a separate animation buffer in addition to the colour buffer?
 
virtual uint8_t * GetAnimBuffer ()
 Get a pointer to the animation buffer of the video back-end.
 
virtual void EditBoxLostFocus ()
 An edit box lost the input focus.
 
virtual void EditBoxGainedFocus ()
 An edit box gained the input focus.
 
virtual std::vector< int > GetListOfMonitorRefreshRates ()
 Get a list of refresh rates of each available monitor.
 
virtual int GetSuggestedUIScale ()
 Get a suggested default GUI scale taking screen DPI into account.
 
virtual std::string_view GetInfoString () const
 
void QueueOnMainThread (std::function< void()> &&func)
 Queue a function to be called on the main thread with game state lock held and video buffer locked.
 
void GameLoopPause ()
 Pause the game-loop for a bit, releasing the game-state lock.
 
- Public Member Functions inherited from Driver
virtual std::optional< std::string_view > Start (const StringList &parm)=0
 Start this driver.
 
virtual void Stop ()=0
 Stop this driver.
 
virtual std::string_view GetName () const =0
 Get the name of this driver.
 

Static Public Member Functions

static VideoDriverGetInstance ()
 Get the currently active instance of the video driver.
 
static std::string GetCaption ()
 Get the caption to use for the game's title bar.
 

Protected Member Functions

virtual Dimension GetScreenSize () const
 Get the resolution of the main screen.
 
virtual float GetDPIScale ()
 Get DPI scaling factor of the screen OTTD is displayed on.
 
void UpdateAutoResolution ()
 Apply resolution auto-detection and clamp to sensible defaults.
 
virtual void InputLoop ()
 Handle input logic, is CTRL pressed, should we fast-forward, etc.
 
virtual bool LockVideoBuffer ()
 Make sure the video buffer is ready for drawing.
 
virtual void UnlockVideoBuffer ()
 Unlock a previously locked video buffer.
 
virtual void Paint ()
 Paint the window.
 
virtual void CheckPaletteAnim ()
 Process any pending palette animation.
 
virtual bool PollEvent ()
 Process a single system event.
 
void StartGameThread ()
 Start the loop for game-tick.
 
void StopGameThread ()
 Stop the loop for the game-tick.
 
void Tick ()
 Give the video-driver a tick.
 
void SleepTillNextTick ()
 Sleep till the next tick is about to happen.
 
std::chrono::steady_clock::duration GetGameInterval ()
 
std::chrono::steady_clock::duration GetDrawInterval ()
 
void DrainCommandQueue ()
 Execute all queued commands.
 

Static Protected Member Functions

static void GameThreadThunk (VideoDriver *drv)
 

Protected Attributes

const uint ALLOWED_DRIFT = 5
 How many times videodriver can miss deadlines without it being overly compensated.
 
std::chrono::steady_clock::time_point next_game_tick
 
std::chrono::steady_clock::time_point next_draw_tick
 
bool fast_forward_key_pressed
 The fast-forward key is being pressed.
 
bool fast_forward_via_key
 The fast-forward was enabled by key press.
 
bool is_game_threaded
 
std::thread game_thread
 
std::mutex game_state_mutex
 
std::mutex game_thread_wait_mutex
 
bool uses_hardware_acceleration
 

Private Member Functions

void GameLoop ()
 
void GameThread ()
 

Private Attributes

const uint DEFAULT_WINDOW_WIDTH = 640u
 Default window width.
 
const uint DEFAULT_WINDOW_HEIGHT = 480u
 Default window height.
 
std::mutex cmd_queue_mutex
 
std::vector< std::function< void()> > cmd_queue
 

Additional Inherited Members

- Public Types inherited from Driver
enum  Type {
  DT_BEGIN = 0 , DT_MUSIC = 0 , DT_SOUND , DT_VIDEO ,
  DT_END
}
 The type of driver. More...
 

Detailed Description

The base of all video drivers.

Definition at line 34 of file video_driver.hpp.

Constructor & Destructor Documentation

◆ VideoDriver()

VideoDriver::VideoDriver ( bool  uses_hardware_acceleration = false)
inline

Definition at line 39 of file video_driver.hpp.

Member Function Documentation

◆ AfterBlitterChange()

virtual bool VideoDriver::AfterBlitterChange ( )
inlinevirtual

Callback invoked after the blitter was changed.

Returns
True if no error.

Reimplemented in VideoDriver_Allegro, VideoDriver_Cocoa, VideoDriver_SDL_Base, VideoDriver_SDL, and VideoDriver_Win32GDI.

Definition at line 80 of file video_driver.hpp.

◆ ChangeResolution()

virtual bool VideoDriver::ChangeResolution ( int  w,
int  h 
)
pure virtual

Change the resolution of the window.

Parameters
wThe new width.
hThe new height.
Returns
True if the change succeeded.

Implemented in VideoDriver_Allegro, VideoDriver_Cocoa, VideoDriver_Dedicated, VideoDriver_Null, VideoDriver_SDL_Base, VideoDriver_SDL, and VideoDriver_Win32Base.

◆ CheckPaletteAnim()

virtual void VideoDriver::CheckPaletteAnim ( )
inlineprotectedvirtual

Process any pending palette animation.

Reimplemented in VideoDriver_Allegro, VideoDriver_CocoaQuartz, VideoDriver_SDL_Base, VideoDriver_SDL, and VideoDriver_Win32Base.

Definition at line 283 of file video_driver.hpp.

Referenced by Tick().

◆ ClaimMousePointer()

virtual bool VideoDriver::ClaimMousePointer ( )
inlinevirtual

Definition at line 85 of file video_driver.hpp.

◆ ClearSystemSprites()

virtual void VideoDriver::ClearSystemSprites ( )
inlinevirtual

Clear all cached sprites.

Reimplemented in VideoDriver_CocoaOpenGL, VideoDriver_Cocoa, and VideoDriver_SDL_OpenGL.

Definition at line 107 of file video_driver.hpp.

Referenced by AdjustGUIZoom(), GfxClearSpriteCache(), and GfxLoadSprites().

◆ DrainCommandQueue()

void VideoDriver::DrainCommandQueue ( )
inlineprotected

Execute all queued commands.

Definition at line 337 of file video_driver.hpp.

References lock.

Referenced by VideoDriver_Dedicated::MainLoop(), and Tick().

◆ EditBoxGainedFocus()

virtual void VideoDriver::EditBoxGainedFocus ( )
inlinevirtual

An edit box gained the input focus.

Reimplemented in VideoDriver_SDL_Base.

Definition at line 157 of file video_driver.hpp.

Referenced by Window::OnFocus(), IConsoleWindow::OnFocus(), and Window::SetFocusedWidget().

◆ EditBoxLostFocus()

virtual void VideoDriver::EditBoxLostFocus ( )
inlinevirtual

An edit box lost the input focus.

Abort character compositing if necessary.

Reimplemented in VideoDriver_Cocoa, VideoDriver_SDL_Base, and VideoDriver_Win32Base.

Definition at line 152 of file video_driver.hpp.

Referenced by IConsoleWindow::Close(), Window::OnFocusLost(), OskWindow::OnFocusLost(), IConsoleWindow::OnFocusLost(), Window::SetFocusedWidget(), and Window::UnfocusFocusedWidget().

◆ GameLoop()

void VideoDriver::GameLoop ( )
private

Definition at line 28 of file video_driver.cpp.

◆ GameLoopPause()

void VideoDriver::GameLoopPause ( )

Pause the game-loop for a bit, releasing the game-state lock.

This allows, if the draw-tick requested this, the drawing to happen.

Definition at line 66 of file video_driver.cpp.

References lock.

Referenced by GRFFileScanner::AddFile().

◆ GameThread()

void VideoDriver::GameThread ( )
private

Definition at line 43 of file video_driver.cpp.

◆ GameThreadThunk()

void VideoDriver::GameThreadThunk ( VideoDriver drv)
staticprotected

Definition at line 81 of file video_driver.cpp.

◆ GetAnimBuffer()

virtual uint8_t * VideoDriver::GetAnimBuffer ( )
inlinevirtual

◆ GetCaption()

std::string VideoDriver::GetCaption ( )
static

Get the caption to use for the game's title bar.

Returns
The caption.

Definition at line 189 of file video_driver.cpp.

Referenced by VideoDriver_SDL_Base::CreateMainWindow(), and VideoDriver_Win32Base::MakeWindow().

◆ GetDPIScale()

virtual float VideoDriver::GetDPIScale ( )
inlineprotectedvirtual

Get DPI scaling factor of the screen OTTD is displayed on.

Returns
1.0 for default platform DPI, > 1.0 for higher DPI values, and < 1.0 for smaller DPI values.

Reimplemented in VideoDriver_Cocoa, and VideoDriver_Win32Base.

Definition at line 239 of file video_driver.hpp.

Referenced by GetSuggestedUIScale().

◆ GetDrawInterval()

std::chrono::steady_clock::duration VideoDriver::GetDrawInterval ( )
inlineprotected

Definition at line 329 of file video_driver.hpp.

◆ GetGameInterval()

std::chrono::steady_clock::duration VideoDriver::GetGameInterval ( )
inlineprotected

Definition at line 313 of file video_driver.hpp.

◆ GetInfoString()

virtual std::string_view VideoDriver::GetInfoString ( ) const
inlinevirtual

Definition at line 178 of file video_driver.hpp.

◆ GetInstance()

◆ GetListOfMonitorRefreshRates()

virtual std::vector< int > VideoDriver::GetListOfMonitorRefreshRates ( )
inlinevirtual

Get a list of refresh rates of each available monitor.

Returns
A vector of the refresh rates of all available monitors.

Reimplemented in VideoDriver_Allegro, VideoDriver_Cocoa, VideoDriver_SDL_Base, and VideoDriver_Win32Base.

Definition at line 163 of file video_driver.hpp.

Referenced by AddCustomRefreshRates().

◆ GetScreenSize()

virtual Dimension VideoDriver::GetScreenSize ( ) const
inlineprotectedvirtual

Get the resolution of the main screen.

Reimplemented in VideoDriver_Cocoa, VideoDriver_SDL_Base, and VideoDriver_Win32Base.

Definition at line 233 of file video_driver.hpp.

References DEFAULT_WINDOW_HEIGHT, and DEFAULT_WINDOW_WIDTH.

Referenced by VideoDriver_SDL_Base::GetScreenSize(), and UpdateAutoResolution().

◆ GetSuggestedUIScale()

virtual int VideoDriver::GetSuggestedUIScale ( )
inlinevirtual

Get a suggested default GUI scale taking screen DPI into account.

Definition at line 171 of file video_driver.hpp.

References Clamp(), and GetDPIScale().

Referenced by UpdateGUIZoom().

◆ HasAnimBuffer()

virtual bool VideoDriver::HasAnimBuffer ( )
inlinevirtual

Does this video driver support a separate animation buffer in addition to the colour buffer?

Returns
True if a separate animation buffer is supported.

Reimplemented in VideoDriver_CocoaOpenGL, and VideoDriver_SDL_OpenGL.

Definition at line 135 of file video_driver.hpp.

Referenced by FBlitter_40bppAnim::IsUsable().

◆ HasEfficient8Bpp()

virtual bool VideoDriver::HasEfficient8Bpp ( ) const
inlinevirtual

Has this video driver an efficient code path for palette animated 8-bpp sprites?

Returns
True if the driver has an efficient code path for 8-bpp.

Reimplemented in VideoDriver_CocoaOpenGL, and VideoDriver_SDL_OpenGL.

Definition at line 126 of file video_driver.hpp.

◆ HasGUI()

virtual bool VideoDriver::HasGUI ( ) const
inlinevirtual

Whether the driver has a graphical user interface with the end user.

Or in other words, whether we should spawn a thread for world generation and NewGRF scanning so the graphical updates can keep coming. Otherwise progress has to be shown on the console, which uses by definition another thread/process for display purposes.

Returns
True for all drivers except null and dedicated.

Reimplemented in VideoDriver_Dedicated, and VideoDriver_Null.

Definition at line 117 of file video_driver.hpp.

Referenced by Tick().

◆ InputLoop()

virtual void VideoDriver::InputLoop ( )
inlineprotectedvirtual

Handle input logic, is CTRL pressed, should we fast-forward, etc.

Reimplemented in VideoDriver_Allegro, VideoDriver_Cocoa, VideoDriver_SDL_Base, VideoDriver_SDL, and VideoDriver_Win32Base.

Definition at line 259 of file video_driver.hpp.

Referenced by VideoDriver_Null::MainLoop(), and Tick().

◆ LockVideoBuffer()

virtual bool VideoDriver::LockVideoBuffer ( )
inlineprotectedvirtual

Make sure the video buffer is ready for drawing.

Returns
True if the video buffer has to be unlocked.

Reimplemented in VideoDriver_Cocoa, VideoDriver_SDL_Base, and VideoDriver_Win32Base.

Definition at line 265 of file video_driver.hpp.

Referenced by Tick().

◆ MainLoop()

virtual void VideoDriver::MainLoop ( )
pure virtual

◆ MakeDirty()

virtual void VideoDriver::MakeDirty ( int  left,
int  top,
int  width,
int  height 
)
pure virtual

Mark a particular area dirty.

Parameters
leftThe left most line of the dirty area.
topThe top most line of the dirty area.
widthThe width of the dirty area.
heightThe height of the dirty area.

Implemented in VideoDriver_Allegro, VideoDriver_Cocoa, VideoDriver_Dedicated, VideoDriver_Null, VideoDriver_SDL_Base, VideoDriver_SDL, and VideoDriver_Win32Base.

Referenced by NetworkDrawChatMessage(), NetworkUndrawChatMessage(), Blitter_32bppAnim::PaletteAnimate(), and RedrawScreenRect().

◆ Paint()

virtual void VideoDriver::Paint ( )
inlineprotectedvirtual

◆ PollEvent()

virtual bool VideoDriver::PollEvent ( )
inlineprotectedvirtual

Process a single system event.

Returns
False if there are no more events to process.

Reimplemented in VideoDriver_Allegro, VideoDriver_Cocoa, VideoDriver_SDL_Base, VideoDriver_SDL, and VideoDriver_Win32Base.

Definition at line 289 of file video_driver.hpp.

Referenced by Tick().

◆ PopulateSystemSprites()

virtual void VideoDriver::PopulateSystemSprites ( )
inlinevirtual

Populate all sprites in cache.

Reimplemented in VideoDriver_CocoaOpenGL, VideoDriver_Cocoa, and VideoDriver_SDL_OpenGL.

Definition at line 102 of file video_driver.hpp.

Referenced by Tick().

◆ QueueOnMainThread()

void VideoDriver::QueueOnMainThread ( std::function< void()> &&  func)
inline

Queue a function to be called on the main thread with game state lock held and video buffer locked.

Queued functions will be executed on the next draw tick.

Parameters
funcFunction to call.

Definition at line 189 of file video_driver.hpp.

References lock.

Referenced by MakeScreenshot(), and SwitchNewGRFBlitter().

◆ SleepTillNextTick()

void VideoDriver::SleepTillNextTick ( )
protected

Sleep till the next tick is about to happen.

Definition at line 171 of file video_driver.cpp.

Referenced by VideoDriver_Dedicated::MainLoop(), VideoDriver_SDL::MainLoop(), and VideoDriver_Win32Base::MainLoop().

◆ StartGameThread()

void VideoDriver::StartGameThread ( )
protected

Start the loop for game-tick.

Definition at line 86 of file video_driver.cpp.

References Debug, and StartNewThread().

Referenced by VideoDriver_SDL_Base::MainLoop(), VideoDriver_SDL::MainLoop(), and VideoDriver_Win32Base::MainLoop().

◆ StopGameThread()

void VideoDriver::StopGameThread ( )
protected

Stop the loop for the game-tick.

This can still tick at most one time before truly shutting down.

Definition at line 95 of file video_driver.cpp.

Referenced by VideoDriver_SDL_Base::MainLoop(), VideoDriver_SDL::MainLoop(), and VideoDriver_Win32Base::MainLoop().

◆ Tick()

void VideoDriver::Tick ( )
protected

◆ ToggleFullscreen()

virtual bool VideoDriver::ToggleFullscreen ( bool  fullscreen)
pure virtual

Change the full screen setting.

Parameters
fullscreenThe new setting.
Returns
True if the change succeeded.

Implemented in VideoDriver_Allegro, VideoDriver_Cocoa, VideoDriver_Dedicated, VideoDriver_Null, VideoDriver_SDL_Base, VideoDriver_SDL, and VideoDriver_Win32Base.

◆ ToggleVsync()

virtual void VideoDriver::ToggleVsync ( bool  vsync)
inlinevirtual

Change the vsync setting.

Parameters
vsyncThe new setting.

Reimplemented in VideoDriver_SDL_OpenGL.

Definition at line 74 of file video_driver.hpp.

Referenced by GameOptionsWindow::OnClick().

◆ UnlockVideoBuffer()

virtual void VideoDriver::UnlockVideoBuffer ( )
inlineprotectedvirtual

Unlock a previously locked video buffer.

Reimplemented in VideoDriver_Cocoa, VideoDriver_SDL_Base, and VideoDriver_Win32Base.

Definition at line 273 of file video_driver.hpp.

Referenced by Tick().

◆ UpdateAutoResolution()

void VideoDriver::UpdateAutoResolution ( )
inlineprotected

Apply resolution auto-detection and clamp to sensible defaults.

Definition at line 244 of file video_driver.hpp.

References _cur_resolution, ClampU(), DEFAULT_WINDOW_HEIGHT, DEFAULT_WINDOW_WIDTH, and GetScreenSize().

Referenced by VideoDriver_Dedicated::Start(), VideoDriver_Null::Start(), and VideoDriver_SDL::Start().

◆ UseSystemCursor()

virtual bool VideoDriver::UseSystemCursor ( )
inlinevirtual

Get whether the mouse cursor is drawn by the video driver.

Returns
True if cursor drawing is done by the video driver.

Reimplemented in VideoDriver_CocoaOpenGL, and VideoDriver_SDL_OpenGL.

Definition at line 94 of file video_driver.hpp.

Field Documentation

◆ ALLOWED_DRIFT

const uint VideoDriver::ALLOWED_DRIFT = 5
protected

How many times videodriver can miss deadlines without it being overly compensated.

Definition at line 228 of file video_driver.hpp.

◆ cmd_queue

std::vector<std::function<void()> > VideoDriver::cmd_queue
private

Definition at line 371 of file video_driver.hpp.

◆ cmd_queue_mutex

std::mutex VideoDriver::cmd_queue_mutex
private

Definition at line 370 of file video_driver.hpp.

◆ DEFAULT_WINDOW_HEIGHT

const uint VideoDriver::DEFAULT_WINDOW_HEIGHT = 480u
private

Default window height.

Definition at line 36 of file video_driver.hpp.

Referenced by GetScreenSize(), and UpdateAutoResolution().

◆ DEFAULT_WINDOW_WIDTH

const uint VideoDriver::DEFAULT_WINDOW_WIDTH = 640u
private

Default window width.

Definition at line 35 of file video_driver.hpp.

Referenced by GetScreenSize(), and UpdateAutoResolution().

◆ fast_forward_key_pressed

bool VideoDriver::fast_forward_key_pressed
protected

The fast-forward key is being pressed.

Definition at line 357 of file video_driver.hpp.

Referenced by VideoDriver_SDL_Base::InputLoop(), VideoDriver_SDL::InputLoop(), VideoDriver_Win32Base::InputLoop(), and Tick().

◆ fast_forward_via_key

bool VideoDriver::fast_forward_via_key
protected

The fast-forward was enabled by key press.

Definition at line 358 of file video_driver.hpp.

Referenced by Tick().

◆ game_state_mutex

std::mutex VideoDriver::game_state_mutex
protected

Definition at line 362 of file video_driver.hpp.

◆ game_thread

std::thread VideoDriver::game_thread
protected

Definition at line 361 of file video_driver.hpp.

◆ game_thread_wait_mutex

std::mutex VideoDriver::game_thread_wait_mutex
protected

Definition at line 363 of file video_driver.hpp.

◆ is_game_threaded

bool VideoDriver::is_game_threaded
protected

Definition at line 360 of file video_driver.hpp.

◆ next_draw_tick

std::chrono::steady_clock::time_point VideoDriver::next_draw_tick
protected

Definition at line 355 of file video_driver.hpp.

◆ next_game_tick

std::chrono::steady_clock::time_point VideoDriver::next_game_tick
protected

Definition at line 354 of file video_driver.hpp.

◆ uses_hardware_acceleration

bool VideoDriver::uses_hardware_acceleration
protected

Definition at line 365 of file video_driver.hpp.


The documentation for this class was generated from the following files: