/
window.cpp
177 lines (142 loc) · 3.51 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
/*
Copyright (C) 2014 - 2018 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://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 "ogl/utils.hpp"
#include "sdl/exception.hpp"
#include "sdl/render_utils.hpp"
#include "sdl/surface.hpp"
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))
, info_()
{
if(!window_) {
throw exception("Failed to create a SDL_Window object.", true);
}
#ifndef USE_GL_RENDERING
if(!SDL_CreateRenderer(window_, -1, render_flags)) {
throw exception("Failed to create a SDL_Renderer object.", true);
}
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)) {
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");
fill(0,0,0);
render();
#endif
}
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)
{
set_draw_color(*this, r, g, b, a);
if(SDL_RenderClear(*this) != 0) {
throw exception("Failed to clear the SDL_Renderer object.",
true);
}
}
void window::render()
{
#ifdef USE_GL_RENDERING
SDL_GL_SwapWindow(*this);
#else
SDL_RenderPresent(*this);
#endif
}
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_);
}
window::operator SDL_Window*()
{
return window_;
}
window::operator SDL_Renderer*()
{
return SDL_GetRenderer(window_);
}
} // namespace sdl