The Battle for Wesnoth  1.17.6+dev
Classes | Public Types | Public Member Functions | Static Public Member Functions | Private Types | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
CVideo Class Reference

#include <video.hpp>

Classes

struct  error
 
class  quit
 Type that can be thrown as an exception to quit to desktop. More...
 
class  video_event_handler
 Helper class to manage SDL events. More...
 

Public Types

enum  FAKE_TYPES { NO_FAKE, FAKE, FAKE_TEST }
 

Public Member Functions

 CVideo (const CVideo &)=delete
 
CVideooperator= (const CVideo &)=delete
 
 CVideo (FAKE_TYPES type=NO_FAKE)
 
 ~CVideo ()
 
void make_fake ()
 
void make_test_fake (const unsigned width=1024, const unsigned height=768)
 Creates a fake frame buffer for the unit tests. More...
 
bool faked () const
 
bool any_fake () const
 
bool non_interactive () const
 
bool surface_initialized () const
 
void init_window ()
 Initializes a new SDL window instance, taking into account any preiously saved states. More...
 
sdl::windowget_window ()
 Returns a pointer to the underlying SDL window. More...
 
SDL_Renderer * get_renderer ()
 Returns a pointer to the underlying window's renderer. More...
 
bool has_window ()
 
void set_fullscreen (bool ison)
 
void toggle_fullscreen ()
 
bool is_fullscreen () const
 
bool supports_software_vsync () const
 Only recent versions of SDL support vsync with the software renderer. More...
 
bool set_resolution (const point &resolution)
 Set the window resolution. More...
 
point current_resolution ()
 
void update_buffers ()
 Update buffers to match current resolution and pixel scale settings. More...
 
std::vector< pointget_available_resolutions (const bool include_current=false)
 Returns the list of available screen resolutions. More...
 
SDL_Point output_size () const
 Returns the size of the final render target. More...
 
SDL_Point window_size () const
 Returns the size of the window in display units / screen coordinates. More...
 
rect draw_area () const
 Returns the size and location of the current drawing area in pixels. More...
 
SDL_Rect input_area () const
 Returns the size and location of the window's input area in pixels. More...
 
int get_pixel_scale () const
 Get the current active pixel scale multiplier. More...
 
SDL_Rect clip_to_draw_area (const SDL_Rect *r) const
 Clip a rectangle to the drawing area. More...
 
rect to_output (const rect &draw_space_rect) const
 Convert coordinates in draw space to coordinates in render space. More...
 
bool window_has_flags (uint32_t flags) const
 Tests whether the given flags are currently set on the SDL window. More...
 
void set_window_title (const std::string &title)
 Sets the title of the main window. More...
 
void set_window_icon (surface &icon)
 Sets the icon of the main window. More...
 
int current_refresh_rate () const
 
void render_screen ()
 Renders the screen. More...
 
void update_framebuffer ()
 Updates and ensures the framebuffer surface is valid. More...
 
void clear_screen ()
 Clear the screen contents. More...
 
surface read_pixels (SDL_Rect *r=nullptr)
 Copy back a portion of the render target that is already drawn. More...
 
surface read_pixels_low_res (SDL_Rect *r=nullptr)
 The same as read_pixels, but returns a low-resolution surface suitable for use with the old drawing system. More...
 
texture read_texture (SDL_Rect *r=nullptr)
 Copy a portion of the render target to another texture. More...
 
void lock_updates (bool value)
 Stop the screen being redrawn. More...
 
bool update_locked () const
 Whether the screen has been 'locked' or not. More...
 
void lock_flips (bool)
 
void force_render_target (const texture &t)
 Set the render target, without any provided way of setting it back. More...
 
void clear_render_target ()
 Reset the render target to the main window / screen. More...
 
texture get_render_target ()
 Get the current render target. More...
 
int set_help_string (const std::string &str)
 Displays a help string with the given text. More...
 
void clear_help_string (int handle)
 Removes the help string with the given handle. More...
 
void clear_all_help_strings ()
 Removes all help strings. More...
 

Static Public Member Functions

static bool setup_completed ()
 
static CVideoget_singleton ()
 
static std::string current_driver ()
 
