The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
window.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "sdl/window.hpp"
16 
17 
18 #include "sdl/exception.hpp"
19 #include "sdl/image.hpp"
20 
21 #include <SDL_render.h>
22 
23 namespace sdl
24 {
25 
27  const int x,
28  const int y,
29  const int w,
30  const int h,
31  const Uint32 window_flags,
32  const Uint32 render_flags)
33  : window_(SDL_CreateWindow(title.c_str(), x, y, w, h, window_flags))
34  , pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
35 {
36  if(!window_) {
37  throw texception("Failed to create a SDL_Window object.", true);
38  }
39 
40  if(!SDL_CreateRenderer(window_, -1, render_flags)) {
41  throw texception("Failed to create a SDL_Renderer object.", true);
42  }
43 
44  SDL_RendererInfo info;
45  if(SDL_GetRendererInfo(*this, &info) != 0) {
46  throw texception("Failed to retrieve the information of the renderer.",
47  true);
48  }
49 
50  if(info.num_texture_formats == 0) {
51  throw texception("The renderer has no texture information available.\n",
52  false);
53  }
54 
55  pixel_format_ = info.texture_formats[0];
56 
57  fill(0,0,0);
58 
59  render();
60 }
61 
63 {
64  if(window_) {
65  SDL_DestroyWindow(window_);
66  }
67 }
68 
69 void twindow::set_size(const int w, const int h)
70 {
71  SDL_SetWindowSize(window_, w, h);
72 }
73 
75 {
76  SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
77 }
78 
80 {
81  SDL_MaximizeWindow(window_);
82 }
83 
85 {
86  SDL_SetWindowFullscreen(window_, 0);
87 }
88 
90 {
91  SDL_RestoreWindow(window_);
92 }
93 
95 {
96  SDL_SetWindowFullscreen(window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
97 }
98 
99 void twindow::fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a)
100 {
101  SDL_SetRenderDrawColor(*this, r, g, b, a);
102  if(SDL_RenderClear(*this) != 0) {
103  throw texception("Failed to clear the SDL_Renderer object.",
104  true);
105  }
106 }
107 
109 {
110  SDL_RenderPresent(*this);
111 }
112 
113 void twindow::set_title(const std::string& title)
114 {
115  SDL_SetWindowTitle(window_, title.c_str());
116 }
117 
118 void twindow::set_icon(const surface& icon)
119 {
120  SDL_SetWindowIcon(window_, icon);
121 }
122 
124 {
125  return SDL_GetWindowFlags(window_);
126 }
127 
128 void twindow::set_minimum_size(int min_w, int min_h)
129 {
130  SDL_SetWindowMinimumSize(window_, min_w, min_h);
131 }
132 
133 twindow::operator SDL_Window*()
134 {
135  return window_;
136 }
137 
138 twindow::operator SDL_Renderer*()
139 {
140  return SDL_GetRenderer(window_);
141 }
142 
143 } // namespace sdl
144 
void restore()
Dummy function for restoring the window.
Definition: window.cpp:89
void set_size(const int w, const int h)
Wrapper for SDL_SetWindowSize.
Definition: window.cpp:69
logger & info()
Definition: log.cpp:91
void full_screen()
Dummy function for setting the window to fullscreen mode.
Definition: window.cpp:94
GLboolean GLboolean g
Definition: glew.h:7319
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
void render()
Renders the contents of the window.
Definition: window.cpp:108
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
void set_icon(const surface &icon)
Sets the icon of the window.
Definition: window.cpp:118
void fill(Uint8 r, Uint8 g, Uint8 b, Uint8 a=0)
Clears the contents of the window with a given color.
Definition: window.cpp:99
GLboolean GLboolean GLboolean GLboolean a
Definition: glew.h:7319
SDL_Window * window_
The SDL_Window we own.
Definition: window.hpp:177
void set_minimum_size(int min_w, int min_h)
Set mimimum size of the window.
Definition: window.cpp:128
void to_window()
Dummy function for returning the window to windowed mode.
Definition: window.cpp:84
Contains a wrapper class for the GPU_Image class.
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
int get_flags()
Definition: window.cpp:123
GLdouble GLdouble GLdouble r
Definition: glew.h:1374
Contains a basic exception class for SDL operations.
twindow(const std::string &title, const int x, const int y, const int w, const int h, const Uint32 window_flags, const Uint32 render_flags)
Constructor.
Definition: window.cpp:26
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:113
Contains a wrapper class for the SDL_Window class.
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:79
void center()
Dummy function for centering the window.
Definition: window.cpp:74
Uint32 pixel_format_
The preferred pixel format for the renderer.
Definition: window.hpp:180
GLsizei const GLcharARB ** string
Definition: glew.h:4503