/
window.cpp
219 lines (175 loc) · 4.52 KB
/
window.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
Copyright (C) 2014 - 2022
by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#include "sdl/window.hpp"
#include "sdl/exception.hpp"
#include "sdl/input.hpp"
#include "sdl/surface.hpp"
#include "sdl/utils.hpp"
#include <SDL2/SDL_render.h>
namespace sdl
{
window::window(const std::string& title,
const int x,
const int y,
const int w,
const int h,
const uint32_t window_flags,
const uint32_t render_flags)
: window_(SDL_CreateWindow(
title.c_str(), x, y, w, h, window_flags | SDL_WINDOW_HIDDEN))
, pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
{
if(!window_) {
throw exception("Failed to create a SDL_Window object.", true);
}
if(sdl::runtime_at_least(2,0,10)) {
// Rendering in batches (for efficiency) is enabled by default from SDL 2.0.10
// The way Wesnoth uses SDL as of September 2019 does not work well with this rendering mode (eg story-only scenarios)
SDL_SetHint(SDL_HINT_RENDER_BATCHING, "0");
}
if(!SDL_CreateRenderer(window_, -1, render_flags)) {
throw exception("Failed to create a SDL_Renderer object.", true);
}
SDL_RendererInfo info;
if(SDL_GetRendererInfo(*this, &info) != 0) {
throw exception("Failed to retrieve the information of the renderer.",
true);
}
if(info.num_texture_formats == 0) {
throw exception("The renderer has no texture information available.\n",
false);
}
if((info.flags & SDL_RENDERER_TARGETTEXTURE) == 0) {
throw exception("Render-to-texture not supported or enabled!", false);
}
// Set default blend mode to blend.
SDL_SetRenderDrawBlendMode(*this, SDL_BLENDMODE_BLEND);
// In fullscreen mode, do not minimize on focus loss.
// Minimizing was reported as bug #1606 with blocker priority.
SDL_SetHint(SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
pixel_format_ = info.texture_formats[0];
fill(0,0,0);
render();
// If we didn't explicitly ask for the window to be hidden, show it
if(!(window_flags & SDL_WINDOW_HIDDEN)) {
SDL_ShowWindow(window_);
}
}
window::~window()
{
if(window_) {
SDL_DestroyWindow(window_);
}
}
void window::set_size(const int w, const int h)
{
SDL_SetWindowSize(window_, w, h);
}
SDL_Point window::get_size()
{
SDL_Point res;
SDL_GetWindowSize(*this, &res.x, &res.y);
return res;
}
SDL_Point window::get_output_size()
{
SDL_Point res;
SDL_GetRendererOutputSize(*this, &res.x, &res.y);
return res;
}
void window::center()
{
SDL_SetWindowPosition(window_, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}
void window::maximize()
{
SDL_MaximizeWindow(window_);
}
void window::to_window()
{
SDL_SetWindowFullscreen(window_, 0);
}
void window::restore()
{
SDL_RestoreWindow(window_);
}
void window::full_screen()
{
SDL_SetWindowFullscreen(window_, SDL_WINDOW_FULLSCREEN_DESKTOP);
}
void window::fill(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
SDL_SetRenderDrawColor(*this, r, g, b, a);
if(SDL_RenderClear(*this) != 0) {
throw exception("Failed to clear the SDL_Renderer object.",
true);
}
}
void window::render()
{
SDL_RenderPresent(*this);
}
void window::set_title(const std::string& title)
{
SDL_SetWindowTitle(window_, title.c_str());
}
void window::set_icon(const surface& icon)
{
SDL_SetWindowIcon(window_, icon);
}
uint32_t window::get_flags()
{
return SDL_GetWindowFlags(window_);
}
void window::set_minimum_size(int min_w, int min_h)
{
SDL_SetWindowMinimumSize(window_, min_w, min_h);
}
int window::get_display_index()
{
return SDL_GetWindowDisplayIndex(window_);
}
void window::set_logical_size(int w, int h)
{
SDL_Renderer* r = SDL_GetRenderer(window_);
SDL_RenderSetLogicalSize(r, w, h);
}
void window::set_logical_size(const point& p)
{
set_logical_size(p.x, p.y);
}
point window::get_logical_size() const
{
SDL_Renderer* r = SDL_GetRenderer(window_);
int w, h;
SDL_RenderGetLogicalSize(r, &w, &h);
return {w, h};
}
void window::get_logical_size(int& w, int& h) const
{
SDL_Renderer* r = SDL_GetRenderer(window_);
SDL_RenderGetLogicalSize(r, &w, &h);
}
uint32_t window::pixel_format()
{
return pixel_format_;
}
window::operator SDL_Window*()
{
return window_;
}
window::operator SDL_Renderer*()
{
return SDL_GetRenderer(window_);
}
} // namespace sdl