static std::vector< std::string > enumerate_drivers ()
 
static void delay (unsigned int milliseconds)
 Waits a given number of milliseconds before returning. More...
 

Private Types

enum  MODE_EVENT { TO_RES, TO_FULLSCREEN, TO_WINDOWED, TO_MAXIMIZED_WINDOW }
 

Private Member Functions

void init_fake_window ()
 
void set_window_mode (const MODE_EVENT mode, const point &size)
 Sets the window's mode - ie, changing it to fullscreen, maximizing, etc. More...
 
void update_framebuffer_fake ()
 
void initSDL ()
 Initializes the SDL video subsystem. More...
 

Private Attributes

std::unique_ptr< sdl::windowwindow
 The SDL window object. More...
 
texture render_texture_ = {}
 The main offscreen render target. More...
 
texture current_render_target_ = {}
 The current offscreen render target. More...
 
bool fake_screen_
 
video_event_handler event_handler_
 
int help_string_
 Curent ID of the help string. More...
 
int updated_locked_
 
int flip_locked_
 
int refresh_rate_
 
int offset_x_
 
int offset_y_
 
point logical_size_
 
int pixel_scale_
 

Static Private Attributes

static CVideosingleton_ = nullptr
 

Detailed Description

Definition at line 40 of file video.hpp.

Member Enumeration Documentation

◆ FAKE_TYPES

Enumerator
NO_FAKE 
FAKE 
FAKE_TEST 

Definition at line 46 of file video.hpp.

◆ MODE_EVENT

enum CVideo::MODE_EVENT
private
Enumerator
TO_RES 
TO_FULLSCREEN 
TO_WINDOWED 
TO_MAXIMIZED_WINDOW 

Definition at line 109 of file video.hpp.

Constructor & Destructor Documentation

◆ CVideo() [1/2]

CVideo::CVideo ( const CVideo )
delete

◆ CVideo() [2/2]

CVideo::CVideo ( FAKE_TYPES  type = NO_FAKE)

Definition at line 53 of file video.cpp.

References FAKE, FAKE_TEST, initSDL(), make_fake(), make_test_fake(), NO_FAKE, pixel_scale_, and singleton_.

◆ ~CVideo()

CVideo::~CVideo ( )

Definition at line 90 of file video.cpp.

References LOG_DP, and singleton_.

Member Function Documentation

◆ any_fake()

bool CVideo::any_fake ( ) const

Definition at line 99 of file video.cpp.

References fake_screen_.

◆ clear_all_help_strings()

void CVideo::clear_all_help_strings ( )

Removes all help strings.

Definition at line 835 of file video.cpp.

References clear_help_string(), and help_string_.

◆ clear_help_string()

void CVideo::clear_help_string ( int  handle)

◆ clear_render_target()

void CVideo::clear_render_target ( )

Reset the render target to the main window / screen.

Definition at line 498 of file video.cpp.

References force_render_target().

Referenced by render_screen().

◆ clear_screen()

void CVideo::clear_screen ( )

Clear the screen contents.

Definition at line 675 of file video.cpp.

References DBG_DP, and window.

Referenced by editor::editor_display::editor_display().

◆ clip_to_draw_area()

SDL_Rect CVideo::clip_to_draw_area ( const SDL_Rect *  r) const

Clip a rectangle to the drawing area.

This does not change the original

Parameters
rThe SDL_Rect to clip.
Returns
The new clipped SDL_Rect.

Definition at line 510 of file video.cpp.

References draw_area(), and rect::intersect().

◆ current_driver()

std::string CVideo::current_driver ( )
static

Definition at line 697 of file video.cpp.

◆ current_refresh_rate()

int CVideo::current_refresh_rate ( ) const
inline

Definition at line 227 of file video.hpp.

References t.

Referenced by display::draw_wrap(), and draw_manager::wait_for_vsync().

◆ current_resolution()

point CVideo::current_resolution ( )

◆ delay()

void CVideo::delay ( unsigned int  milliseconds)
static

Waits a given number of milliseconds before returning.

Definition at line 458 of file video.cpp.

References game_config::no_delay.

