-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsdl-wrapper.h
84 lines (65 loc) · 2.78 KB
/
sdl-wrapper.h
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
#ifndef SDL_WRAPPER_H
#define SDL_WRAPPER_H
#include <filesystem>
#include <string>
#include <SDL3/SDL.h>
#include "debug-log.h"
#include "raii-wrapper.h"
namespace SDL
{
struct FreeFunctor
{
void operator()(void* const pointer) { return SDL_free(pointer); }
};
template<typename T>
using Pointer = std::unique_ptr<T, FreeFunctor>;
MAKE_RAII_POINTER(Window, SDL_Window, SDL_DestroyWindow );
MAKE_RAII_POINTER(Renderer, SDL_Renderer, SDL_DestroyRenderer);
MAKE_RAII_POINTER(Texture, SDL_Texture, SDL_DestroyTexture );
MAKE_RAII_POINTER(IOStream, SDL_IOStream, SDL_CloseIO );
template<auto Function, typename... Args>
auto PathFunction(const char* const path, Args &&...args)
{
return Function(path, std::forward<Args>(args)...);
};
template<auto Function, typename... Args>
auto PathFunction(const std::string &path, Args &&...args)
{
return Function(path.c_str(), std::forward<Args>(args)...);
};
template<auto Function, typename... Args>
auto PathFunction(const std::filesystem::path &path, Args &&...args)
{
return Function(reinterpret_cast<const char*>(path.u8string().c_str()), std::forward<Args>(args)...);
};
template<typename T>
IOStream IOFromFile(const T &path, const char* const mode) { return IOStream(PathFunction<SDL_IOFromFile>(path, mode)); }
template<typename T>
bool RemovePath(const T &path) { return PathFunction<SDL_RemovePath>(path); }
template<typename T>
bool GetPathInfo(const T &path, SDL_PathInfo* const info) { return PathFunction<SDL_GetPathInfo>(path, info); }
using Pixel = Uint32;
inline Texture CreateTexture(Renderer &renderer, const SDL_TextureAccess access, const int width, const int height, const SDL_ScaleMode scale_mode, const bool blending = false)
{
// We're using ARGB8888 because it's more likely to be supported natively by the GPU, avoiding the need for constant conversions
Texture texture(SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, access, width, height));
if (!texture)
{
Frontend::debug_log.Log("SDL_CreateTexture failed with the following message - '{}'", SDL_GetError());
}
else
{
// Disable blending, since we don't need it
if (!SDL_SetTextureBlendMode(texture, blending ? SDL_BLENDMODE_BLEND : SDL_BLENDMODE_NONE))
Frontend::debug_log.Log("SDL_SetTextureBlendMode failed with the following message - '{}'", SDL_GetError());
if (!SDL_SetTextureScaleMode(texture, scale_mode))
Frontend::debug_log.Log("SDL_SetTextureScaleMode failed with the following message - '{}'", SDL_GetError());
}
return texture;
}
inline Texture CreateTextureWithBlending(Renderer &renderer, const SDL_TextureAccess access, const int width, const int height, const SDL_ScaleMode scale_mode)
{
return CreateTexture(renderer, access, width, height, scale_mode, true);
}
}
#endif /* SDL_WRAPPER_H */