Wizard Engine
2D cross-platform game engine built around SDL2
 
Loading...
Searching...
No Matches
wze::assets Class Referencefinal

Static Public Member Functions

static std::shared_ptr< image > load_image (std::string const &path)
 
static std::shared_ptr< image > create_image (std::string const &text, std::shared_ptr< font > const &font, uint32_t wrap_length=0)
 
static size_t hash_image (std::shared_ptr< image > const &image)
 
static std::shared_ptr< texture > create_texture (std::shared_ptr< image > const &image)
 
static std::shared_ptr< sound > load_sound (std::string const &path)
 
static size_t hash_sound (std::shared_ptr< sound > const &sound)
 
static std::shared_ptr< font > load_font (std::string const &path, uint8_t size=48, font_style style=FONT_STYLE_NORMAL, font_alignment alignment=FONT_ALIGNMENT_LEFT)
 
static std::unique_ptr< cursor, std::function< void(cursor *)> > create_cursor (system_cursor system_cursor)
 
static std::unique_ptr< cursor, std::function< void(cursor *)> > create_cursor (std::shared_ptr< image > const &image, uint16_t hot_x=0, uint16_t hot_y=0)
 

Detailed Description

Definition at line 72 of file assets.hpp.

Member Function Documentation

◆ load_image()

std::shared_ptr< wze::image > wze::assets::load_image ( std::string const & path)
staticnodiscard

Definition at line 34 of file assets.cpp.

34 {
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}

◆ create_image()

std::shared_ptr< wze::image > wze::assets::create_image ( std::string const & text,
std::shared_ptr< font > const & font,
uint32_t wrap_length = 0 )
staticnodiscard

Definition at line 48 of file assets.cpp.

50 {
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}

◆ hash_image()

size_t wze::assets::hash_image ( std::shared_ptr< image > const & image)
staticnodiscard

Definition at line 67 of file assets.cpp.

67 {
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}

◆ create_texture()

std::shared_ptr< wze::texture > wze::assets::create_texture ( std::shared_ptr< image > const & image)
staticnodiscard

Definition at line 89 of file assets.cpp.

89 {
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}

◆ load_sound()

std::shared_ptr< wze::sound > wze::assets::load_sound ( std::string const & path)
staticnodiscard

Definition at line 101 of file assets.cpp.

101 {
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}

◆ hash_sound()

size_t wze::assets::hash_sound ( std::shared_ptr< sound > const & sound)
staticnodiscard

Definition at line 114 of file assets.cpp.

114 {
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}

◆ load_font()

std::shared_ptr< wze::font > wze::assets::load_font ( std::string const & path,
uint8_t size = 48,
font_style style = FONT_STYLE_NORMAL,
font_alignment alignment = FONT_ALIGNMENT_LEFT )
staticnodiscard

Definition at line 132 of file assets.cpp.

135 {
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}

◆ create_cursor() [1/2]

std::unique_ptr< wze::cursor, std::function< void(wze::cursor *)> > wze::assets::create_cursor ( system_cursor system_cursor)
staticnodiscard

Definition at line 153 of file assets.cpp.

153 {
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}
system_cursor
System cursors.
Definition enums.hpp:352

◆ create_cursor() [2/2]

std::unique_ptr< wze::cursor, std::function< void(wze::cursor *)> > wze::assets::create_cursor ( std::shared_ptr< image > const & image,
uint16_t hot_x = 0,
uint16_t hot_y = 0 )
staticnodiscard

Definition at line 166 of file assets.cpp.

167 {
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}

The documentation for this class was generated from the following files: