cpp-sdl2
C++ header-only SDL2 wrapper
sdl::Window Class Reference

Represent an SDL window. More...

#include <window.hpp>

Classes

class  GlContext
 Nested class that represent a managed OpenGL Context by the SDL. More...
 

Public Types

enum  gl_swap_interval { gl_swap_interval::immediate, gl_swap_interval::vsync, gl_swap_interval::adaptive_vsync }
 Define the type of window swapping strategy for opengl windows. More...
 

Public Member Functions

 Window (std::string const &title, Vec2i const &size, Uint32 flags=SDL_WINDOW_SHOWN)
 Construct a window. More...
 
 Window ()=default
 Construct an invalid window object. You will need to initialize it by moving in a real sdl::Window in. More...
 
 Window (Window &&other) noexcept
 Default move ctor. More...
 
Windowoperator= (Window &&other) noexcept
 Move assign operator. More...
 
 Window (Window &)=delete
 
Windowoperator= (Window &)=delete
 
virtual ~Window ()
 Destructor. Calls SDL_DestroyWindow() automatically for you. More...
 
SDL_Window * ptr () const
 Getter for the raw SDL2 window pointer. More...
 
Renderer make_renderer (Uint32 flags=SDL_RENDERER_ACCELERATED) const
 2D renderer factory. More...
 
int display_index () const
 Get the current window display index. More...
 
void set_display_mode (SDL_DisplayMode const &mode) const
 Set the window display mode. More...
 
SDL_DisplayMode display_mode () const
 Get the current display mode. More...
 
Uint32 flags () const
 Get the flags of this window. More...
 
Windowgrab (bool g=true)
 Grab window. More...
 
Windowrelease (bool r=true)
 Release window. More...
 
bool grabbed () const
 Is window grabed. More...
 
Windowmove_to (Vec2i const &v)
 Move window to specific location on screen. More...
 
Windowmove_by (Vec2i const &v)
 Translate window on screen. More...
 
Vec2i position () const
 Get current window position. More...
 
void resize (Vec2i const &newsize) const
 Change the size of the window the size of the window. More...
 
Vec2i size () const
 Get current window size. More...
 
Windowrename (std::string const &t)
 Change window name. More...
 
std::string title () const
 the current window title More...
 
void set_icon (Surface const &icon) const
 Set the window icon. More...
 
void set_icon (std::string const &filename) const
 Set the window icon (may require linking and activating SDL_Image) More...
 
Windowhide ()
 Hide the window. More...
 
Windowmaximize ()
 Maximize the window. More...
 
Windowminimize ()
 Minimize the window. More...
 
Windowraise ()
 Raise the window. More...
 
Windowrestore ()
 Restore the window. More...
 
bool fullscreen () const
 Returns true if window is currently fullscreen (both real and "desktop mode") More...
 
Windowset_fullscreen (bool fs)
 Set the window fullscreen. More...
 
Windowtoggle_fullscreen ()
 Toggle the window fullscreen. More...
 
SDL_SysWMinfo wm_info () const
 Get window manager info. Exact content of this structure is fully platform dependant. More...
 
std::vector< const char * > vk_get_instance_extensions ()
 Enumerate the required extensions to create a VkSurfaceKHR on the current system. More...
 
VkSurfaceKHR vk_create_surface (VkInstance instance)
 Create a vulkan surface for the current platform. More...
 
vk::UniqueSurfaceKHR vk_create_unique_surface (vk::Instance instance)
 Create a vulkan surface using the C++ wrapper around UniqueHandle. More...
 
Vec2i vk_get_drawable_size ()
 Get the underlayoing drawable size of the window. More...
 
GlContext create_context () const
 Create an OpenGL context from the current window. More...
 
void gl_swap () const
 Swap buffers for GL when using double buffering on the current window. More...
 
Vec2i gl_get_drawable_size () const
 Get the actual size of the OpenGL drawing area on the window. May not match window size on platform that uses display scaling. More...
 

Static Public Member Functions