Referenced by game_lua_kernel::intf_delay(), controller_base::play_slice(), unit_animator::wait_for_end(), and unit_animator::wait_until().

◆ draw_area()

rect CVideo::draw_area ( ) const

◆ enumerate_drivers()

std::vector< std::string > CVideo::enumerate_drivers ( )
static

Definition at line 703 of file video.cpp.

References n.

◆ faked()

bool CVideo::faked ( ) const
inline

◆ force_render_target()

void CVideo::force_render_target ( const texture t)

Set the render target, without any provided way of setting it back.

End-users should not use this function directly. In stead use draw::set_render_target(), which returns a setter object which will automatically restore the render target upon leaving scope.

Parameters
tThe new render target. This must be a texture created with SDL_TEXTUREACCESS_TARGET, or an empty texture to indicate the underlying window.

Definition at line 465 of file video.cpp.

References current_render_target_, DBG_DP, draw_area(), texture::draw_size(), ERR_DP, texture::get(), texture::get_raw_size(), get_renderer(), texture::h(), h, render_texture_, t, texture::w(), w, and window.

Referenced by clear_render_target(), render_screen(), draw::render_target_setter::render_target_setter(), update_framebuffer(), update_framebuffer_fake(), and draw::render_target_setter::~render_target_setter().

◆ get_available_resolutions()

std::vector< point > CVideo::get_available_resolutions ( const bool  include_current = false)

Returns the list of available screen resolutions.

Definition at line 721 of file video.cpp.

References current_resolution(), i, preferences::min_window_height, preferences::min_window_width, PLAIN_LOG, and window.

Referenced by gui2::dialogs::preferences_dialog::set_resolution_list().

◆ get_pixel_scale()

int CVideo::get_pixel_scale ( ) const
inline

Get the current active pixel scale multiplier.

This is equal to output_size() / draw_area(). Currently it is always integer, and the same in both dimensions.

This may differ from preferences::pixel_scale() in some cases, For example if the window is too small to fit the desired scale.

Returns
The currently active pixel scale multiplier.

Definition at line 190 of file video.hpp.

Referenced by font::floating_label::create_texture(), gui::textbox::draw_contents(), display::expose(), draw::tiled_highres(), to_output(), font::pango_text::update_pixel_scale(), and display::update_render_textures().

◆ get_render_target()

texture CVideo::get_render_target ( )

Get the current render target.

Will return an empty texture if the render target is the underlying window.

Definition at line 503 of file video.cpp.

References current_render_target_, and get_renderer().

Referenced by draw::render_target_setter::render_target_setter().

◆ get_renderer()

SDL_Renderer * CVideo::get_renderer ( )

Returns a pointer to the underlying window's renderer.

Definition at line 688 of file video.cpp.

References window.

Referenced by force_render_target(), get_render_target(), renderer(), texture::reset(), and texture::texture().

◆ get_singleton()

static CVideo& CVideo::get_singleton ( )
inlinestatic

Definition at line 57 of file video.hpp.

Referenced by gui2::dialogs::preferences_dialog::apply_pixel_scale(), font::floating_label::create_texture(), gui2::dialogs::loading_screen::display(), do_replay(), gui2::canvas::draw(), gui2::event::sdl_event_handler::draw_everything(), hotkey::command_executor::execute_command_wrap(), game_events::wml_event_pump::flush_messages(), gui2::dialogs::preferences_dialog::fullscreen_toggle_callback(), draw::get_clip(), draw::get_viewport(), windows_tray_notification::get_window_handle(), gui2::event::sdl_event_handler::handle_event(), gui2::dialogs::preferences_dialog::handle_res_select(), controller_base::handle_scroll(), preferences::prefs_event_handler::handle_window_event(), game_lua_kernel::impl_run_animation(), savegame::loadgame::load_game_ingame(), gui2::dialogs::make_screenshot(), help::unit_topic_generator::operator()(), events::peek_for_resize(), controller_base::play_slice(), campaign_controller::playsingle_scenario(), events::pump(), events::raise_resize_event(), draw::render_target_setter::render_target_setter(), renderer(), texture::reset(), desktop::notifications::send(), gui2::dialogs::preferences_dialog::set_resolution_list(), editor::context_manager::set_window_title(), gui2::dialogs::modeless_dialog::show(), gui2::dialogs::modal_dialog::show(), help::show_with_toplevel(), draw_manager::sparkle(), texture::texture(), preferences::theme(), draw::tiled_highres(), preferences::turbo(), font::floating_label::update(), font::pango_text::update_pixel_scale(), display::update_render_textures(), gui2::settings::update_screen_size_variables(), gui::widget::video(), draw_manager::wait_for_vsync(), gui2::window::window(), editor::context_manager::~context_manager(), and draw::render_target_setter::~render_target_setter().

