Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
assets.cpp
1/*
2 Wizard Engine
3 Copyright (C) 2023-2024 Zana Domán
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22// NOLINTNEXTLINE(bugprone-reserved-identifier,cert-dcl37-c,cert-dcl51-cpp)
23#define __WIZARD_ENGINE_INTERNAL__
24
28
29void wze::assets::combine_hash(size_t& seed, size_t value) {
30 // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers,readability-magic-numbers)
31 seed ^= value + 0x9e3779b9 + (seed << 6) + (seed >> 2);
32}
33
34std::shared_ptr<wze::image> wze::assets::load_image(std::string const& path) {
35 std::shared_ptr<image> image;
36
37 image = {
38 IMG_Load(std::filesystem::relative(_assets + path).string().c_str()),
39 SDL_FreeSurface};
40 if (!image) {
41 throw exception(IMG_GetError());
42 }
43
44 return image;
45}
46
47std::shared_ptr<wze::image>
48wze::assets::create_image(std::string const& text,
49 std::shared_ptr<font> const& font,
50 uint32_t wrap_length) {
51 std::shared_ptr<image> image;
52
53 image = {TTF_RenderUTF8_LCD_Wrapped(font.get(), text.c_str(),
54 {std::numeric_limits<uint8_t>::max(),
55 std::numeric_limits<uint8_t>::max(),
56 std::numeric_limits<uint8_t>::max(),
57 std::numeric_limits<uint8_t>::max()},
58 {0, 0, 0, 0}, wrap_length),
59 SDL_FreeSurface};
60 if (!image) {
61 throw exception(TTF_GetError());
62 }
63
64 return image;
65}
66
67size_t wze::assets::hash_image(std::shared_ptr<image> const& image) {
68 size_t seed;
69 std::hash<uint8_t> hash;
70
71 if (!image) {
72 return hash(0);
73 }
74
75 seed = 0;
76 std::for_each(
77 static_cast<uint8_t*>(image->pixels),
78 // NOLINTNEXTLINE(bugprone-implicit-widening-of-multiplication-result,cppcoreguidelines-pro-bounds-pointer-arithmetic)
79 static_cast<uint8_t*>(image->pixels) +
80 image->w * image->h * image->format->BytesPerPixel,
81 [&](uint8_t pixel) -> void {
82 combine_hash(seed, hash(pixel));
83 });
84
85 return seed;
86}
87
88std::shared_ptr<wze::texture>
89wze::assets::create_texture(std::shared_ptr<image> const& image) {
90 std::shared_ptr<texture> texture;
91
92 texture = {SDL_CreateTextureFromSurface(renderer::base(), image.get()),
93 SDL_DestroyTexture};
94 if (!texture) {
95 throw exception(SDL_GetError());
96 }
97
98 return texture;
99}
100
101std::shared_ptr<wze::sound> wze::assets::load_sound(std::string const& path) {
102 std::shared_ptr<sound> sound;
103
104 sound = {
105 Mix_LoadWAV(std::filesystem::relative(_assets + path).string().c_str()),
106 Mix_FreeChunk};
107 if (!sound) {
108 throw exception(Mix_GetError());
109 }
110
111 return sound;
112}
113
114size_t wze::assets::hash_sound(std::shared_ptr<sound> const& sound) {
115 size_t seed;
116 std::hash<uint8_t> hash;
117
118 if (!sound) {
119 return hash(0);
120 }
121
122 seed = 0;
123 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
124 std::for_each(sound->abuf, sound->abuf + sound->alen,
125 [&](uint8_t sample) -> void {
126 combine_hash(seed, hash(sample));
127 });
128
129 return seed;
130}
131
132std::shared_ptr<wze::font> wze::assets::load_font(std::string const& path,
133 uint8_t size,
134 font_style style,
135 font_alignment alignment) {
136 std::shared_ptr<font> font;
137
138 font = {
139 TTF_OpenFont(std::filesystem::relative(_assets + path).string().c_str(),
140 size),
141 TTF_CloseFont};
142 if (!font) {
143 throw exception(TTF_GetError());
144 }
145 TTF_SetFontStyle(font.get(), style);
146 TTF_SetFontWrappedAlign(font.get(), alignment);
147 TTF_SetFontHinting(font.get(), TTF_HINTING_LIGHT_SUBPIXEL);
148
149 return font;
150}
151
152std::unique_ptr<wze::cursor, std::function<void(wze::cursor*)>>
153wze::assets::create_cursor(system_cursor system_cursor) {
154 std::unique_ptr<cursor, std::function<void(cursor*)>> cursor;
155
156 cursor = {SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor),
157 SDL_FreeCursor};
158 if (!cursor) {
159 throw exception(SDL_GetError());
160 }
161
162 return cursor;
163}
164
165std::unique_ptr<wze::cursor, std::function<void(wze::cursor*)>>
166wze::assets::create_cursor(std::shared_ptr<image> const& image, uint16_t hot_x,
167 uint16_t hot_y) {
168 std::unique_ptr<cursor, std::function<void(cursor*)>> cursor;
169
170 cursor = {SDL_CreateColorCursor(image.get(), hot_x, hot_y), SDL_FreeCursor};
171 if (!cursor) {
172 throw exception(SDL_GetError());
173 }
174
175 return cursor;
176}
Image file in host memory.
Generic exception.
font_style
Font styles.
Definition enums.hpp:59
system_cursor
System cursors.
Definition enums.hpp:352
font_alignment
Font alignments.
Definition enums.hpp:48
Subsystem to handle graphics.