-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
window.cpp
134 lines (106 loc) · 2.81 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
/*
Copyright (C) 2014 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"
#if SDL_VERSION_ATLEAST(2, 0, 0)
#include "sdl/exception.hpp"
#include "sdl/texture.hpp"
#include <SDL_render.h>
namespace sdl
{
twindow::twindow(const std::string& title,
const int x,
const int y,
const int w,
const int h,
const Uint32 window_flags,
const Uint32 render_flags)
: window_(SDL_CreateWindow(title.c_str(), x, y, w, h, window_flags))
, pixel_format_(SDL_PIXELFORMAT_UNKNOWN)
{
if(!window_) {
throw texception("Failed to create a SDL_Window object.", true);
}
if(!SDL_CreateRenderer(window_, -1, render_flags)) {
throw texception("Failed to create a SDL_Renderer object.", true);
}
SDL_RendererInfo info;
if(SDL_GetRendererInfo(*this, &info) != 0) {
throw texception("Failed to retrieve the information of the renderer.",
true);
}
if(info.num_texture_formats == 0) {
throw texception("The renderer has no texture information available.\n",
false);
}
pixel_format_ = info.texture_formats[0];
clear();
}
twindow::~twindow()
{
if(window_) {
SDL_DestroyWindow(window_);
}
}
void twindow::set_size(const int w, const int h)
{
SDL_SetWindowSize(window_, w, h);
}
void twindow::full_screen()
{
/** @todo Implement. */
}
void twindow::clear()
{
if(SDL_RenderClear(*this) != 0) {
throw texception("Failed to clear the SDL_Renderer object.",
true);
}
}
void twindow::render()
{
SDL_RenderPresent(*this);
}
void twindow::set_title(const std::string& title)
{
SDL_SetWindowTitle(window_, title.c_str());
}
void twindow::set_icon(const surface& icon)
{
SDL_SetWindowIcon(window_, icon);
}
ttexture twindow::create_texture(const int access, const int w, const int h)
{
return ttexture(*SDL_GetRenderer(window_), pixel_format_, access, w, h);
}
ttexture twindow::create_texture(const int access,
SDL_Surface* source_surface__)
{
return ttexture(*SDL_GetRenderer(window_), access, source_surface__);
}
ttexture twindow::create_texture(const int access, const surface& surface)
{
return ttexture(*SDL_GetRenderer(window_), access, surface);
}
void twindow::draw(ttexture& texture, const int x, const int y)
{
texture.draw(*SDL_GetRenderer(window_), x, y);
}
twindow::operator SDL_Window*()
{
return window_;
}
twindow::operator SDL_Renderer*()
{
return SDL_GetRenderer(window_);
}
} // namespace sdl
#endif