The Battle for Wesnoth  1.17.4+dev
window.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2022
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/surface.hpp"
17 #include "sdl/window.hpp"
18 #include "sdl/input.hpp"
19 
20 #include "sdl/exception.hpp"
21 
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(title.c_str(), x, y, w, h, window_flags))
35  , pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
36 {
37  if(!window_) {
38  throw exception("Failed to create a SDL_Window object.", true);
39  }
40 
41 #if SDL_VERSION_ATLEAST(2, 0, 10)
42  // Rendering in batches (for efficiency) is enabled by default from SDL 2.0.10
43  // The way Wesnoth uses SDL as of September 2019 does not work well with this rendering mode (eg story-only scenarios)
44  SDL_SetHint(SDL_HINT_RENDER_BATCHING, "0");
45 #endif
46 
47  if(!SDL_CreateRenderer(window_, -1, render_flags)) {
48  throw exception("Failed to create a SDL_Renderer object.", true);
49  }
50 
51  SDL_RendererInfo info;
52  if(SDL_GetRendererInfo(*this, &info) != 0) {
53  throw exception("Failed to retrieve the information of the renderer.",
54  true);
55  }
56 
57  if(info.num_texture_formats == 0) {
58  throw exception("The renderer has no texture information available.\n",
59  false);
60  }
61 
62  if((info.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {
63  throw exception("Render-to-texture not supported or enabled!", false);
64  }
65 
66  // Set default blend mode to blend.
67  SDL_SetRenderDrawBlendMode(*this, SDL_BLENDMODE_BLEND);
68 
69  // In fullscreen mode, do not minimize on focus loss.
70  // Minimizing was reported as bug #1606 with blocker priority.
71  SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
72 
73  pixel_format_ = info.texture_formats[0];
74 
75  fill(0,0,0);
76 
77  // Now that we have a window and renderer we can scale input correctly.
79 
80  render();
81 }
82 
84 {
85  if(window_) {
86  SDL_DestroyWindow(window_);
87  }
88 }
89 
90 void window::set_size(const int w, const int h)
91 {
92  SDL_SetWindowSize(window_, w, h);
94 }
95 
96 SDL_Point window::get_size()
97 {
98  SDL_Point res;
99  SDL_GetWindowSize(*this, &res.x, &res.y);
100 
101  return res;
102 }
103 
105 {
106  SDL_Point res;
107  SDL_GetRendererOutputSize(*this, &res.x, &res.y);
108 
109  return res;
110 }
111 
113 {
114  SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
115 }
116 
118 {
119  SDL_MaximizeWindow(window_);
121 }
122 
124 {
125  SDL_SetWindowFullscreen(window_, 0);
127 }
128 
130 {
131  SDL_RestoreWindow(window_);
133 }
134 
136 {
137  SDL_SetWindowFullscreen(window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
139 }
140 
141 void window::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
142 {
143  SDL_SetRenderDrawColor(*this, r, g, b, a);
144  if(SDL_RenderClear(*this) != 0) {
145  throw exception("Failed to clear the SDL_Renderer object.",
146  true);
147  }
148 }
149 
151 {
152  SDL_RenderPresent(*this);
153 }
154 
155 void window::set_title(const std::string& title)
156 {
157  SDL_SetWindowTitle(window_, title.c_str());
158 }
159 
160 void window::set_icon(const surface& icon)
161 {
162  SDL_SetWindowIcon(window_, icon);
163 }
164 
166 {
167  return SDL_GetWindowFlags(window_);
168 }
169 
170 void window::set_minimum_size(int min_w, int min_h)
171 {
172  SDL_SetWindowMinimumSize(window_, min_w, min_h);
173  // Can this change the size of the window?
175 }
176 
178 {
179  return SDL_GetWindowDisplayIndex(window_);
180 }
181 
183 {
184  SDL_Renderer* r = SDL_GetRenderer(window_);
185  SDL_RenderSetLogicalSize(r, w, h);
187 }
188 
189 SDL_Point window::get_logical_size() const
190 {
191  SDL_Renderer* r = SDL_GetRenderer(window_);
192  int w, h;
193  SDL_RenderGetLogicalSize(r, &w, &h);
194  return {w, h};
195 }
196 
197 void window::get_logical_size(int& w, int& h) const
198 {
199  SDL_Renderer* r = SDL_GetRenderer(window_);
200  SDL_RenderGetLogicalSize(r, &w, &h);
201 }
202 
204 {
205  return pixel_format_;
206 }
207 
208 window::operator SDL_Window*()
209 {
210  return window_;
211 }
212 
213 window::operator SDL_Renderer*()
214 {
215  return SDL_GetRenderer(window_);
216 }
217 
218 } // namespace sdl
void set_logical_size(int w, int h)
Sets the desired size of the rendering surface.
Definition: window.cpp:182
void full_screen()
Dummy function for setting the window to fullscreen mode.
Definition: window.cpp:135
void set_size(const int w, const int h)
Wrapper for SDL_SetWindowSize.
Definition: window.cpp:90
void restore()
Dummy function for restoring the window.
Definition: window.cpp:129
logger & info()
Definition: log.cpp:89
#define a
uint32_t pixel_format()
The current pixel format of the renderer.
Definition: window.cpp:203
uint32_t pixel_format_
The preferred pixel format for the renderer.
Definition: window.hpp:209
void update_input_dimensions(int draw_width, int draw_height, int input_width, int input_height)
Update the cached drawing area and input area sizes.
Definition: input.cpp:57
#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:141
Contains functions for cleanly handling SDL input.
#define b
uint32_t get_flags()
Definition: window.cpp:165
void set_icon(const surface &icon)
Sets the icon of the window.
Definition: window.cpp:160
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:117
SDL_Point get_size()
Gets the window&#39;s size, in screen coordinates.
Definition: window.cpp:96
SDL_Point get_output_size()
Gets the window&#39;s renderer output size, in pixels.
Definition: window.cpp:104
SDL_Point get_logical_size() const
Definition: window.cpp:189
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:155
SDL_Window * window_
The SDL_Window we own.
Definition: window.hpp:206
double g
Definition: astarsearch.cpp:65
window(const window &)=delete
int w
Contains a basic exception class for SDL operations.
void set_minimum_size(int min_w, int min_h)
Set minimum size of the window.
Definition: window.cpp:170
Contains a wrapper class for the SDL_Window class.
void render()
Renders the contents of the window.
Definition: window.cpp:150
void to_window()
Dummy function for returning the window to windowed mode.
Definition: window.cpp:123
int get_display_index()
Definition: window.cpp:177
void center()
Dummy function for centering the window.
Definition: window.cpp:112