static void gl_set_attribute (SDL_GLattr attr, int val)
 
static void gl_set_attribute (SDL_GLattr attr, bool val)
 
static int gl_get_attribute (SDL_GLattr attr)
 Get the value of the specified OpenGL attribute. More...
 
static void gl_reset_attribute ()
 Reset all OpenGL attributes to their default values. More...
 
static bool gl_is_extension_supported (std::string const &ext_name)
 Return true if the specified extension is supported. More...
 
static void gl_set_swap_interval (gl_swap_interval swap_mode)
 Set the swap interval. If exception thrown while attempting to use adaptive vsync, use standard vsync. More...
 
static gl_swap_interval gl_get_swap_interval ()
 Get the current swap interval. More...
 

Private Attributes

SDL_Window * window_ = nullptr
 Raw naked pointer to an SDL window. More...
 

Detailed Description

Represent an SDL window.

Also contains accessor to any window related adjacent functionality like OpenGL/Vulkan helper functions

Definition at line 19 of file window.hpp.

Member Enumeration Documentation

◆ gl_swap_interval

Define the type of window swapping strategy for opengl windows.

Enumerator
immediate 
vsync 
adaptive_vsync 

Definition at line 309 of file window.hpp.

Constructor & Destructor Documentation

◆ Window() [1/4]

sdl::Window::Window ( std::string const &  title,
Vec2i const &  size,
Uint32  flags = SDL_WINDOW_SHOWN 
)
inline

Construct a window.

This safely create an SDL_Window for you

Parameters
titleName of the window
sizeSize of the window on screen when shown
flagsAny flags needed to be passed to SDL_CreateWindow

Definition at line 26 of file window.hpp.

References flags(), Window(), and window_.

◆ Window() [2/4]

sdl::Window::Window ( )
default

Construct an invalid window object. You will need to initialize it by moving in a real sdl::Window in.

Referenced by operator=(), and Window().

◆ Window() [3/4]

sdl::Window::Window ( Window &&  other)
inlinenoexcept

Default move ctor.

Definition at line 37 of file window.hpp.

◆ Window() [4/4]

sdl::Window::Window ( Window )
delete

◆ ~Window()

virtual sdl::Window::~Window ( )
inlinevirtual

Destructor. Calls SDL_DestroyWindow() automatically for you.

Definition at line 58 of file window.hpp.

References window_.

Member Function Documentation

◆ create_context()

GlContext sdl::Window::create_context ( ) const
inline

Create an OpenGL context from the current window.

Definition at line 386 of file window.hpp.

References window_.

◆ display_index()

int sdl::Window::display_index ( ) const
inline

Get the current window display index.

Definition at line 74 of file window.hpp.

References window_.

◆ display_mode()

SDL_DisplayMode sdl::Window::display_mode ( ) const
inline

Get the current display mode.

Definition at line 92 of file window.hpp.

References window_.

◆ flags()

Uint32 sdl::Window::flags ( ) const
inline

Get the flags of this window.

Definition at line 103 of file window.hpp.

References window_.

Referenced by fullscreen(), make_renderer(), and Window().

◆ fullscreen()

bool sdl::Window::fullscreen ( ) const
inline

Returns true if window is currently fullscreen (both real and "desktop mode")

Definition at line 202 of file window.hpp.

References flags().

Referenced by toggle_fullscreen().

◆ gl_get_attribute()

static int sdl::Window::gl_get_attribute ( SDL_GLattr  attr)
inlinestatic

Get the value of the specified OpenGL attribute.

Definition at line 292 of file window.hpp.

◆ gl_get_drawable_size()

Vec2i sdl::Window::gl_get_drawable_size ( ) const
inline

Get the actual size of the OpenGL drawing area on the window. May not match window size on platform that uses display scaling.

Definition at line 392 of file window.hpp.

References size(), and window_.

◆ gl_get_swap_interval()

static gl_swap_interval sdl::Window::gl_get_swap_interval ( )
inlinestatic

Get the current swap interval.

