The Battle for Wesnoth  1.15.2+dev
window.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "sdl/surface.hpp"
16 #include "sdl/window.hpp"
17 
18 #include "sdl/exception.hpp"
19 
20 #include <SDL2/SDL_render.h>
21 
22 namespace sdl
23 {
24 
25 window::window(const std::string& title,
26  const int x,
27  const int y,
28  const int w,
29  const int h,
30  const uint32_t window_flags,
31  const uint32_t render_flags)
32  : window_(SDL_CreateWindow(title.c_str(), x, y, w, h, window_flags))
33  , pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
34 {
35  if(!window_) {
36  throw exception("Failed to create a SDL_Window object.", true);
37  }
38 
39 #if SDL_VERSION_ATLEAST(2, 0, 10)
40  // Rendering in batches (for efficiency) is enabled by default from SDL 2.0.10
41  // The way Wesnoth uses SDL as of September 2019 does not work well with this rendering mode (eg story-only scenarios)
42  SDL_SetHint(SDL_HINT_RENDER_BATCHING, "0");
43 #endif
44 
45  if(!SDL_CreateRenderer(window_, -1, render_flags)) {
46  throw exception("Failed to create a SDL_Renderer object.", true);
47  }
48 
49  SDL_RendererInfo info;
50  if(SDL_GetRendererInfo(*this, &info) != 0) {
51  throw exception("Failed to retrieve the information of the renderer.",
52  true);
53  }
54 
55  if(info.num_texture_formats == 0) {
56  throw exception("The renderer has no texture information available.\n",
57  false);
58  }
59 
60  // Set default blend mode to blend.
61  SDL_SetRenderDrawBlendMode(*this, SDL_BLENDMODE_BLEND);
62 
63  // In fullscreen mode, do not minimize on focus loss.
64  // Minimizing was reported as bug #1606 with blocker priority.
65  SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
66 
67  pixel_format_ = info.texture_formats[0];
68 
69  fill(0,0,0);
70 
71  render();
72 }
73 
75 {
76  if(window_) {
77  SDL_DestroyWindow(window_);
78  }
79 }
80 
81 void window::set_size(const int w, const int h)
82 {
83  SDL_SetWindowSize(window_, w, h);
84 }
85 
86 SDL_Point window::get_size()
87 {
88  SDL_Point res;
89  SDL_GetWindowSize(*this, &res.x, &res.y);
90 
91  return res;
92 }
93 
95 {
96  SDL_Point res;
97  SDL_GetRendererOutputSize(*this, &res.x, &res.y);
98 
99  return res;
100 }
101 
103 {
104  SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
105 }
106 
108 {
109  SDL_MaximizeWindow(window_);
110 }
111 
113 {
114  SDL_SetWindowFullscreen(window_, 0);
115 }
116 
118 {
119  SDL_RestoreWindow(window_);
120 }
121 
123 {
124  SDL_SetWindowFullscreen(window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
125 }
126 
127 void window::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
128 {
129  SDL_SetRenderDrawColor(*this, r, g, b, a);
130  if(SDL_RenderClear(*this) != 0) {
131  throw exception("Failed to clear the SDL_Renderer object.",
132  true);
133  }
134 }
135 
137 {
138  SDL_RenderPresent(*this);
139 }
140 
141 void window::set_title(const std::string& title)
142 {
143  SDL_SetWindowTitle(window_, title.c_str());
144 }
145 
146 void window::set_icon(const surface& icon)
147 {
148  SDL_SetWindowIcon(window_, icon);
149 }
150 
152 {
153  return SDL_GetWindowFlags(window_);
154 }
155 
156 void window::set_minimum_size(int min_w, int min_h)
157 {
158  SDL_SetWindowMinimumSize(window_, min_w, min_h);
159 }
160 
162 {
163  return SDL_GetWindowDisplayIndex(window_);
164 }
165 
166 window::operator SDL_Window*()
167 {
168  return window_;
169 }
170 
171 window::operator SDL_Renderer*()
172 {
173  return SDL_GetRenderer(window_);
174 }
175 
176 } // namespace sdl
void full_screen()
Dummy function for setting the window to fullscreen mode.
Definition: window.cpp:122
void set_size(const int w, const int h)
Wrapper for SDL_SetWindowSize.
Definition: window.cpp:81
void restore()
Dummy function for restoring the window.
Definition: window.cpp:117
logger & info()
Definition: log.cpp:90
#define a
uint32_t pixel_format_
The preferred pixel format for the renderer.
Definition: window.hpp:189
#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:127
#define b
uint32_t get_flags()
Definition: window.cpp:151
void set_icon(const surface &icon)
Sets the icon of the window.
Definition: window.cpp:146
void maximize()
Dummy function for maximizing the window.
Definition: window.cpp:107
SDL_Point get_size()
Gets the window&#39;s size, in screen coordinates.
Definition: window.cpp:86
SDL_Point get_output_size()
Gets the window&#39;s renderer output size, in pixels.
Definition: window.cpp:94
void set_title(const std::string &title)
Sets the title of the window.
Definition: window.cpp:141
SDL_Window * window_
The SDL_Window we own.
Definition: window.hpp:186
double g
Definition: astarsearch.cpp:64
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:156
Contains a wrapper class for the SDL_Window class.
void render()
Renders the contents of the window.
Definition: window.cpp:136
void to_window()
Dummy function for returning the window to windowed mode.
Definition: window.cpp:112
int get_display_index()
Definition: window.cpp:161
void center()
Dummy function for centering the window.
Definition: window.cpp:102