/
PluSDL.cpp
206 lines (166 loc) · 4.7 KB
/
PluSDL.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
#include "PluSDL.h"
AppRect::AppRect(int w, int h, int x, int y) {
rect.w = w;
rect.h = h;
rect.x = x;
rect.y = y;
}
void AppRect::resize(int w, int h) {
rect.w = w;
rect.h = h;
}
void AppRect::updatePosition(int x, int y) {
rect.x = x;
rect.y = y;
}
AppSurface::AppSurface(int width, int height, int depth, Uint32 rmask, Uint32 gmask, Uint32 bmask, Uint32 amask) :
surface(ReferenceCounter<SDL_Surface>::create(
SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask),
SDL_FreeSurface))
{}
AppSurface::AppSurface(SDL_Window* w) :
surface(ReferenceCounter<SDL_Surface>::create(SDL_GetWindowSurface(w), SDL_FreeSurface))
{}
AppSurface::AppSurface(std::string filepath) :
surface(ReferenceCounter<SDL_Surface>::create(IMG_Load(filepath.c_str()), SDL_FreeSurface))
{}
AppSurface::operator bool(){
if (surface.good()) {
return true;
}
return false;
}
AppTexture::AppTexture(std::string filepath) :
texture(ReferenceCounter<SDL_Texture>::create(
SDL_CreateTextureFromSurface(static_cast<SDL_Renderer*>(App::instance()->getRenderer()), static_cast<SDL_Surface*>(AppSurface(filepath))),
SDL_DestroyTexture))
{}
AppTexture::operator bool(){
if (texture.good()) {
return true;
}
return false;
}
AppInitializer::AppInitializer() {
initializeVideo();
}
AppInitializer::AppInitializer(std::initializer_list<int> subsystem_flags) {
for (auto it = subsystem_flags.begin(); it != subsystem_flags.end(); it++) {
initializeSystem(*it);
}
}
AppInitializer::~AppInitializer() {
quitSystems();
}
void AppInitializer::initializeSystem(int flag) {
if (SDL_InitSubSystem(flag) != 0) {
printf("SDL_InitSubSystem error: %s\n", SDL_GetError());
good = false;
}
else {
initialized.push_back(flag);
}
}
void AppInitializer::quitSystems() {
for (auto it = initialized.begin(); it != initialized.end(); it++) {
SDL_QuitSubSystem(*it);
}
}
AppInitializer::operator bool() const {
return good;
}
AppWindow::AppWindow(int w, int h, Uint32 flags) :
window(ReferenceCounter<SDL_Window>::create(
SDL_CreateWindow("", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h, flags),
SDL_DestroyWindow
)),
surface(window)
{
width = w;
height = h;
}
AppWindow::operator bool() {
if (window.good()) {
return true;
}
return false;
}
AppRenderer::AppRenderer(AppWindow &window) :
renderer(ReferenceCounter<SDL_Renderer>::create(
SDL_CreateRenderer(static_cast<SDL_Window*>(window), -1, SDL_RENDERER_ACCELERATED), SDL_DestroyRenderer)
)
{}
AppRenderer::operator bool() {
if (renderer.good()) {
return true;
}
return false;
}
void AppRenderer::clear() {
SDL_RenderClear(this->get());
}
void AppRenderer::update() {
SDL_RenderPresent(this->get());
}
void AppRenderer::copy(SDL_Texture* texture, SDL_Rect* rect) {
SDL_RenderCopy(this->get(), texture, NULL, rect);
}
AppEventManager::AppEventManager() {
default_function = [](SDL_Event) {};
}
bool AppEventManager::isHandled(EventType etype) {
if (event_functions.find(etype) != event_functions.end() ||
simple_functions.find(etype) != simple_functions.end()) {
return true;
}
return false;
}
void AppEventManager::registerEventFunction(EventType etype, EventFunction efunc) {
if (!isHandled(etype)) {
event_functions[etype] = efunc;
}
}
void AppEventManager::registerSimpleFunction(EventType etype, SimpleFunction sfunc) {
if (!isHandled(etype)) {
simple_functions[etype] = sfunc;
}
}
void AppEventManager::callFunction(SDL_Event e) {
if (isHandled(e.type)) {
std::map<EventType, EventFunction>::iterator event_it = event_functions.find(e.type);
if (event_it != event_functions.end()) {
event_it->second(e);
}
else {
simple_functions[e.type]();
}
}
else {
default_function(e);
}
}
void AppEventManager::checkForEvent() {
SDL_Event e;
if (SDL_PollEvent(&e)) {
callFunction(e);
}
}
App *App::app_instance = 0;
App::App(int width, int height, Uint32 flags) : initializer(), window(width, height, flags), renderer(window), event_manager(){
event_manager.registerSimpleFunction(SDL_QUIT, []() {App::instance()->stop();});
event_manager.registerEventFunction(SDL_WINDOWEVENT, [](SDL_Event e) {if (e.window.event == SDL_WINDOWEVENT_RESIZED) { App::instance()->getWindow().updateDimensions(e.window.data1, e.window.data2); }});
}
void App::init(int width, int height, Uint32 flags) {
if (!app_instance) {
app_instance = new App(width, height, flags);
}
}
App* App::instance(){
if (app_instance) {
return app_instance;
}
else {
printf("App not initialized, call App::init(int, int, Uint32)\n");
return NULL;
}
}