-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdl2.h
More file actions
101 lines (84 loc) · 3.13 KB
/
Copy pathsdl2.h
File metadata and controls
101 lines (84 loc) · 3.13 KB
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
// From https://github.com/xyproto/sdl2-examples/blob/master/include/sdl2.h
// MIT License
#pragma once
#include <SDL2/SDL.h>
#include <memory>
#include <sstream>
namespace sdl2 {
// Very useful function from Eric Scott Barr:
// https://eb2.co/blog/2014/04/c-plus-plus-14-and-sdl2-managing-resources/
template <typename Creator, typename Destructor, typename... Arguments>
inline auto make_resource(Creator c, Destructor d, Arguments&&... args)
{
auto r = c(std::forward<Arguments>(args)...);
return std::unique_ptr<std::decay_t<decltype(*r)>, decltype(d)>(r, d);
}
// The "internal type" of the SDL System
using SDL_System = int;
// SDL_CreateSDL initiates the use of SDL.
// The given flags are passed to SDL_Init.
// The returned value contains the exit code.
inline SDL_System* SDL_CreateSDL(Uint32 flags)
{
auto init_status = new SDL_System;
*init_status = SDL_Init(flags);
return init_status;
}
// SDL_DestroySDL ends the use of SDL
inline void SDL_DestroySDL(SDL_System* init_status)
{
delete init_status; // Delete the int that contains the return value from SDL_Init
SDL_Quit();
}
using sdlsystem_ptr_t = std::unique_ptr<SDL_System, decltype(&SDL_DestroySDL)>;
using window_ptr_t = std::unique_ptr<SDL_Window, decltype(&SDL_DestroyWindow)>;
using renderer_ptr_t = std::unique_ptr<SDL_Renderer, decltype(&SDL_DestroyRenderer)>;
using surf_ptr_t = std::unique_ptr<SDL_Surface, decltype(&SDL_FreeSurface)>;
using texture_ptr_t = std::unique_ptr<SDL_Texture, decltype(&SDL_DestroyTexture)>;
// Initialize SDL (the returned int* contains the return value from SDL_Init)
inline sdlsystem_ptr_t make_sdlsystem(Uint32 flags)
{
return make_resource(SDL_CreateSDL, SDL_DestroySDL, flags);
}
// Create a window (that contains both a SDL_Window and the destructor for SDL_Windows)
inline window_ptr_t make_window(const char* title, int x, int y, int w, int h, Uint32 flags)
{
return make_resource(SDL_CreateWindow, SDL_DestroyWindow, title, x, y, w, h, flags);
}
// Create a renderer given a window, containing both the renderer and the destructor
inline renderer_ptr_t make_renderer(SDL_Window* win, int x, Uint32 flags)
{
return make_resource(SDL_CreateRenderer, SDL_DestroyRenderer, win, x, flags);
}
// Create a surface from a bmp file, containing both the surface and the destructor
inline surf_ptr_t make_bmp(SDL_RWops* sdlfile)
{
// May throw an exception if sdlfile is nullptr
return make_resource(SDL_LoadBMP_RW, SDL_FreeSurface, sdlfile, 1);
}
// Create a texture from a renderer and a surface
inline texture_ptr_t make_texture(SDL_Renderer* ren, SDL_Surface* surf)
{
return make_resource(SDL_CreateTextureFromSurface, SDL_DestroyTexture, ren, surf);
}
// Easy to raise exceptions with this class
class SDLError : public std::exception
{
std::string error_message;
public:
SDLError(const char *err_msg)
{
std::stringstream msg;
msg << err_msg << ": " << SDL_GetError();
this->error_message = msg.str();
}
SDLError(const std::string &err_msg)
: SDLError(err_msg.c_str())
{}
const char*
what() const noexcept override
{
return error_message.c_str();
}
};
} // namespace sdl2