◆ get_window()

sdl::window * CVideo::get_window ( )

Returns a pointer to the underlying SDL window.

Definition at line 683 of file video.cpp.

References window.

Referenced by windows_tray_notification::get_window_handle().

◆ has_window()

bool CVideo::has_window ( )
inline

Definition at line 99 of file video.hpp.

References sound::current_driver(), and sound::enumerate_drivers().

◆ init_fake_window()

void CVideo::init_fake_window ( )
private

◆ init_window()

void CVideo::init_window ( )

◆ initSDL()

void CVideo::initSDL ( )
private

Initializes the SDL video subsystem.

Definition at line 82 of file video.cpp.

References ERR_DP.

Referenced by CVideo().

◆ input_area()

SDL_Rect CVideo::input_area ( ) const

Returns the size and location of the window's input area in pixels.

We use SDL_RendererSetLogicalSize to ensure this always matches draw_area(), but for clarity there are two separate functions.

Definition at line 444 of file video.cpp.

References draw_area(), p, window, and WRN_DP.

Referenced by events::pump().

◆ is_fullscreen()

bool CVideo::is_fullscreen ( ) const

Definition at line 781 of file video.cpp.

References window.

Referenced by set_fullscreen().

◆ lock_flips()

void CVideo::lock_flips ( bool  lock)

Definition at line 918 of file video.cpp.

References flip_locked_.

◆ lock_updates()

void CVideo::lock_updates ( bool  value)

Stop the screen being redrawn.

Anything that happens while the updates are locked will be hidden from the user's view.

Note that this function is re-entrant, meaning that if lock_updates(true) is called twice, lock_updates(false) must be called twice to unlock updates.

Definition at line 649 of file video.cpp.

References updated_locked_.

Referenced by display::display().

◆ make_fake()

void CVideo::make_fake ( )

Definition at line 136 of file video.cpp.

References fake_screen_, logical_size_, and refresh_rate_.

Referenced by CVideo().

◆ make_test_fake()

void CVideo::make_test_fake ( const unsigned  width = 1024,
const unsigned  height = 768 
)

Creates a fake frame buffer for the unit tests.

Parameters
widthThe width of the buffer.
heightThe height of the buffer.

Definition at line 143 of file video.cpp.

References init_fake_window(), refresh_rate_, set_resolution(), and window.

Referenced by CVideo(), and test_utils::get_fake_display().

◆ non_interactive()

bool CVideo::non_interactive ( ) const

Definition at line 104 of file video.cpp.

References window.

Referenced by display::display().

◆ operator=()

CVideo& CVideo::operator= ( const CVideo )
delete

◆ output_size()

SDL_Point CVideo::output_size ( ) const

Returns the size of the final render target.

This is irrelevant for most purposes. Use draw_area() in stead.

Definition at line 422 of file video.cpp.

References window.

◆ read_pixels()

surface CVideo::read_pixels ( SDL_Rect *  r = nullptr)

Copy back a portion of the render target that is already drawn.

This area is specified in draw coordinates, not render coordinates. Thus the size of the retrieved surface may not match the size of r.

If not null, r will be automatically clipped to the drawing area.

Note: This is a very slow function! Its use should be phased out for everything except maybe screenshots.

Parameters
rThe portion of the render target to retrieve, in draw coordinates. If not null, this will be modified to reflect the portion of the draw area that has been returned.

Definition at line 572 of file video.cpp.

References rect::clip(), current_render_target_, d, DBG_DP, draw_area(), texture::draw_size(), ERR_DP, texture::get(), texture::get_raw_size(), h, render_texture_, s, t, to_output(), w, window, and WRN_DP.

