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