Definition at line 331 of file window.hpp.

References adaptive_vsync, immediate, and vsync.

◆ gl_is_extension_supported()

static bool sdl::Window::gl_is_extension_supported ( std::string const &  ext_name)
inlinestatic

Return true if the specified extension is supported.

Definition at line 303 of file window.hpp.

◆ gl_reset_attribute()

static void sdl::Window::gl_reset_attribute ( )
inlinestatic

Reset all OpenGL attributes to their default values.

Definition at line 300 of file window.hpp.

◆ gl_set_attribute() [1/2]

static void sdl::Window::gl_set_attribute ( SDL_GLattr  attr,
int  val 
)
inlinestatic

Definition at line 284 of file window.hpp.

◆ gl_set_attribute() [2/2]

static void sdl::Window::gl_set_attribute ( SDL_GLattr  attr,
bool  val 
)
inlinestatic

Definition at line 289 of file window.hpp.

References gl_set_attribute().

Referenced by gl_set_attribute().

◆ gl_set_swap_interval()

static void sdl::Window::gl_set_swap_interval ( gl_swap_interval  swap_mode)
inlinestatic

Set the swap interval. If exception thrown while attempting to use adaptive vsync, use standard vsync.

Definition at line 317 of file window.hpp.

References adaptive_vsync, immediate, and vsync.

◆ gl_swap()

void sdl::Window::gl_swap ( ) const
inline

Swap buffers for GL when using double buffering on the current window.

Definition at line 389 of file window.hpp.

References window_.

◆ grab()

Window& sdl::Window::grab ( bool  g = true)
inline

Grab window.

Definition at line 106 of file window.hpp.

References window_.

Referenced by release().

◆ grabbed()

bool sdl::Window::grabbed ( ) const
inline

Is window grabed.

Definition at line 116 of file window.hpp.

References window_.

◆ hide()

Window& sdl::Window::hide ( )
inline

Hide the window.

Definition at line 170 of file window.hpp.

References window_.

◆ make_renderer()

Renderer sdl::Window::make_renderer ( Uint32  flags = SDL_RENDERER_ACCELERATED) const
inline

2D renderer factory.

Permit to easily create a 2D renderer. Hardware acceleration enabled by default

Parameters
flagsAny flags needed to be passed to SDL_CreateRenderer.

Definition at line 66 of file window.hpp.

References flags(), and window_.

◆ maximize()

Window& sdl::Window::maximize ( )
inline

Maximize the window.

Definition at line 176 of file window.hpp.

References window_.

◆ minimize()

Window& sdl::Window::minimize ( )
inline

Minimize the window.

Definition at line 182 of file window.hpp.

References window_.

◆ move_by()

Window& sdl::Window::move_by ( Vec2i const &  v)
inline

Translate window on screen.

Parameters
vtranslation vector

Definition at line 127 of file window.hpp.

References move_to(), and position().

◆ move_to()

Window& sdl::Window::move_to ( Vec2i const &  v)
inline

Move window to specific location on screen.

Parameters
vVector pointing to the new window location

Definition at line 120 of file window.hpp.

References window_.

Referenced by move_by().

◆ operator=() [1/2]

Window& sdl::Window::operator= ( Window &&  other)
inlinenoexcept

Move assign operator.

If this object represent a valid window, it will be destroyed before acquiring the window_ pointer from other

Parameters
otherAnother sdl::Window object

Definition at line 42 of file window.hpp.

References Window(), and window_.

Referenced by sdl::Window::GlContext::~GlContext().

◆ operator=() [2/2]

Window& sdl::Window::operator= ( Window )
delete

◆ position()

Vec2i sdl::Window::position ( ) const
inline

Get current window position.

Definition at line 129 of file window.hpp.

References window_.

Referenced by move_by().

◆ ptr()

SDL_Window* sdl::Window::ptr ( ) const
inline

Getter for the raw SDL2 window pointer.

Definition at line 61 of file window.hpp.

References window_.

