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