The Battle for Wesnoth  1.17.0-dev
window.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2021
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 
19 #include "sdl/exception.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(title.c_str(), x, y, w, h, window_flags))
34  , pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
35 {
36  if(!window_) {
37  throw exception("Failed to create a SDL_Window object.", true);
38  }
39 
40 #if SDL_VERSION_ATLEAST(2, 0, 10)
41  // Rendering in batches (for efficiency) is enabled by default from SDL 2.0.10
42  // The way Wesnoth uses SDL as of September 2019 does not work well with this rendering mode (eg story-only scenarios)
43  SDL_SetHint(SDL_HINT_RENDER_BATCHING, "0");
44 #endif
45 
46  if(!SDL_CreateRenderer(window_, -1, render_flags)) {
47  throw exception("Failed to create a SDL_Renderer object.", true);
48  }
49 
50  SDL_RendererInfo info;
51  if(SDL_GetRendererInfo(*this, &info) != 0) {
52  throw exception("Failed to retrieve the information of the renderer.",
53  true);
54  }
55 
56  if(info.num_texture_formats == 0) {
57  throw exception("The renderer has no texture information available.\n",
58  false);
59  }
60 
61  // Set default blend mode to blend.
62  SDL_SetRenderDrawBlendMode(*this, SDL_BLENDMODE_BLEND);
63 
64  // In fullscreen mode, do not minimize on focus loss.
65  // Minimizing was reported as bug #1606 with blocker priority.
66  SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
67 
68  pixel_format_ = info.texture_formats[0];
69 
70  fill(0,0,0);
71 
72  render();
73 }
74 
76 {
77  if(window_) {
78  SDL_DestroyWindow(window_);
79  }
80 }
81 
82 void window::set_size(const int w, const int h)
83 {
84  SDL_SetWindowSize(window_, w, h);
85 }
86 
87 SDL_Point window::get_size()
88 {
89  SDL_Point res;
90  SDL_GetWindowSize(*this, &res.x, &res.y);
91 
92  return res;
93 }
94 
96 {
97  SDL_Point res;
98  SDL_GetRendererOutputSize(*this, &res.x, &res.y);
99 
100  return res;
101 }
102 
104 {
105  SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
106 }
107 
109 {
110  SDL_MaximizeWindow(window_);
111 }
112 
114 {
115  SDL_SetWindowFullscreen(window_, 0);
116 }
117 
119 {
120  SDL_RestoreWindow(window_);
121 }
122 
124 {
125  SDL_SetWindowFullscreen(window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
126 }
127 
128 void window::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
129 {
130  SDL_SetRenderDrawColor(*this, r, g, b, a);
131  if(SDL_RenderClear(*this) != 0) {
132  throw exception("Failed to clear the SDL_Renderer object.",
133  true);
134  }
135 }
136 
138 {
139  SDL_RenderPresent(*this);
140 }
141 
142 void window::set_title(const std::string& title)
143 {
144  SDL_SetWindowTitle(window_, title.c_str());
145 }
146 
147 void window::set_icon(const surface& icon)
148 {
149  SDL_SetWindowIcon(window_, icon);
150 }
151 
153 {
154  return SDL_GetWindowFlags(window_);
155 }
156 
157 void window::set_minimum_size(int min_w, int min_h)
158 {
159  SDL_SetWindowMinimumSize(window_, min_w, min_h);
160 }
161 
163 {
164  return SDL_GetWindowDisplayIndex(window_);
165 }
166 
167 window::operator SDL_Window*()
168 {
169  return window_;
170 }
171 
172 window::operator SDL_Renderer*()
173 {
174  return SDL_GetRenderer(window_);
175 }
176 
177 } // namespace sdl
void full_screen()
Dummy function for setting the window to fullscreen mode.
Definition: window.cpp:123
void set_size(const int w, const int h)
Wrapper for SDL_SetWindowSize.
Definition: window.cpp:82
void restore()
Dummy function for restoring the window.
Definition: window.cpp:118
logger & info()
Definition: log.cpp:89
#define a
uint32_t pixel_format_
The preferred pixel format for the renderer.
Definition: window.hpp:191
#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:128
#define b
uint32_t get_flags()
Definition: window.cpp:152
void set_icon(const surface &icon)
Sets the icon of the window.
Definition: window.cpp:147
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:108
SDL_Point get_size()
Gets the window&#39;s size, in screen coordinates.
Definition: window.cpp:87
SDL_Point get_output_size()
Gets the window&#39;s renderer output size, in pixels.
Definition: window.cpp:95
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:142
SDL_Window * window_
The SDL_Window we own.
Definition: window.hpp:188
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:157
Contains a wrapper class for the SDL_Window class.
void render()
Renders the contents of the window.
Definition: window.cpp:137
void to_window()
Dummy function for returning the window to windowed mode.
Definition: window.cpp:113
int get_display_index()
Definition: window.cpp:162
void center()
Dummy function for centering the window.
Definition: window.cpp:103