Referenced by sdl::show_message_box(), and sdl::Mouse::warp_in_window().

◆ raise()

Window& sdl::Window::raise ( )
inline

Raise the window.

Definition at line 188 of file window.hpp.

References window_.

◆ release()

Window& sdl::Window::release ( bool  r = true)
inline

Release window.

Definition at line 113 of file window.hpp.

References grab().

◆ rename()

Window& sdl::Window::rename ( std::string const &  t)
inline

Change window name.

Parameters
tnew window "title"

Definition at line 149 of file window.hpp.

References window_.

◆ resize()

void sdl::Window::resize ( Vec2i const &  newsize) const
inline

Change the size of the window the size of the window.

Definition at line 138 of file window.hpp.

References window_.

◆ restore()

Window& sdl::Window::restore ( )
inline

Restore the window.

Definition at line 194 of file window.hpp.

References window_.

◆ set_display_mode()

void sdl::Window::set_display_mode ( SDL_DisplayMode const &  mode) const
inline

Set the window display mode.

Parameters
modeDisplay mode to use

Definition at line 83 of file window.hpp.

References window_.

◆ set_fullscreen()

Window& sdl::Window::set_fullscreen ( bool  fs)
inline

Set the window fullscreen.

Definition at line 207 of file window.hpp.

References window_.

Referenced by toggle_fullscreen().

◆ set_icon() [1/2]

void sdl::Window::set_icon ( Surface const &  icon) const
inline

Set the window icon.

Parameters
iconSurface containing the icon to use

Definition at line 159 of file window.hpp.

References sdl::Surface::ptr(), and window_.

◆ set_icon() [2/2]

void sdl::Window::set_icon ( std::string const &  filename) const
inline

Set the window icon (may require linking and activating SDL_Image)

Parameters
filenamepath to a file you can use to set the window icon

Definition at line 163 of file window.hpp.

References window_.

◆ size()

Vec2i sdl::Window::size ( ) const
inline

Get current window size.

Definition at line 140 of file window.hpp.

References window_.

Referenced by gl_get_drawable_size(), and vk_get_drawable_size().

◆ title()

std::string sdl::Window::title ( ) const
inline

the current window title

Definition at line 155 of file window.hpp.

References window_.

◆ toggle_fullscreen()

Window& sdl::Window::toggle_fullscreen ( )
inline

Toggle the window fullscreen.

Definition at line 217 of file window.hpp.

References fullscreen(), and set_fullscreen().

◆ vk_create_surface()

VkSurfaceKHR sdl::Window::vk_create_surface ( VkInstance  instance)
inline

Create a vulkan surface for the current platform.

Returns
your vulkan surface

Definition at line 252 of file window.hpp.

References window_.

Referenced by vk_create_unique_surface().

◆ vk_create_unique_surface()

vk::UniqueSurfaceKHR sdl::Window::vk_create_unique_surface ( vk::Instance  instance)
inline

Create a vulkan surface using the C++ wrapper around UniqueHandle.

Definition at line 262 of file window.hpp.

References vk_create_surface().

◆ vk_get_drawable_size()

Vec2i sdl::Window::vk_get_drawable_size ( )
inline

Get the underlayoing drawable size of the window.

Use this for vulkan viewport size, scissor rects and any place you need VkExtents

Returns
Size in an integer 2D vector

Definition at line 270 of file window.hpp.

References size(), and window_.

◆ vk_get_instance_extensions()

std::vector<const char*> sdl::Window::vk_get_instance_extensions ( )
inline

Enumerate the required extensions to create a VkSurfaceKHR on the current system.

Returns
a vector of const char strings containing the extensions names

Definition at line 234 of file window.hpp.

Referenced by wm_info().

◆ wm_info()

SDL_SysWMinfo sdl::Window::wm_info ( ) const
inline

Get window manager info. Exact content of this structure is fully platform dependant.

Definition at line 220 of file window.hpp.

References vk_get_instance_extensions(), and window_.

Member Data Documentation

◆ window_


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