The Battle for Wesnoth  1.15.11+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 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 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 SDL_Window objects.
41  * For functions not wrapped the class offers an implicit conversion operator
42  * to a pointer to the 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 SDL_CreateWindow and SDL_CreateRenderer.
56  *
57  * @param title Used as title for SDL_CreateWindow.
58  * @param x Used as x for SDL_CreateWindow.
59  * @param y Used as y for SDL_CreateWindow.
60  * @param w Used as w for SDL_CreateWindow.
61  * @param h Used as x for SDL_CreateWindow.
62  * @param window_flags Used as flags for SDL_CreateWindow.
63  * @param render_flags Used as flags for 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 SDL_SetWindowSize.
80  *
81  * @param w Used as w for SDL_SetWindowSize.
82  * @param h Used as x for 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  * @param a Alpha value.
133  */
134  void fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0);
135 
136  /** Renders the contents of the window. */
137  void render();
138 
139  /**
140  * Sets the title of the window.
141  *
142  * This is a wrapper for SDL_SetWindowTitle.
143  *
144  * @param title The new title for the window.
145  */
146  void set_title(const std::string& title);
147 
148  /**
149  * Sets the icon of the window.
150  *
151  * This is a wrapper for SDL_SetWindowIcon.
152  *
153  * @note The @p icon is a SDL_Surface and not a SDL_Texture, this
154  * is part of the SDL 2 API.
155  *
156  * @param icon The new icon for the window.
157  */
158  void set_icon(const surface& icon);
159 
160  uint32_t get_flags();
161 
162  /**
163  * Set minimum size of the window.
164  *
165  * This is a wrapper for SDL_SetWindowMinimumWindowSize.
166  */
167  void set_minimum_size(int min_w, int min_h);
168 
169  int get_display_index();
170 
171  /***** ***** ***** Conversion operators. ***** ***** *****/
172 
173  /**
174  * Conversion operator to a SDL_Window*.
175  */
176  operator SDL_Window*();
177 
178  /**
179  * Conversion operator to a SDL_Renderer*.
180  */
181  operator SDL_Renderer*();
182 
183 private:
184  /***** ***** ***** Members. ***** ***** *****/
185 
186  /** The SDL_Window we own. */
187  SDL_Window* window_;
188 
189  /** The preferred pixel format for the renderer. */
190  uint32_t pixel_format_;
191 };
192 
193 } // namespace sdl
void full_screen()
Dummy function for setting the window to fullscreen mode.
Definition: window.cpp:122
void set_size(const int w, const int h)
Wrapper for SDL_SetWindowSize.
Definition: window.cpp:81
void restore()
Dummy function for restoring the window.
Definition: window.cpp:117
#define a
uint32_t pixel_format_
The preferred pixel format for the renderer.
Definition: window.hpp:190
#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:127
#define b
uint32_t get_flags()
Definition: window.cpp:151
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:146
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:107
SDL_Point get_size()
Gets the window&#39;s size, in screen coordinates.
Definition: window.cpp:86
SDL_Point get_output_size()
Gets the window&#39;s renderer output size, in pixels.
Definition: window.cpp:94
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:141
SDL_Window * window_
The SDL_Window we own.
Definition: window.hpp:187
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:156
void render()
Renders the contents of the window.
Definition: window.cpp:136
void to_window()
Dummy function for returning the window to windowed mode.
Definition: window.cpp:112
int get_display_index()
Definition: window.cpp:161
void center()
Dummy function for centering the window.
Definition: window.cpp:102