-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsdlpp.hpp
609 lines (577 loc) · 22.4 KB
/
sdlpp.hpp
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
This source file is part of sdlpp (C++ wrapper for SDL2)
Copyright (c) 2017-2020 Mika Pi
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#pragma once
// enable this if you want to use the higher-level SDL2_gfx features
//#define USE_SDLGFX
#include <SDL.h>
#ifdef USE_SDLGFX
#include <SDL2_gfxPrimitives.h>
#endif
#include <filesystem>
#include <functional>
#include <sstream>
namespace sdl
{
class Error : public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};
class Init
{
public:
template <typename... Args>
Init(Args... args)
{
auto res = SDL_Init(args...);
if (res < 0)
{
std::ostringstream strm;
strm << "Init::Init(): " << SDL_GetError();
throw Error(strm.str());
}
}
Init(const Init &) = delete;
Init &operator=(const Init &) = delete;
~Init() { SDL_Quit(); }
};
#define SDL_CLASS(X) \
class X \
{ \
public: \
template <typename... Args> \
X(Args... args) \
{ \
handle = SDL_Create##X(args...); \
if (!handle) \
{ \
std::ostringstream strm; \
strm << #X "::" #X "(): " << SDL_GetError(); \
throw Error(strm.str()); \
} \
} \
X(SDL_##X *handle_) : handle(handle_) {} \
X(const X &) = delete; \
X &operator=(const X &) = delete; \
X &operator=(X &&x) \
{ \
handle = x.handle; \
x.handle = nullptr; \
return *this; \
} \
X(X &&x) : handle(x.handle) { x.handle = nullptr; } \
~X() \
{ \
if (handle) \
SDL_Destroy##X(handle); \
} \
SDL_##X *get() { return handle; } \
const SDL_##X *get() const { return handle; } \
private: \
SDL_##X *handle; \
\
public: \
SDL_CLASS_METHOD_LIST \
}
template <typename R, typename... Args2, typename... Args>
auto callSdl(R(f)(Args2...), Args... args) -> decltype(f(args...))
{
return f(args...);
}
template <typename R, typename... Args2, typename... Args>
auto callSdl(R *(f)(Args2...), Args... args) -> decltype(f(args...))
{
auto r = f(args...);
if (r == nullptr)
{
throw Error(std::string("Error string: ") + SDL_GetError());
}
return r;
}
template <typename... Args2, typename... Args>
auto callSdl(int(f)(Args2...), Args... args) -> decltype(f(args...))
{
auto r = f(args...);
if (r == -1)
{
throw Error("Error code: " + std::to_string(r) + " Error string: " + SDL_GetError());
}
return r;
}
template <typename... Args2, typename... Args>
auto callSdl(void(f)(Args2...), Args... args) -> decltype(f(args...))
{
f(args...);
}
#define METHOD2(X, Y) \
template <typename... Args> \
auto X(Args... args)->decltype(Y(handle, args...)) \
{ \
return callSdl(Y, handle, args...); \
}
#define METHOD(X, Y) METHOD2(X, SDL_##Y)
#define SDL_CLASS_METHOD_LIST_TMP \
METHOD(glCreateContext, GL_CreateContext) \
METHOD(glGetDrawableSize, GL_GetDrawableSize) \
METHOD(glMakeCurrent, GL_MakeCurrent) \
METHOD(glSwap, GL_SwapWindow) \
METHOD(getBrightness, GetWindowBrightness) \
METHOD(getData, GetWindowData) \
METHOD(getDisplayIndex, GetWindowDisplayIndex) \
METHOD(getDisplayMode, GetWindowDisplayMode) \
METHOD(getFlags, GetWindowFlags) \
METHOD(getGammaRamp, GetWindowGammaRamp) \
METHOD(getGrab, GetWindowGrab) \
METHOD(getID, GetWindowID) \
METHOD(getMaximumSize, GetWindowMaximumSize) \
METHOD(getMinimumSize, GetWindowMinimumSize) \
METHOD(getPixelFormat, GetWindowPixelFormat) \
METHOD(getPosition, GetWindowPosition) \
METHOD(getSize, GetWindowSize) \
METHOD(getSurface, GetWindowSurface) \
METHOD(getTitle, GetWindowTitle) \
METHOD(hide, HideWindow) \
METHOD(maximize, MaximizeWindow) \
METHOD(minimize, MinimizeWindow) \
METHOD(raise, RaiseWindow) \
METHOD(restore, RestoreWindow) \
METHOD(setBordered, SetWindowBordered) \
METHOD(setBrightness, SetWindowBrightness) \
METHOD(setData, SetWindowData) \
METHOD(setDisplayMode, SetWindowDisplayMode) \
METHOD(setFullscreen, SetWindowFullscreen) \
METHOD(setGammaRamp, SetWindowGammaRamp) \
METHOD(setGrab, SetWindowGrab) \
METHOD(setIcon, SetWindowIcon) \
METHOD(setMaximumSize, SetWindowMaximumSize) \
METHOD(setMinimumSize, SetWindowMinimumSize) \
METHOD(setPosition, SetWindowPosition) \
METHOD(setSize, SetWindowSize) \
METHOD(setTitle, SetWindowTitle) \
METHOD(show, ShowWindow) \
METHOD(updateSurface, UpdateWindowSurface) \
METHOD(updateSurfaceRects, UpdateWindowSurfaceRects)
#if SDL_COMPILEDVERSION < 2004
#define SDL_CLASS_METHOD_LIST \
SDL_CLASS_METHOD_LIST_TMP
#else
#define SDL_CLASS_METHOD_LIST \
SDL_CLASS_METHOD_LIST_TMP \
METHOD(setHitTest, SetWindowHitTest)
#endif
SDL_CLASS(Window);
#undef SDL_CLASS_METHOD_LIST
#undef SDL_CLASS_METHOD_LIST_TMP
#define SDL_CLASS_METHOD_LIST_TMP \
METHOD(getDrawBlendMode, GetRenderDrawBlendMode) \
METHOD(getDrawColor, GetRenderDrawColor) \
METHOD(getDriverInfo, GetRenderDriverInfo) \
METHOD(getTarget, GetRenderTarget) \
METHOD(getInfo, GetRendererInfo) \
METHOD(getOutputSize, GetRendererOutputSize) \
METHOD(clear, RenderClear) \
METHOD(copy, RenderCopy) \
METHOD(copyEx, RenderCopyEx) \
METHOD(drawLine, RenderDrawLine) \
METHOD(drawLines, RenderDrawLines) \
METHOD(drawPoint, RenderDrawPoint) \
METHOD(drawPoints, RenderDrawPoints) \
METHOD(drawRect, RenderDrawRect) \
METHOD(drawRects, RenderDrawRects) \
METHOD(fillRect, RenderFillRect) \
METHOD(fillRects, RenderFillRects) \
METHOD(getClipRect, RenderGetClipRect) \
METHOD(getLogicalSize, RenderGetLogicalSize) \
METHOD(getScale, RenderGetScale) \
METHOD(getViewport, RenderGetViewport) \
METHOD(present, RenderPresent) \
METHOD(readPixels, RenderReadPixels) \
METHOD(setClipRect, RenderSetClipRect) \
METHOD(setLogicalSize, RenderSetLogicalSize) \
METHOD(setScale, RenderSetScale) \
METHOD(setViewport, RenderSetViewport) \
METHOD(targetSupported, RenderTargetSupported) \
METHOD(setDrawBlendMode, SetRenderDrawBlendMode) \
METHOD(setDrawColor, SetRenderDrawColor) \
METHOD(setTarget, SetRenderTarget)
#ifdef USE_SDLGFX
#define SDLGFX_METHOD_LIST \
METHOD2(pixelColor, ::pixelColor) \
METHOD2(pixelRGBA, ::pixelRGBA) \
METHOD2(hlineColor, ::hlineColor) \
METHOD2(hlineRGBA, ::hlineRGBA) \
METHOD2(vlineColor, ::vlineColor) \
METHOD2(vlineRGBA, ::vlineRGBA) \
METHOD2(rectangleColor, ::rectangleColor) \
METHOD2(rectangleRGBA, ::rectangleRGBA) \
METHOD2(roundedRectangleColor, ::roundedRectangleColor) \
METHOD2(roundedRectangleRGBA, ::roundedRectangleRGBA) \
METHOD2(boxColor, ::boxColor) \
METHOD2(boxRGBA, ::boxRGBA) \
METHOD2(roundedBoxColor, ::roundedBoxColor) \
METHOD2(roundedBoxRGBA, ::roundedBoxRGBA) \
METHOD2(lineColor, ::lineColor) \
METHOD2(lineRGBA, ::lineRGBA) \
METHOD2(aalineColor, ::aalineColor) \
METHOD2(aalineRGBA, ::aalineRGBA) \
METHOD2(thickLineColor, ::thickLineColor) \
METHOD2(thickLineRGBA, ::thickLineRGBA) \
METHOD2(circleColor, ::circleColor) \
METHOD2(circleRGBA, ::circleRGBA) \
METHOD2(arcColor, ::arcColor) \
METHOD2(arcRGBA, ::arcRGBA) \
METHOD2(aacircleColor, ::aacircleColor) \
METHOD2(aacircleRGBA, ::aacircleRGBA) \
METHOD2(filledCircleColor, ::filledCircleColor) \
METHOD2(filledCircleRGBA, ::filledCircleRGBA) \
METHOD2(ellipseColor, ::ellipseColor) \
METHOD2(ellipseRGBA, ::ellipseRGBA) \
METHOD2(aaellipseColor, ::aaellipseColor) \
METHOD2(aaellipseRGBA, ::aaellipseRGBA) \
METHOD2(filledEllipseColor, ::filledEllipseColor) \
METHOD2(filledEllipseRGBA, ::filledEllipseRGBA) \
METHOD2(pieColor, ::pieColor) \
METHOD2(pieRGBA, ::pieRGBA) \
METHOD2(filledPieColor, ::filledPieColor) \
METHOD2(filledPieRGBA, ::filledPieRGBA) \
METHOD2(trigonColor, ::trigonColor) \
METHOD2(trigonRGBA, ::trigonRGBA) \
METHOD2(aatrigonColor, ::aatrigonColor) \
METHOD2(aatrigonRGBA, ::aatrigonRGBA) \
METHOD2(filledTrigonColor, ::filledTrigonColor) \
METHOD2(filledTrigonRGBA, ::filledTrigonRGBA) \
METHOD2(polygonColor, ::polygonColor) \
METHOD2(polygonRGBA, ::polygonRGBA) \
METHOD2(aapolygonColor, ::aapolygonColor) \
METHOD2(aapolygonRGBA, ::aapolygonRGBA) \
METHOD2(filledPolygonColor, ::filledPolygonColor) \
METHOD2(filledPolygonRGBA, ::filledPolygonRGBA) \
METHOD2(texturedPolygon, ::texturedPolygon) \
METHOD2(bezierColor, ::bezierColor) \
METHOD2(bezierRGBA, ::bezierRGBA) \
METHOD2(characterColor, ::characterColor) \
METHOD2(characterRGBA, ::characterRGBA) \
METHOD2(stringColor, ::stringColor) \
METHOD2(stringRGBA, ::stringRGBA) \
int stringColor(Sint16 x, Sint16 y, const std::string& str, Uint32 color) \
{ return stringColor(x,y, str.c_str(), color); } \
int stringRGBA(Sint16 x, Sint16 y, const std::string& str, Uint8 r, Uint8 g, Uint8 b, Uint8 a) \
{ return stringRGBA(x,y, str.c_str(), r,g,b,a); }
#else
#define SDLGFX_METHOD_LIST
#endif
#if SDL_COMPILEDVERSION < 2004
#define SDL_CLASS_METHOD_LIST \
SDL_CLASS_METHOD_LIST_TMP \
SDLGFX_METHOD_LIST
SDL_CLASS(Renderer);
#else
#define SDL_CLASS_METHOD_LIST \
SDL_CLASS_METHOD_LIST_TMP \
SDLGFX_METHOD_LIST \
METHOD(isClipEnabled, RenderIsClipEnabled)
SDL_CLASS(Renderer);
#endif
#undef SDL_CLASS_METHOD_LIST
#define SDL_CLASS_METHOD_LIST \
Texture(SDL_Renderer *renderer, SDL_Surface *surface) \
: handle(SDL_CreateTextureFromSurface(renderer, surface)) \
{ \
if (!handle) \
{ \
std::ostringstream strm; \
strm << "Texture::Texture(): " << SDL_GetError(); \
throw Error(strm.str()); \
} \
} \
METHOD(glBind, GL_BindTexture) \
METHOD(glUnbind, GL_UnbindTexture) \
METHOD(getAlphaMod, GetTextureAlphaMod) \
METHOD(getBlendMode, GetTextureBlendMode) \
METHOD(getColorMod, GetTextureColorMod) \
METHOD(lock, LockTexture) \
METHOD(query, QueryTexture) \
METHOD(setAlphaMod, SetTextureAlphaMod) \
METHOD(setBlendMode, SetTextureBlendMode) \
METHOD(setColorMod, SetTextureColorMod) \
METHOD(unlock, UnlockTexture) \
METHOD(update, UpdateTexture) \
METHOD(updateYuv, UpdateYUVTexture)
SDL_CLASS(Texture);
class Surface
{
void checkErrors()
{
if (!handle)
{
std::ostringstream strm;
strm << "Surface::Surface(): " << SDL_GetError();
throw Error(strm.str());
}
}
public:
Surface(Uint32 flags,
int width,
int height,
int depth,
Uint32 Rmask,
Uint32 Gmask,
Uint32 Bmask,
Uint32 Amask)
: handle(SDL_CreateRGBSurface(flags, width, height, depth, Rmask, Gmask, Bmask, Amask))
{
checkErrors();
}
Surface(SDL_RWops *src, int freesrc) : handle(SDL_LoadBMP_RW(src, freesrc)) { checkErrors(); }
Surface(const std::string &file) : handle(SDL_LoadBMP(file.c_str())) { checkErrors(); }
Surface(SDL_Surface *src, const SDL_PixelFormat *fmt, Uint32 flags)
: handle(SDL_ConvertSurface(src, fmt, flags))
{
checkErrors();
}
Surface(SDL_Surface *src, Uint32 pixel_format, Uint32 flags)
: handle(SDL_ConvertSurfaceFormat(src, pixel_format, flags))
{
checkErrors();
}
Surface(SDL_Surface *handle_) : handle(handle_)
{
checkErrors();
}
Surface(const Surface &) = delete;
Surface(Surface &&x) : handle(x.handle) { x.handle = nullptr; }
Surface &operator=(const Surface &) = delete;
Surface &operator=(Surface &&x)
{
handle = x.handle;
x.handle = nullptr;
return *this;
}
~Surface() { SDL_FreeSurface(handle); }
SDL_Surface *get() { return handle; }
const SDL_Surface *get() const { return handle; }
SDL_Surface *operator->() { return handle; }
const SDL_Surface *operator->() const { return handle; }
private:
SDL_Surface *handle;
public:
METHOD(setPalette, SetSurfacePalette)
METHOD(lock, LockSurface)
METHOD(unlock, UnlockSurface)
METHOD(setColorKey, SetColorKey)
METHOD(getColorKey, GetColorKey)
METHOD(setColorMode, SetSurfaceColorMod)
METHOD(getColorMode, GetSurfaceColorMod)
METHOD(setClipRect, SetClipRect)
METHOD(getClipRect, GetClipRect)
METHOD(fillRect, FillRect)
METHOD(fillRects, FillRects)
METHOD(blit, BlitSurface)
METHOD(softStretch, SoftStretch)
METHOD(blitScaled, BlitScaled)
class
#if __has_cpp_attribute(nodiscard)
[[nodiscard]]
#endif
Lock {
public:
explicit Lock(Surface& s) : s(s) {
if (SDL_MUSTLOCK(s.get())) s.lock();
}
~Lock() {
if (SDL_MUSTLOCK(s.get())) s.unlock();
}
private:
Surface& s;
};
};
class Audio
{
public:
Audio(const char *device,
bool iscapture,
const SDL_AudioSpec *desired,
SDL_AudioSpec *obtained,
int allowed_changes,
std::function<void(Uint8 *stream, int len)> callback_ = nullptr)
: callback(callback_)
{
if (desired)
{
SDL_AudioSpec tmp;
tmp = *desired;
if (callback)
{
tmp.callback = Audio::internalCallback;
tmp.userdata = this;
}
else
{
tmp.callback = nullptr;
}
handle = SDL_OpenAudioDevice(device, iscapture, &tmp, obtained, allowed_changes);
}
else
{
handle = SDL_OpenAudioDevice(device, iscapture, desired, obtained, allowed_changes);
}
if (handle <= 0)
{
std::ostringstream strm;
strm << "Audio::Audio(): " << SDL_GetError();
throw Error(strm.str());
}
}
Audio(SDL_AudioDeviceID handle_) : handle(handle_) {}
Audio(const Audio &) = delete;
Audio &operator=(const Audio &) = delete;
~Audio() { SDL_CloseAudioDevice(handle); }
SDL_AudioDeviceID get() { return handle; }
private:
SDL_AudioDeviceID handle;
std::function<void(Uint8 *stream, int len)> callback;
static void internalCallback(void *userdata, Uint8 *stream, int len)
{
static_cast<Audio *>(userdata)->callback(stream, len);
}
public:
#if SDL_COMPILEDVERSION >= 2004
METHOD(clearQueued, ClearQueuedAudio)
METHOD(getQueuedSize, GetQueuedAudioSize)
METHOD(queue, QueueAudio)
#endif
METHOD(getStatus, GetAudioDeviceStatus)
METHOD(lock, LockAudioDevice)
METHOD(pause, PauseAudioDevice)
METHOD(unlock, UnlockAudioDevice)
};
class EventHandler
{
public:
inline SDL_Event* get() {
return &e;
}
void setCallback(const std::function<bool(SDL_Event)>& callback_)
{
callback = callback_;
}
bool poll()
{
if (SDL_PollEvent(&e))
{
handleEvent();
return true;
}
else
return false;
}
bool wait(int timeout = -1)
{
int res;
if (timeout == -1)
res = SDL_WaitEvent(&e);
else
res = SDL_WaitEventTimeout(&e, timeout);
if (res)
{
handleEvent();
return true;
}
else
return false;
}
#define SDL_EVENTS_TMP \
EVENT(SDL_CONTROLLERAXISMOTION, controllerAxisMotion, caxis); \
EVENT(SDL_CONTROLLERBUTTONDOWN, controllerButtonDown, cbutton); \
EVENT(SDL_CONTROLLERBUTTONUP, controllerButtonUp, cbutton); \
EVENT(SDL_CONTROLLERDEVICEADDED, controllerDeviceAdded, cdevice); \
EVENT(SDL_CONTROLLERDEVICEREMOVED, controllerDeviceRemoved, cdevice); \
EVENT(SDL_CONTROLLERDEVICEREMAPPED, controllerDeviceRemapped, cdevice); \
EVENT(SDL_DOLLARGESTURE, dollarGesture, dgesture); \
EVENT(SDL_DOLLARRECORD, dollarRecord, dgesture); \
EVENT(SDL_DROPFILE, dropFile, drop); \
EVENT(SDL_FINGERMOTION, fingerMotion, tfinger); \
EVENT(SDL_FINGERDOWN, fingerDown, tfinger); \
EVENT(SDL_FINGERUP, fingerUp, tfinger); \
EVENT(SDL_KEYDOWN, keyDown, key); \
EVENT(SDL_KEYUP, keyUp, key); \
EVENT(SDL_JOYAXISMOTION, joyAxisMotion, jaxis); \
EVENT(SDL_JOYBALLMOTION, joyBallMotion, jball); \
EVENT(SDL_JOYHATMOTION, joyHatMotion, jhat); \
EVENT(SDL_JOYBUTTONDOWN, joyButtonDown, jbutton); \
EVENT(SDL_JOYBUTTONUP, joyButtonUp, jbutton); \
EVENT(SDL_JOYDEVICEADDED, joyDeviceAdded, jdevice); \
EVENT(SDL_JOYDEVICEREMOVED, joyDeviceRemoved, jdevice); \
EVENT(SDL_MOUSEMOTION, mouseMotion, motion); \
EVENT(SDL_MOUSEBUTTONDOWN, mouseButtonDown, button); \
EVENT(SDL_MOUSEBUTTONUP, mouseButtonUp, button); \
EVENT(SDL_MOUSEWHEEL, mouseWheel, wheel); \
EVENT(SDL_MULTIGESTURE, multiGesture, mgesture); \
EVENT(SDL_QUIT, quit, quit); \
EVENT(SDL_SYSWMEVENT, sysWmEvent, syswm); \
EVENT(SDL_TEXTEDITING, textEditing, edit); \
EVENT(SDL_TEXTINPUT, textInput, text); \
EVENT(SDL_USEREVENT, userEvent, user); \
EVENT(SDL_WINDOWEVENT, windowEvent, window);
#if SDL_COMPILEDVERSION < 2004
#define SDL_EVENTS SDL_EVENTS_TMP
#else
#define SDL_EVENTS \
SDL_EVENTS_TMP \
EVENT(SDL_AUDIODEVICEADDED, audioDeviceAdded, adevice); \
EVENT(SDL_AUDIODEVICEREMOVED, audioDeviceRemoved, adevice);
#endif
#define EVENT(x, y, z) std::function<void(const decltype(SDL_Event::z) &)> y
SDL_EVENTS
#undef EVENT
private:
SDL_Event e;
std::function<bool(SDL_Event)> callback;
void handleEvent()
{
if (callback)
{
if (callback(e))
{
//consider handled
return;
}
}
switch (e.type)
{
#define EVENT(x, y, z) \
case x: \
if (y) \
y(e.z); \
break;
SDL_EVENTS
#undef EVENT
default: break;
}
}
};
inline std::filesystem::path get_base_path()
{
using deleter = decltype([](void *ptr) { SDL_free(ptr); });
auto ptr = std::unique_ptr<char[], deleter>(SDL_GetBasePath());
return std::filesystem::path(reinterpret_cast<char8_t const *>(ptr.get()),
std::filesystem::path::native_format);
}
} // namespace sdl