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