29 #include "nomlib/graphics/Window.hpp"
35 SDL_RENDERER::RawPtr Window::context_ =
nullptr;
37 Window::Window (
void ) : window_
38 { SDL_WINDOW::UniquePtr (
nullptr, priv::FreeWindow ) },
39 window_id_ ( 0 ), window_display_id_ ( -1 ),
40 enabled_ (
false ), fullscreen_ (
false )
42 NOM_LOG_TRACE ( NOM );
44 if ( SDL_Init ( SDL_INIT_VIDEO ) != 0 )
46 NOM_LOG_ERR ( NOM, SDL_GetError() );
52 Window::~Window (
void )
54 NOM_LOG_TRACE ( NOM );
58 const std::string& window_title, int32 width, int32 height,
59 uint32 window_flags, uint32 context_flags
62 this->window_.reset ( SDL_CreateWindow (
64 SDL_WINDOWPOS_UNDEFINED,
65 SDL_WINDOWPOS_UNDEFINED,
74 NOM_LOG_ERR ( NOM,
"Could not create SDL window." );
81 NOM_LOG_ERR ( NOM,
"Could not create SDL renderer." );
92 if ( context_ ==
nullptr )
111 return this->window_.get();
116 return SDL_GetWindowSurface ( this->
window() );
121 if ( this->
window() !=
nullptr )
return true;
130 SDL_GetWindowPosition ( this->
window(), &pos.x, &pos.y );
138 SDL_GetWindowSize ( this->
window(), &size.x, &size.y );
163 return SDL_GetWindowFlags ( this->
window() );
185 return SDL_GetWindowPixelFormat ( this->
window() );
195 NOM_LOG_ERR ( NOM, SDL_GetError() );
199 bounds.x = display_bounds.x;
200 bounds.y = display_bounds.y;
201 bounds.
w = display_bounds.w;
202 bounds.
h = display_bounds.h;
236 return VideoModeList();
282 SDL_Rect blit_coords = SDL_RECT ( bounds );
283 SDL_Rect blit_offsets = SDL_RECT ( bounds );
287 if ( blit_offsets.w != -1 && blit_offsets.h != -1 )
289 if ( SDL_BlitSurface ( this->
window_surface(), &blit_offsets, video_buffer, &blit_coords ) != 0 )
291 NOM_LOG_ERR ( NOM, SDL_GetError() );
297 if ( SDL_BlitSurface ( this->
window_surface(),
nullptr, video_buffer, &blit_coords ) != 0 )
299 NOM_LOG_ERR ( NOM, SDL_GetError() );
307 if ( SDL_UpdateWindowSurface ( this->
window() ) != 0 )
309 NOM_LOG_ERR ( NOM, SDL_GetError() );
331 if ( SDL_SetWindowFullscreen ( this->
window(), 0 ) != 0 )
333 NOM_LOG_ERR ( NOM, SDL_GetError() );
342 if ( SDL_SetWindowFullscreen ( this->
window(), SDL_WINDOW_FULLSCREEN_DESKTOP ) != 0 )
344 NOM_LOG_ERR ( NOM, SDL_GetError() );
354 const std::string Window::window_title (
void )
const
356 std::string title = SDL_GetWindowTitle ( this->
window() );
361 void Window::set_window_title (
const std::string& title )
363 SDL_SetWindowTitle ( this->
window(), title.c_str() );
370 if ( icon.
load ( filename ) == false )
return false;
372 if ( icon.
valid() == false )
374 NOM_LOG_ERR ( NOM,
"Could not obtain a valid icon." );
378 SDL_SetWindowIcon ( this->
window(), icon.
image() );
383 void Window::set_position ( int32 x, int32 y )
385 SDL_SetWindowPosition ( this->
window(), x, y );
395 return SDL_GetWindowFromID (
id );
405 SDL_DisableScreenSaver();
410 return SDL_IsScreenSaverEnabled();
415 SDL_MaximizeWindow ( this->
window() );
420 SDL_MinimizeWindow ( this->
window() );
425 SDL_RaiseWindow ( this->
window() );
430 SDL_RestoreWindow ( this->
window() );
435 SDL_ShowWindow ( this->
window() );
440 SDL_HideWindow ( this->
window() );
445 SDL_SetWindowGrab ( this->
window(), SDL_BOOL(grab) );
450 SDL_SetWindowMaximumSize ( this->
window(), min_width, min_height );
455 SDL_SetWindowMaximumSize ( this->
window(), max_width, max_height );
462 SDL_SURFACE::UniquePtr buffer (
nullptr, priv::FreeSurface );
466 uint32 green_mask = 0;
467 uint32 blue_mask = 0;
468 uint32 alpha_mask = 0;
470 if ( SDL_BOOL( SDL_PixelFormatEnumToMasks ( SDL_PIXELFORMAT_ARGB8888, &bpp, &red_mask, &green_mask, &blue_mask, &alpha_mask ) ) !=
true )
472 NOM_LOG_ERR( NOM, SDL_GetError() );
476 screenshot.
initialize(
Renderer::pixels(), renderer_size.x, renderer_size.y, bpp, (renderer_size.x * 4), red_mask, green_mask, blue_mask, alpha_mask );
478 if ( screenshot.
save( filename ) == false )
void draw(SDL_SURFACE::RawPtr video_buffer, const Coords &bounds) const
static SDL_RENDERER::RawPtr context(void)
const Point2i size(void) const
SDL_WINDOW::RawPtr window(void) const
Returns a raw pointer to the SDL_Window struct in use for this object.
const Coords bounds(void) const
uint32 window_id_
Cache the unique window identifier we get from SDL upon initialization.
void disable_screensaver(void)
static const Color Black
Predefined color constants for convenience sake.
bool create(const std::string &window_title, int32 width, int32 height, uint32 window_flags, uint32 context_flags=SDL_RENDERER_ACCELERATED)
void set_minimum_window_size(int min_width, int min_height)
bool save_screenshot(const std::string &filename) const
const Coords display_bounds(void) const
Window::RawPtr get(void)
Obtain a pointer to this Window object.
bool fullscreen(void) const
Getter for fullscreen_ state variable.
static const Coords null
Convenience object that will always contain a value of -1.
bool enabled_
State of the window (visible or not)
void maximize_window(void)
SDL_SURFACE::RawPtr image(void) const
Obtain the SDL_Surface struct used in this object instance.
bool fullscreen_
Toggle window & full-screen states.
SDL_SURFACE::RawPtr window_surface(void) const
Obtain a SDL_Surface pointer from this Window.
bool renderer_valid(void) const
Is this object initialized? Valid when NOT nullptr.
void restore_window(void)
bool fill(const Color &color=Color::Blue)
uint32 window_flags(void) const
bool load(const std::string &filename)
static void set_context(Window::RawPtr window)
uint32 pixel_format(void) const
Obtain a pointer to this object's pixels.
Point2i position(void) const
Obtain this Window's position.
int window_display_id_
Cache the display identifier we get from SDL upon initialization.
int32 & h
Reference alias for height instance variable.
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
void set_maximum_window_size(int max_width, int max_height)
Bitmap storage container.
bool initialize(SDL_WINDOW::RawPtr window, int32 rendering_driver=-1, uint32 context_flags=SDL_RENDERER_ACCELERATED)
VideoModeList getVideoModes(void) const
bool initialize(void *pixels, int32 width, int32 height, int32 bpp, uint16 pitch, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask)
bool window_valid(void) const
Is this object initialized – not nullptr?
bool valid(void) const
Is this object initialized? Valid when NOT nullptr.
bool toggle_fullscreen(void)
uint32 window_id(void) const
Coordinate values (x, y, z, width, height ) wrapper.
int window_display_id(void) const
bool save(const std::string &filename, SDL_SURFACE::RawPtr video_buffer) const
void set_fullscreen(bool state)
Set new fullscreen state.
bool set_window_icon(const std::string &filename)
Set an icon for this window.
void * pixels(void) const
int32 & w
Reference alias for width instance variable.
void minimize_window(void)
void set_window_grab(bool grab)