The Battle for Wesnoth  1.19.24+dev
window.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2025
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 <SDL3/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 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  */
65  window(const std::string& title,
66  const int w,
67  const int h,
68  const uint32_t window_flags);
69 
70  ~window();
71 
72 
73  /***** ***** ***** Operations. ***** ***** *****/
74 
75  /**
76  * Wrapper for SDL_SetWindowSize.
77  *
78  * @param w Used as w for SDL_SetWindowSize.
79  * @param h Used as x for SDL_SetWindowSize.
80  */
81  void set_size(const int w, const int h);
82 
83  /**
84  * Gets the window's size, in screen coordinates.
85  *
86  * For the most part, this seems to return the same result as @ref get_output_size. However,
87  * SDL indicates for high DPI displays these two functions could differ. I could not observe
88  * any change in behavior with DPI virtualization on or off, but to be safe, I'm keeping the
89  * two functions separate and using this for matters of window resolution.
90  *
91  * - vultraz, 6/27/2017
92  */
93  point get_size();
94 
95  /** Gets the window's renderer output size, in pixels */
97 
98  /**
99  * Dummy function for centering the window.
100  */
101  void center();
102 
103  /**
104  * Dummy function for maximizing the window.
105  */
106  void maximize();
107 
108  /**
109  * Dummy function for restoring the window.
110  */
111  void restore();
112 
113  /**
114  * Dummy function for returning the window to windowed mode.
115  */
116  void to_window();
117 
118  /**
119  * Dummy function for setting the window to fullscreen mode.
120  */
121  void full_screen();
122 
123  /**
124  * Clears the contents of the window with a given color.
125  *
126  * @param r Red value of the color.
127  * @param g Green value of the color.
128  * @param b Blue value of the color.
129  * @param a Alpha value.
130  */
131  void fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a = 0);
132 
133  /** Renders the contents of the window. */
134  void render();
135 
136  /**
137  * Sets the title of the window.
138  *
139  * This is a wrapper for SDL_SetWindowTitle.
140  *
141  * @param title The new title for the window.
142  */
143  void set_title(const std::string& title);
144 
145  /**
146  * Sets the icon of the window.
147  *
148  * This is a wrapper for SDL_SetWindowIcon.
149  *
150  * @note The @p icon is a SDL_Surface and not a SDL_Texture, this
151  * is part of the SDL 2 API.
152  *
153  * @param icon The new icon for the window.
154  */
155  void set_icon(const surface& icon);
156 
157  uint32_t get_flags();
158 
159  /**
160  * Set minimum size of the window.
161  *
162  * This is a wrapper for SDL_SetWindowMinimumWindowSize.
163  */
164  void set_minimum_size(int min_w, int min_h);
165 
166  int get_display_index();
167 
168  /**
169  * Sets the desired size of the rendering surface. Input event coordinates
170  * will be scaled as if the window were also of this size. For best
171  * results this should be an integer fraction of the window size.
172  *
173  * This is a wrapper for SDL_SetRenderLogicalPresentation.
174  *
175  * @param w Width of the window's rendering surface
176  * @param h Height of the window's rendering surface
177  */
178  void set_logical_size(int w, int h);
179  void set_logical_size(const point& p);
180 
181  point get_logical_size() const;
182  void get_logical_size(int& w, int& h) const;
183 
184  /** The current pixel format of the renderer. */
185 
186  /***** ***** ***** Conversion operators. ***** ***** *****/
187 
188  /**
189  * Conversion operator to a SDL_Window*.
190  */
191  operator SDL_Window*();
192 
193  /**
194  * Conversion operator to a SDL_Renderer*.
195  */
196  operator SDL_Renderer*();
197 
198 private:
199  /***** ***** ***** Members. ***** ***** *****/
200 
201  /** The SDL_Window we own. */
202  SDL_Window* window_;
203 };
204 
205 } // namespace sdl
double g
Definition: astarsearch.cpp:63
The wrapper class for the SDL_Window class.
Definition: window.hpp:48
void set_size(const int w, const int h)
Wrapper for SDL_SetWindowSize.
Definition: window.cpp:99
void to_window()
Dummy function for returning the window to windowed mode.
Definition: window.cpp:143
window & operator=(const window &)=delete
window(const window &)=delete
point get_size()
Gets the window's size, in screen coordinates.
Definition: window.cpp:109
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:164
point get_logical_size() const
Definition: window.cpp:222
void center()
Dummy function for centering the window.
Definition: window.cpp:129
void render()
Renders the contents of the window.
Definition: window.cpp:172
void restore()
Dummy function for restoring the window.
Definition: window.cpp:150
void set_minimum_size(int min_w, int min_h)
Set minimum size of the window.
Definition: window.cpp:188
SDL_Window * window_
The SDL_Window we own.
Definition: window.hpp:202
void full_screen()
Dummy function for setting the window to fullscreen mode.
Definition: window.cpp:157
int get_display_index()
Definition: window.cpp:203
void set_icon(const surface &icon)
Sets the icon of the window.
Definition: window.cpp:183
void set_logical_size(int w, int h)
Sets the desired size of the rendering surface.
Definition: window.cpp:208
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:136
uint32_t get_flags()
Definition: window.cpp:198
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:178
point get_output_size()
Gets the window's renderer output size, in pixels.
Definition: window.cpp:121
int w
Definition: pathfind.cpp:188
Holds a 2D point.
Definition: point.hpp:25
mock_party p
#define h
#define b