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