Referenced by gui2::dialogs::make_screenshot(), read_pixels_low_res(), read_texture(), and display::screenshot().

◆ read_pixels_low_res()

surface CVideo::read_pixels_low_res ( SDL_Rect *  r = nullptr)

The same as read_pixels, but returns a low-resolution surface suitable for use with the old drawing system.

This should be considered deprecated, and phased out ASAP.

Definition at line 630 of file video.cpp.

References draw_area(), h, read_pixels(), s, scale_surface(), w, window, and WRN_DP.

Referenced by gui2::canvas::draw().

◆ read_texture()

texture CVideo::read_texture ( SDL_Rect *  r = nullptr)

Copy a portion of the render target to another texture.

This area is specified in draw coordinates, not render coordinates. Thus the size of the retrieved texture may not match the size of r.

If not null, r will be automatically clipped to the drawing area.

Note: This is a very slow function! Its use should be phased out for everything except maybe screenshots.

Parameters
rThe portion of the render target to retrieve, in draw coordinates. If not null, this will be modified to reflect the portion of the draw area that has been returned.

Definition at line 644 of file video.cpp.

References read_pixels().

◆ render_screen()

void CVideo::render_screen ( )

Renders the screen.

Should normally not be called directly!

Definition at line 532 of file video.cpp.

References clear_render_target(), current_render_target_, DBG_DP, ERR_DP, fake_screen_, flip_locked_, force_render_target(), texture::get(), render_texture_, window, and WRN_DP.

Referenced by draw_manager::sparkle().

◆ set_fullscreen()

void CVideo::set_fullscreen ( bool  ison)

◆ set_help_string()

int CVideo::set_help_string ( const std::string &  str)

◆ set_resolution()

bool CVideo::set_resolution ( const point resolution)

Set the window resolution.

Parameters
resolutionThe new width and height.
Returns
Whether the resolution was successfully changed.

Definition at line 873 of file video.cpp.

References preferences::_set_maximized(), preferences::_set_resolution(), current_resolution(), d, display::get_singleton(), LOG_DP, events::raise_resize_event(), preferences::resolution(), set_window_mode(), and TO_RES.

Referenced by gui2::dialogs::preferences_dialog::handle_res_select(), and make_test_fake().

◆ set_window_icon()

void CVideo::set_window_icon ( surface icon)

Sets the icon of the main window.

Parameters
iconThe new icon for the window.

Definition at line 669 of file video.cpp.

References window.

◆ set_window_mode()

void CVideo::set_window_mode ( const MODE_EVENT  mode,
const point size 
)
private

Sets the window's mode - ie, changing it to fullscreen, maximizing, etc.

Parameters
modeThe action to perform.
sizeThe new window size. Utilized if mode is TO_RES.

Definition at line 381 of file video.cpp.

References fake_screen_, LOG_DP, utf8::size(), TO_FULLSCREEN, TO_MAXIMIZED_WINDOW, TO_RES, TO_WINDOWED, update_framebuffer(), update_framebuffer_fake(), and window.

Referenced by set_fullscreen(), and set_resolution().

◆ set_window_title()

void CVideo::set_window_title ( const std::string &  title)

Sets the title of the main window.

Parameters
titleThe new title for the window.

Definition at line 663 of file video.cpp.

References window.

Referenced by editor::context_manager::set_window_title(), and editor::context_manager::~context_manager().

◆ setup_completed()

static bool CVideo::setup_completed ( )
inlinestatic

Definition at line 52 of file video.hpp.

◆ supports_software_vsync()

bool CVideo::supports_software_vsync ( ) const

Only recent versions of SDL support vsync with the software renderer.

This is irrelevant now as we're using hardware rendering exclusively. I recommend removing this function entirely after several months, when it has become clear that the software renderer won't be needed.

Definition at line 789 of file video.cpp.

References sdl::runtime_at_least().

◆ surface_initialized()

bool CVideo::surface_initialized ( ) const

Definition at line 109 of file video.cpp.

References logical_size_.

Referenced by display::display().

◆ to_output()

rect CVideo::to_output ( const rect draw_space_rect) const

