The Battle for Wesnoth  1.15.1+dev
window.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 /**
18  * @file
19  * Contains a wrapper class for the @ref SDL_Window class.
20  */
21 
22 #include <SDL2/SDL_video.h>
23 
24 #include <string>
25 
26 class surface;
27 struct SDL_Renderer;
28 
29 namespace sdl
30 {
31 
32 /**
33  * The wrapper class for the @ref SDL_Window class.
34  *
35  * At the moment of writing it is not certain yet how many windows will be
36  * created. At least one as main window, but maybe the GUI dialogs will have
37  * their own window. Once that is known it might be a good idea to evaluate
38  * whether the class should become a singleton or not.
39  *
40  * The class also wraps several functions operating on @ref SDL_Window objects.
41  * For functions not wrapped the class offers an implicit conversion operator
42  * to a pointer to the @ref SDL_Window object it owns.
43  */
44 class window
45 {
46 public:
47  window(const window&) = delete;
48  window& operator=(const window&) = delete;
49 
50  /***** ***** ***** Constructor and destructor. ***** ***** *****/
51 
52  /**
53  * Constructor.
54  *
55  * The function calls @ref SDL_CreateWindow and @ref SDL_CreateRenderer.
56  *
57  * @param title Used as title for @ref SDL_CreateWindow.
58  * @param x Used as x for @ref SDL_CreateWindow.
59  * @param y Used as y for @ref SDL_CreateWindow.
60  * @param w Used as w for @ref SDL_CreateWindow.
61  * @param h Used as x for @ref SDL_CreateWindow.
62  * @param window_flags Used as flags for @ref SDL_CreateWindow.
63  * @param render_flags Used as flags for @ref SDL_CreateRenderer.
64  */
65  window(const std::string& title,
66  const int x,
67  const int y,
68  const int w,
69  const int h,
70  const uint32_t window_flags,
71  const uint32_t render_flags);
72 
73  ~window();
74 
75 
76  /***** ***** ***** Operations. ***** ***** *****/
77 
78  /**
79  * Wrapper for @ref SDL_SetWindowSize.
80  *
81  * @param w Used as w for @ref SDL_SetWindowSize.
82  * @param h Used as x for @ref SDL_SetWindowSize.
83  */
84  void set_size(const int w, const int h);
85 
86  /**
87  * Gets the window's size, in screen coordinates.
88  *
89  * For the most part, this seems to return the same result as @ref get_output_size. However,
90  * SDL indicates for high DPI displays these two functions could differ. I could not observe
91  * any change in behavior with DPI virtualization on or off, but to be safe, I'm keeping the
92  * two functions separate and using this for matters of window resolution.
93  *
94  * - vultraz, 6/27/2017
95  */
96  SDL_Point get_size();
97 
98  /** Gets the window's renderer output size, in pixels */
99  SDL_Point get_output_size();
100 
101  /**
102  * Dummy function for centering the window.
103  */
104  void center();
105 
106  /**
107  * Dummy function for maximizing the window.
108  */
109  void maximize();
110 
111  /**
112  * Dummy function for restoring the window.
113  */
114  void restore();
115 
116  /**
117  * Dummy function for returning the window to windowed mode.
118  */
119  void to_window();
120 
121  /**
122  * Dummy function for setting the window to fullscreen mode.
123  */
124  void full_screen();
125 
126  /**
127  * Clears the contents of the window with a given color.
128  *
129  * @param r Red value of the color.
130  * @param g Green value of the color.
131  * @param b Blue value of the color.
132  */
133  void fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0);
134 
135  /** Renders the contents of the window. */
136  void render();
137 
138  /**
139  * Sets the title of the window.
140  *
141  * This is a wrapper for @ref SDL_SetWindowTitle.
142  *
143  * @param title The new title for the window.
144  */
145  void set_title(const std::string& title);
146 
147  /**
148  * Sets the icon of the window.
149  *
150  * This is a wrapper for @ref SDL_SetWindowIcon.
151  *
152  * @note The @p icon is a @ref SDL_Surface and not a @ref SDL_Texture, this
153  * is part of the SDL 2 API.
154  *
155  * @param icon The new icon for the window.
156  */
157  void set_icon(const surface& icon);
158 
159  uint32_t get_flags();
160 
161  /**
162  * Set minimum size of the window.
163  *
164  * This is a wrapper for @ref SDL_SetWindowMinimumWindowSize.
165  */
166  void set_minimum_size(int min_w, int min_h);
167 
168  int get_display_index();
169 
170  /***** ***** ***** Conversion operators. ***** ***** *****/
171 
172  /**
173  * Conversion operator to a SDL_Window*.
174  */
175  operator SDL_Window*();
176 
177  /**
178  * Conversion operator to a SDL_Renderer*.
179  */
180  operator SDL_Renderer*();
181 
182 private:
183  /***** ***** ***** Members. ***** ***** *****/
184 
185  /** The @ref SDL_Window we own. */
186  SDL_Window* window_;
187 
188  /** The preferred pixel format for the renderer. */
189  uint32_t pixel_format_;
190 };
191 
192 } // namespace sdl
void full_screen()
Dummy function for setting the window to fullscreen mode.
Definition: window.cpp:116
void set_size(const int w, const int h)
Wrapper for SDL_SetWindowSize.
Definition: window.cpp:75
void restore()
Dummy function for restoring the window.
Definition: window.cpp:111
#define a
uint32_t pixel_format_
The preferred pixel format for the renderer.
Definition: window.hpp:189
#define h
void fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a=0)
Clears the contents of the window with a given color.
Definition: window.cpp:121
#define b
uint32_t get_flags()
Definition: window.cpp:145
The wrapper class for the SDL_Window class.
Definition: window.hpp:44
void set_icon(const surface &icon)
Sets the icon of the window.
Definition: window.cpp:140
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:101
SDL_Point get_size()
Gets the window&#39;s size, in screen coordinates.
Definition: window.cpp:80
SDL_Point get_output_size()
Gets the window&#39;s renderer output size, in pixels.
Definition: window.cpp:88
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:135
SDL_Window * window_
The SDL_Window we own.
Definition: window.hpp:186
double g
Definition: astarsearch.cpp:64
window(const window &)=delete
int w
window & operator=(const window &)=delete
void set_minimum_size(int min_w, int min_h)
Set minimum size of the window.
Definition: window.cpp:150
void render()
Renders the contents of the window.
Definition: window.cpp:130
void to_window()
Dummy function for returning the window to windowed mode.
Definition: window.cpp:106
int get_display_index()
Definition: window.cpp:155
void center()
Dummy function for centering the window.
Definition: window.cpp:96