The Battle for Wesnoth  1.19.10+dev
window.cpp
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 #include "sdl/window.hpp"
17 
18 #include "sdl/exception.hpp"
19 #include "sdl/surface.hpp"
20 
21 #include <SDL2/SDL_hints.h>
22 #include <SDL2/SDL_render.h>
23 
24 namespace sdl
25 {
26 
27 window::window(const std::string& title,
28  const int x,
29  const int y,
30  const int w,
31  const int h,
32  const uint32_t window_flags,
33  const uint32_t render_flags)
34  : window_(SDL_CreateWindow(
35  title.c_str(), x, y, w, h, window_flags | SDL_WINDOW_HIDDEN))
36  , pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
37 {
38  if(!window_) {
39  throw exception("Failed to create a SDL_Window object.", true);
40  }
41 
42 #ifdef _WIN32
43  // SDL uses Direct3D v9 by default on Windows systems. However, returning
44  // from the Windows lock screen causes issues with rendering. Resolution
45  // is either to rebuild render textures on the SDL_RENDER_TARGETS_RESET
46  // event or use an alternative renderer that does not have this issue.
47  // Suitable options are Direct3D v11+ or OpenGL.
48  // See https://github.com/wesnoth/wesnoth/issues/8038 for details.
49  // Note that SDL_HINT_RENDER_DRIVER implies SDL_HINT_RENDER_BATCHING is
50  // disabled, according to https://discourse.libsdl.org/t/a-couple-of-questions-regarding-batching-in-sdl-2-0-10/26453/2.
51  SDL_SetHint(SDL_HINT_RENDER_DRIVER, "direct3d11");
52 #endif
53 
54  if(!SDL_CreateRenderer(window_, -1, render_flags)) {
55  throw exception("Failed to create a SDL_Renderer object.", true);
56  }
57 
58  SDL_RendererInfo info;
59  if(SDL_GetRendererInfo(*this, &info) != 0) {
60  throw exception("Failed to retrieve the information of the renderer.",
61  true);
62  }
63 
64  if(info.num_texture_formats == 0) {
65  throw exception("The renderer has no texture information available.\n",
66  false);
67  }
68 
69  if((info.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {
70  throw exception("Render-to-texture not supported or enabled!", false);
71  }
72 
73  // Set default blend mode to blend.
74  SDL_SetRenderDrawBlendMode(*this, SDL_BLENDMODE_BLEND);
75 
76  // In fullscreen mode, do not minimize on focus loss.
77  // Minimizing was reported as bug #1606 with blocker priority.
78  SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
79 
80  pixel_format_ = info.texture_formats[0];
81 
82  fill(0,0,0);
83 
84  render();
85 
86  // If we didn't explicitly ask for the window to be hidden, show it
87  if(!(window_flags & SDL_WINDOW_HIDDEN)) {
88  SDL_ShowWindow(window_);
89  }
90 }
91 
93 {
94  if(window_) {
95  SDL_DestroyWindow(window_);
96  }
97 }
98 
99 void window::set_size(const int w, const int h)
100 {
101  SDL_SetWindowSize(window_, w, h);
102 }
103 
104 SDL_Point window::get_size()
105 {
106  SDL_Point res;
107  SDL_GetWindowSize(*this, &res.x, &res.y);
108 
109  return res;
110 }
111 
113 {
114  SDL_Point res;
115  SDL_GetRendererOutputSize(*this, &res.x, &res.y);
116 
117  return res;
118 }
119 
121 {
122  SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
123 }
124 
126 {
127  SDL_MaximizeWindow(window_);
128 }
129 
131 {
132  SDL_SetWindowFullscreen(window_, 0);
133 }
134 
136 {
137  SDL_RestoreWindow(window_);
138 }
139 
141 {
142  SDL_SetWindowFullscreen(window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
143 }
144 
145 void window::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
146 {
147  SDL_SetRenderDrawColor(*this, r, g, b, a);
148  if(SDL_RenderClear(*this) != 0) {
149  throw exception("Failed to clear the SDL_Renderer object.",
150  true);
151  }
152 }
153 
155 {
156  SDL_RenderPresent(*this);
157 }
158 
159 void window::set_title(const std::string& title)
160 {
161  SDL_SetWindowTitle(window_, title.c_str());
162 }
163 
164 void window::set_icon(const surface& icon)
165 {
166  SDL_SetWindowIcon(window_, icon);
167 }
168 
170 {
171  return SDL_GetWindowFlags(window_);
172 }
173 
174 void window::set_minimum_size(int min_w, int min_h)
175 {
176  SDL_SetWindowMinimumSize(window_, min_w, min_h);
177 }
178 
180 {
181  return SDL_GetWindowDisplayIndex(window_);
182 }
183 
185 {
186  SDL_Renderer* r = SDL_GetRenderer(window_);
187  SDL_RenderSetLogicalSize(r, w, h);
188 }
189 
191 {
192  set_logical_size(p.x, p.y);
193 }
194 
196 {
197  SDL_Renderer* r = SDL_GetRenderer(window_);
198  int w, h;
199  SDL_RenderGetLogicalSize(r, &w, &h);
200  return {w, h};
201 }
202 
203 void window::get_logical_size(int& w, int& h) const
204 {
205  SDL_Renderer* r = SDL_GetRenderer(window_);
206  SDL_RenderGetLogicalSize(r, &w, &h);
207 }
208 
210 {
211  return pixel_format_;
212 }
213 
214 window::operator SDL_Window*()
215 {
216  return window_;
217 }
218 
219 window::operator SDL_Renderer*()
220 {
221  return SDL_GetRenderer(window_);
222 }
223 
224 } // namespace sdl
double g
Definition: astarsearch.cpp:63
SDL_Point get_output_size()
Gets the window's renderer output size, in pixels.
Definition: window.cpp:112
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:130
uint32_t pixel_format()
The current pixel format of the renderer.
Definition: window.cpp:209
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:145
point get_logical_size() const
Definition: window.cpp:195
void center()
Dummy function for centering the window.
Definition: window.cpp:120
void render()
Renders the contents of the window.
Definition: window.cpp:154
void restore()
Dummy function for restoring the window.
Definition: window.cpp:135
void set_minimum_size(int min_w, int min_h)
Set minimum size of the window.
Definition: window.cpp:174
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:140
int get_display_index()
Definition: window.cpp:179
SDL_Point get_size()
Gets the window's size, in screen coordinates.
Definition: window.cpp:104
void set_icon(const surface &icon)
Sets the icon of the window.
Definition: window.cpp:164
void set_logical_size(int w, int h)
Sets the desired size of the rendering surface.
Definition: window.cpp:184
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:125
uint32_t pixel_format_
The preferred pixel format for the renderer.
Definition: window.hpp:212
uint32_t get_flags()
Definition: window.cpp:169
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:159
int w
logger & info()
Definition: log.cpp:318
Contains a basic exception class for SDL operations.
Contains a wrapper class for the SDL_Window class.
Holds a 2D point.
Definition: point.hpp:25
mock_party p
#define h
#define b