Convert coordinates in draw space to coordinates in render space.

Definition at line 519 of file video.cpp.

References get_pixel_scale(), offset_x_, and offset_y_.

Referenced by read_pixels(), and display::scroll().

◆ toggle_fullscreen()

void CVideo::toggle_fullscreen ( )

◆ update_buffers()

void CVideo::update_buffers ( )

Update buffers to match current resolution and pixel scale settings.

Also triggers a full redraw.

Definition at line 897 of file video.cpp.

References d, display::get_singleton(), LOG_DP, events::raise_resize_event(), and update_framebuffer().

Referenced by gui2::dialogs::preferences_dialog::apply_pixel_scale().

◆ update_framebuffer()

void CVideo::update_framebuffer ( )

◆ update_framebuffer_fake()

void CVideo::update_framebuffer_fake ( )
private

◆ update_locked()

bool CVideo::update_locked ( ) const

Whether the screen has been 'locked' or not.

Definition at line 658 of file video.cpp.

References updated_locked_.

Referenced by display::draw(), game_lua_kernel::impl_run_animation(), display::redraw_everything(), display::scroll(), display::scroll_to_xy(), and display::update_display().

◆ window_has_flags()

bool CVideo::window_has_flags ( uint32_t  flags) const

Tests whether the given flags are currently set on the SDL window.

Parameters
flagsThe flags to test, OR'd together.

Definition at line 716 of file video.cpp.

References window.

Referenced by controller_base::handle_scroll(), and desktop::notifications::send().

◆ window_size()

SDL_Point CVideo::window_size ( ) const

Returns the size of the window in display units / screen coordinates.

This should match the value sent by window resize events, and also those used for setting resolution.

Definition at line 431 of file video.cpp.

References window.

Referenced by events::raise_resize_event().

Member Data Documentation

◆ current_render_target_

texture CVideo::current_render_target_ = {}
private

The current offscreen render target.

Definition at line 384 of file video.hpp.

Referenced by force_render_target(), get_render_target(), read_pixels(), and render_screen().

◆ event_handler_

video_event_handler CVideo::event_handler_
private

Definition at line 408 of file video.hpp.

Referenced by init_fake_window(), and init_window().

◆ fake_screen_

bool CVideo::fake_screen_
private

Definition at line 390 of file video.hpp.

Referenced by any_fake(), make_fake(), render_screen(), and set_window_mode().

◆ flip_locked_

int CVideo::flip_locked_
private

Definition at line 414 of file video.hpp.

Referenced by lock_flips(), and render_screen().

◆ help_string_

int CVideo::help_string_
private

Curent ID of the help string.

Definition at line 411 of file video.hpp.

Referenced by clear_all_help_strings(), clear_help_string(), and set_help_string().

◆ logical_size_

point CVideo::logical_size_
private

◆ offset_x_

int CVideo::offset_x_
private

Definition at line 416 of file video.hpp.

Referenced by to_output(), and update_framebuffer().

◆ offset_y_

int CVideo::offset_y_
private

Definition at line 416 of file video.hpp.

Referenced by to_output(), and update_framebuffer().

◆ pixel_scale_

int CVideo::pixel_scale_
private

Definition at line 418 of file video.hpp.

Referenced by CVideo(), update_framebuffer(), and update_framebuffer_fake().

◆ refresh_rate_

int CVideo::refresh_rate_
private

Definition at line 415 of file video.hpp.

Referenced by init_window(), make_fake(), and make_test_fake().

◆ render_texture_

texture CVideo::render_texture_ = {}
private

The main offscreen render target.

Definition at line 381 of file video.hpp.

Referenced by force_render_target(), init_window(), read_pixels(), render_screen(), update_framebuffer(), and update_framebuffer_fake().

◆ singleton_

CVideo* CVideo::singleton_ = nullptr
inlinestaticprivate

Definition at line 375 of file video.hpp.

Referenced by CVideo(), and ~CVideo().

◆ updated_locked_

int CVideo::updated_locked_
private

Definition at line 413 of file video.hpp.

Referenced by lock_updates(), and update_locked().

◆ window

std::unique_ptr<sdl::window> CVideo::window
private

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