Window.cpp
1 /******************************************************************************
2 
3  nomlib - C++11 cross-platform game engine
4 
5 Copyright (c) 2013, Jeffrey Carpenter <jeffrey.carp@gmail.com>
6 All rights reserved.
7 
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10 
11 1. Redistributions of source code must retain the above copyright notice, this
12  list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 ******************************************************************************/
29 #include "nomlib/graphics/Window.hpp"
30 //#include "nomlib/graphics/Renderer.hpp"
31 
32 namespace nom {
33 
34 // static initialization
35 SDL_RENDERER::RawPtr Window::context_ = nullptr;
36 
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 )
41 {
42 NOM_LOG_TRACE ( NOM );
43 
44  if ( SDL_Init ( SDL_INIT_VIDEO ) != 0 )
45  {
46 NOM_LOG_ERR ( NOM, SDL_GetError() );
47  }
48 
49  atexit ( SDL_Quit );
50 }
51 
52 Window::~Window ( void )
53 {
54 NOM_LOG_TRACE ( NOM );
55 }
56 
58  const std::string& window_title, int32 width, int32 height,
59  uint32 window_flags, uint32 context_flags
60  )
61 {
62  this->window_.reset ( SDL_CreateWindow (
63  window_title.c_str(),
64  SDL_WINDOWPOS_UNDEFINED,
65  SDL_WINDOWPOS_UNDEFINED,
66  width,
67  height,
69  )
70  );
71 
72  if ( this->window_valid() == false )
73  {
74 NOM_LOG_ERR ( NOM, "Could not create SDL window." );
75  return false;
76  }
77 
78  this->initialize ( this->window(), -1, context_flags );
79  if ( this->renderer_valid() == false )
80  {
81 NOM_LOG_ERR ( NOM, "Could not create SDL renderer." );
82  return false;
83  }
84 
85  // Track our unique identifiers for our brand spanking new window!
86  this->window_id_ = SDL_GetWindowID ( this->window() );
87  this->window_display_id_ = SDL_GetWindowDisplayIndex ( this->window() );
88  this->enabled_ = true;
89 
90  // We must always have an active rendering context before we do any rendering
91  // calls; this includes even creating SDL textures!
92  if ( context_ == nullptr )
93  {
94  this->set_active();
95  }
96 
97  // Try to ensure that we have no leftover artifacts by clearing and filling
98  // window with a solid black paint bucket fill.
99  this->fill ( Color::Black );
100 
101  return true;
102 }
103 
105 {
106  return this;
107 }
108 
109 SDL_WINDOW::RawPtr Window::window ( void ) const
110 {
111  return this->window_.get();
112 }
113 
114 SDL_SURFACE::RawPtr Window::window_surface ( void ) const
115 {
116  return SDL_GetWindowSurface ( this->window() );
117 }
118 
119 bool Window::window_valid ( void ) const
120 {
121  if ( this->window() != nullptr ) return true;
122 
123  return false;
124 }
125 
126 Point2i Window::position ( void ) const
127 {
128  Point2i pos;
129 
130  SDL_GetWindowPosition ( this->window(), &pos.x, &pos.y );
131 
132  return pos;
133 }
134 
135 Point2i Window::size ( void ) const
136 {
137  Point2i size;
138  SDL_GetWindowSize ( this->window(), &size.x, &size.y );
139  return size;
140 }
141 
142 /*
143 const uint8 Window::getDisplayColorBits ( void ) const
144 {
145  SDL_Surface* screen = this->window();
146 
147  // We prevent a segmentation fault here by providing a means of accessing the
148  // video modes without already having initialized the video display via
149  // SDL_SetVideoMode.
150  if ( screen == nullptr )
151  {
152  //const SDL_VideoInfo* bpp = SDL_GetVideoInfo();
153  //return bpp->vfmt->BitsPerPixel;
154  }
155 
156  return screen->format->BitsPerPixel;
157  return 0;
158 }
159 */
160 
161 uint32 Window::window_flags ( void ) const
162 {
163  return SDL_GetWindowFlags ( this->window() );
164 }
165 
166 /*
167 uint16 Window::getDisplayPitch ( void ) const
168 {
169  //return SDL_GetVideoSurface()->pitch;
170  return 0;
171 }
172 */
173 
174 /* FIXME
175 void* Window::pixels ( void ) const
176 {
177  SDL_Surface *buffer = this->window_surface();
178 
179  return buffer->pixels;
180 }
181 FIXME */
182 
183 uint32 Window::pixel_format ( void ) const
184 {
185  return SDL_GetWindowPixelFormat ( this->window() );
186 }
187 
188 const Coords Window::display_bounds ( void ) const
189 {
190  SDL_Rect display_bounds;
191  Coords bounds;
192 
193  if ( SDL_GetDisplayBounds ( this->window_display_id(), &display_bounds ) != 0 )
194  {
195 NOM_LOG_ERR ( NOM, SDL_GetError() );
196  return Coords::null;
197  }
198 
199  bounds.x = display_bounds.x;
200  bounds.y = display_bounds.y;
201  bounds.w = display_bounds.w;
202  bounds.h = display_bounds.h;
203 
204  return bounds;
205 }
206 
207 VideoModeList Window::getVideoModes ( void ) const
208 {
209 /*
210  VideoModeList modes;
211  SDL_Rect** mode;
212 
213  mode = SDL_ListModes ( nullptr, SDL_FULLSCREEN );
214 
215  if ( mode == nullptr )
216  {
217 NOM_LOG_INFO ( NOM, "Any video mode is supported." ); // FIXME?
218  return modes;
219  }
220  else if ( mode == ( SDL_Rect**) - 1 )
221  {
222 NOM_LOG_INFO ( NOM, "No video modes are supported." );
223  return modes;
224  }
225  else
226  {
227  for ( int32 idx = 0; mode[idx]; idx++ )
228  {
229  modes.push_back ( VideoMode ( mode[idx]->w, mode[idx]->h, this->getDisplayColorBits() ) );
230  }
231 
232  std::sort ( modes.begin(), modes.end(), std::greater<VideoMode>() );
233  }
234  return modes;
235 */
236  return VideoModeList();
237 }
238 /*
239 bool Window::getCanvasLock ( void ) const
240 {
241  //return this->window()->locked;
242  return false;
243 }
244 */
245 /*
246 bool Window::mustLock ( void ) const
247 {
248  if ( SDL_MUSTLOCK ( this->window() ) )
249  {
250  return true;
251  }
252  else
253  {
254  return false;
255  }
256  return false;
257 }
258 */
259 /*
260 bool Window::lock ( void ) const
261 {
262  if ( this->mustLock() == true )
263  {
264  if ( SDL_LockSurface ( this->window() ) == -1 )
265  {
266 NOM_LOG_ERR ( NOM, "Could not lock video surface memory." );
267  return false;
268  }
269  }
270  return true;
271 }
272 */
273 /*
274 void Window::unlock ( void ) const
275 {
276  SDL_UnlockSurface ( this->window() );
277 }
278 */
279 
280 void Window::draw ( SDL_SURFACE::RawPtr video_buffer, const Coords& bounds ) const
281 {
282  SDL_Rect blit_coords = SDL_RECT ( bounds );
283  SDL_Rect blit_offsets = SDL_RECT ( bounds );
284 
285  //if ( ! this->valid() ) return;
286 
287  if ( blit_offsets.w != -1 && blit_offsets.h != -1 )
288  {
289  if ( SDL_BlitSurface ( this->window_surface(), &blit_offsets, video_buffer, &blit_coords ) != 0 )
290  {
291 NOM_LOG_ERR ( NOM, SDL_GetError() );
292  return; // ERR
293  }
294  }
295  else
296  {
297  if ( SDL_BlitSurface ( this->window_surface(), nullptr, video_buffer, &blit_coords ) != 0 )
298  {
299 NOM_LOG_ERR ( NOM, SDL_GetError() );
300  return; // ERR
301  }
302  }
303 }
304 
305 bool Window::flip ( void ) const
306 {
307  if ( SDL_UpdateWindowSurface ( this->window() ) != 0 )
308  {
309 NOM_LOG_ERR ( NOM, SDL_GetError() );
310  return false;
311  }
312  return true;
313 }
314 
315 bool Window::fullscreen ( void ) const
316 {
317  if ( this->fullscreen_ ) return true;
318 
319  return false;
320 }
321 
322 void Window::set_fullscreen ( bool state )
323 {
324  this->fullscreen_ = state;
325 }
326 
328 {
329  if ( this->fullscreen() == true ) // Go back to window state
330  {
331  if ( SDL_SetWindowFullscreen ( this->window(), 0 ) != 0 )
332  {
333 NOM_LOG_ERR ( NOM, SDL_GetError() );
334  return false;
335  }
336  this->set_fullscreen ( false );
337  return true;
338  }
339 
340  if ( this->fullscreen() == false )
341  {
342  if ( SDL_SetWindowFullscreen ( this->window(), SDL_WINDOW_FULLSCREEN_DESKTOP ) != 0 )
343  {
344 NOM_LOG_ERR ( NOM, SDL_GetError() );
345  return false;
346  }
347  this->set_fullscreen ( true );
348  return true;
349  }
350 
351  return false; // We should never reach this statement
352 }
353 
354 const std::string Window::window_title ( void ) const
355 {
356  std::string title = SDL_GetWindowTitle ( this->window() );
357 
358  return title;
359 }
360 
361 void Window::set_window_title ( const std::string& title )
362 {
363  SDL_SetWindowTitle ( this->window(), title.c_str() );
364 }
365 
366 bool Window::set_window_icon ( const std::string& filename )
367 {
368  Image icon;
369 
370  if ( icon.load ( filename ) == false ) return false;
371 
372  if ( icon.valid() == false )
373  {
374 NOM_LOG_ERR ( NOM, "Could not obtain a valid icon." );
375  return false;
376  }
377 
378  SDL_SetWindowIcon ( this->window(), icon.image() );
379 
380  return true;
381 }
382 
383 void Window::set_position ( int32 x, int32 y )
384 {
385  SDL_SetWindowPosition ( this->window(), x, y );
386 }
387 
388 uint32 Window::window_id ( void ) const
389 {
390  return this->window_id_;
391 }
392 
393 SDL_Window* Window::window_id ( uint32 id )
394 {
395  return SDL_GetWindowFromID ( id );
396 }
397 
398 int Window::window_display_id ( void ) const
399 {
400  return this->window_display_id_;
401 }
402 
404 {
405  SDL_DisableScreenSaver();
406 }
407 
408 bool Window::screen_saver ( void )
409 {
410  return SDL_IsScreenSaverEnabled();
411 }
412 
414 {
415  SDL_MaximizeWindow ( this->window() );
416 }
417 
419 {
420  SDL_MinimizeWindow ( this->window() );
421 }
422 
423 void Window::raise_window ( void )
424 {
425  SDL_RaiseWindow ( this->window() );
426 }
427 
429 {
430  SDL_RestoreWindow ( this->window() );
431 }
432 
433 void Window::show_window ( void )
434 {
435  SDL_ShowWindow ( this->window() );
436 }
437 
438 void Window::hide_window ( void )
439 {
440  SDL_HideWindow ( this->window() );
441 }
442 
443 void Window::set_window_grab ( bool grab )
444 {
445  SDL_SetWindowGrab ( this->window(), SDL_BOOL(grab) );
446 }
447 
448 void Window::set_minimum_window_size ( int min_width, int min_height )
449 {
450  SDL_SetWindowMaximumSize ( this->window(), min_width, min_height );
451 }
452 
453 void Window::set_maximum_window_size ( int max_width, int max_height )
454 {
455  SDL_SetWindowMaximumSize ( this->window(), max_width, max_height );
456 }
457 
458 bool Window::save_screenshot ( const std::string& filename ) const
459 {
460  Image screenshot;
461  Point2i renderer_size = Renderer::size(); // Width & height of target in pixels
462  SDL_SURFACE::UniquePtr buffer ( nullptr, priv::FreeSurface );
463 
464  int bpp = 0; // bytes per pixel
465  uint32 red_mask = 0;
466  uint32 green_mask = 0;
467  uint32 blue_mask = 0;
468  uint32 alpha_mask = 0;
469 
470  if ( SDL_BOOL( SDL_PixelFormatEnumToMasks ( SDL_PIXELFORMAT_ARGB8888, &bpp, &red_mask, &green_mask, &blue_mask, &alpha_mask ) ) != true )
471  {
472 NOM_LOG_ERR( NOM, SDL_GetError() );
473  return false;
474  }
475 
476  screenshot.initialize( Renderer::pixels(), renderer_size.x, renderer_size.y, bpp, (renderer_size.x * 4), red_mask, green_mask, blue_mask, alpha_mask );
477 
478  if ( screenshot.save( filename ) == false )
479  {
480  return false;
481  }
482 
483  return true;
484 }
485 
486 void Window::set_active ( void )
487 {
488  set_context ( this->get() );
489 }
490 
491 SDL_RENDERER::RawPtr Window::context ( void )
492 {
493  return context_;
494 }
495 
497 {
498  context_ = window->renderer();
499 }
500 
501 
502 } // namespace nom
void draw(SDL_SURFACE::RawPtr video_buffer, const Coords &bounds) const
Definition: Window.cpp:280
static SDL_RENDERER::RawPtr context(void)
Definition: Window.cpp:491
const Point2i size(void) const
Definition: Renderer.cpp:121
SDL_WINDOW::RawPtr window(void) const
Returns a raw pointer to the SDL_Window struct in use for this object.
Definition: Window.cpp:109
const Coords bounds(void) const
Definition: Renderer.cpp:135
uint32 window_id_
Cache the unique window identifier we get from SDL upon initialization.
Definition: Window.hpp:276
void disable_screensaver(void)
Definition: Window.cpp:403
static const Color Black
Predefined color constants for convenience sake.
Definition: Color.hpp:77
bool create(const std::string &window_title, int32 width, int32 height, uint32 window_flags, uint32 context_flags=SDL_RENDERER_ACCELERATED)
Definition: Window.cpp:57
void set_minimum_window_size(int min_width, int min_height)
Definition: Window.cpp:448
bool save_screenshot(const std::string &filename) const
Definition: Window.cpp:458
const Coords display_bounds(void) const
Definition: Window.cpp:188
Window::RawPtr get(void)
Obtain a pointer to this Window object.
Definition: Window.cpp:104
bool fullscreen(void) const
Getter for fullscreen_ state variable.
Definition: Window.cpp:315
static const Coords null
Convenience object that will always contain a value of -1.
Definition: Coords.hpp:94
bool flip(void) const
Definition: Window.cpp:305
bool enabled_
State of the window (visible or not)
Definition: Window.hpp:282
void maximize_window(void)
Definition: Window.cpp:413
SDL_SURFACE::RawPtr image(void) const
Obtain the SDL_Surface struct used in this object instance.
Definition: Image.cpp:93
bool fullscreen_
Toggle window &amp; full-screen states.
Definition: Window.hpp:285
SDL_SURFACE::RawPtr window_surface(void) const
Obtain a SDL_Surface pointer from this Window.
Definition: Window.cpp:114
void show_window(void)
Definition: Window.cpp:433
void hide_window(void)
Definition: Window.cpp:438
bool renderer_valid(void) const
Is this object initialized? Valid when NOT nullptr.
Definition: Renderer.cpp:63
void restore_window(void)
Definition: Window.cpp:428
void raise_window(void)
Definition: Window.cpp:423
bool fill(const Color &color=Color::Blue)
Definition: Renderer.cpp:170
uint32 window_flags(void) const
Definition: Window.cpp:161
bool load(const std::string &filename)
Definition: Image.cpp:185
static void set_context(Window::RawPtr window)
Definition: Window.cpp:496
uint32 pixel_format(void) const
Obtain a pointer to this object&#39;s pixels.
Definition: Window.cpp:183
Point2i position(void) const
Obtain this Window&#39;s position.
Definition: Window.cpp:126
int window_display_id_
Cache the display identifier we get from SDL upon initialization.
Definition: Window.hpp:279
int32 & h
Reference alias for height instance variable.
Definition: Coords.hpp:106
SDL_RENDERER::RawPtr renderer(void) const
Get a raw pointer to the SDL_Renderer in use.
Definition: Renderer.cpp:58
void set_maximum_window_size(int max_width, int max_height)
Definition: Window.cpp:453
Bitmap storage container.
Definition: Image.hpp:49
bool initialize(SDL_WINDOW::RawPtr window, int32 rendering_driver=-1, uint32 context_flags=SDL_RENDERER_ACCELERATED)
Definition: Renderer.cpp:47
VideoModeList getVideoModes(void) const
Definition: Window.cpp:207
bool initialize(void *pixels, int32 width, int32 height, int32 bpp, uint16 pitch, uint32 Rmask, uint32 Gmask, uint32 Bmask, uint32 Amask)
Definition: Image.cpp:74
bool window_valid(void) const
Is this object initialized – not nullptr?
Definition: Window.cpp:119
bool valid(void) const
Is this object initialized? Valid when NOT nullptr.
Definition: Image.cpp:98
bool toggle_fullscreen(void)
Definition: Window.cpp:327
uint32 window_id(void) const
Definition: Window.cpp:388
Coordinate values (x, y, z, width, height ) wrapper.
Definition: Coords.hpp:44
int window_display_id(void) const
Definition: Window.cpp:398
bool save(const std::string &filename, SDL_SURFACE::RawPtr video_buffer) const
Definition: Image.cpp:218
void set_fullscreen(bool state)
Set new fullscreen state.
Definition: Window.cpp:322
bool set_window_icon(const std::string &filename)
Set an icon for this window.
Definition: Window.cpp:366
void set_active(void)
Definition: Window.cpp:486
void * pixels(void) const
Definition: Renderer.cpp:262
int32 & w
Reference alias for width instance variable.
Definition: Coords.hpp:103
void minimize_window(void)
Definition: Window.cpp:418
Point2i size(void) const
Definition: Window.cpp:135
void set_window_grab(bool grab)
Definition: Window.cpp:443
bool screen_saver(void)
Definition: Window.cpp:408