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