4#include <SDL3_image/SDL_image.h>
5#include <SDL3_mixer/SDL_mixer.h>
6#include <SDL3_ttf/SDL_ttf.h>
10#include <unordered_map>
19std::unordered_map<std::string, asw::Texture>
textures;
20std::unordered_map<std::string, asw::Font>
fonts;
21std::unordered_map<std::string, asw::Sample>
samples;
22std::unordered_map<std::string, asw::Music>
music;
31 const char* base_path = SDL_GetBasePath();
32 if (base_path ==
nullptr) {
35 return std::string(base_path) + filename;
40 char* pref_path = SDL_GetPrefPath(org.c_str(), app.c_str());
41 if (pref_path ==
nullptr) {
45 std::string path(pref_path);
54 const auto full_path =
get_path(filename);
57 if (temp ==
nullptr) {
61 SDL_SetTextureScaleMode(temp, SDL_SCALEMODE_NEAREST);
62 SDL_SetTextureBlendMode(temp, SDL_BLENDMODE_BLEND);
67 return { temp, [](SDL_Texture* t) {
69 SDL_DestroyTexture(t);
107 = SDL_CreateTexture(r, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
109 SDL_SetTextureScaleMode(txr, SDL_SCALEMODE_NEAREST);
110 SDL_SetTextureBlendMode(txr, SDL_BLENDMODE_BLEND);
112 return { txr, [](SDL_Texture* t) {
114 SDL_DestroyTexture(t);
126 SDL_Surface* surface = SDL_CreateSurface(size, size, SDL_PIXELFORMAT_RGBA32);
127 if (surface ==
nullptr) {
131 const auto* details = SDL_GetPixelFormatDetails(surface->format);
132 const float half =
static_cast<float>(size) / 2.0F;
134 auto mix = [](uint8_t a, uint8_t b,
float t) {
135 return static_cast<uint8_t
>(
136 std::lround(
static_cast<float>(a) + (
static_cast<float>(b) - a) * t));
139 for (
int y = 0; y < size; y++) {
140 auto* row =
reinterpret_cast<uint32_t*
>(
141 static_cast<uint8_t*
>(surface->pixels) + y * surface->pitch);
143 for (
int x = 0; x < size; x++) {
144 const float distance = std::hypot(
static_cast<float>(x) + 0.5F - half,
145 static_cast<float>(y) + 0.5F - half)
149 const float t = std::clamp(distance, 0.0F, 1.0F);
150 const float amount = t * t * (3.0F - 2.0F * t);
152 row[x] = SDL_MapRGBA(details,
nullptr, mix(inner.
r, outer.
r, amount),
153 mix(inner.
g, outer.
g, amount), mix(inner.
b, outer.
b, amount),
154 mix(inner.
a, outer.
a, amount));
158 SDL_Texture* txr = SDL_CreateTextureFromSurface(r, surface);
159 SDL_DestroySurface(surface);
161 if (txr ==
nullptr) {
165 SDL_SetTextureScaleMode(txr, SDL_SCALEMODE_LINEAR);
166 SDL_SetTextureBlendMode(txr, SDL_BLENDMODE_BLEND);
168 return { txr, [](SDL_Texture* t) {
170 SDL_DestroyTexture(t);
179 const auto full_path =
get_path(filename);
180 TTF_Font* temp = TTF_OpenFont(full_path.c_str(), size);
182 if (temp ==
nullptr) {
189 TTF_SetFontHinting(temp, TTF_HINTING_MONO);
194 return { temp, [](TTF_Font* f) {
195 if (TTF_WasInit() > 0) {
202 const std::string& filename,
float size,
const std::string& key,
asw::FontStyle style)
204 if (
auto it =
fonts.find(key); it !=
fonts.end()) {
209 fonts.try_emplace(key, font);
215 auto it =
fonts.find(key);
216 if (it ==
fonts.end()) {
233 const auto full_path =
get_path(filename);
236 if (temp ==
nullptr) {
240 return { temp, [](MIX_Audio* a) {
254 samples.try_emplace(key, sample);
276 const auto full_path =
get_path(filename);
279 if (temp ==
nullptr) {
283 return { temp, [](MIX_Audio* a) {
292 if (
auto it =
music.find(key); it !=
music.end()) {
297 music.try_emplace(key, mus);
303 auto it =
music.find(key);
304 if (it ==
music.end()) {
Asset routines for the ASW library.
Display and window routines for the ASW library.
Routines for drawing sprites and primitives to the screen.
std::unordered_map< std::string, asw::Music > music
std::unordered_map< std::string, asw::Sample > samples
std::unordered_map< std::string, asw::Texture > textures
std::unordered_map< std::string, asw::Font > fonts
asw::Sample get_sample(const std::string &key)
Get a cached sample.
asw::Texture create_texture(int w, int h)
Create a Texture given the specified dimensions.
void unload_music(const std::string &key)
Remove a cached music.
std::string get_path(const std::string &filename)
Get the full path to an asset given its filename. The filename is relative to the base path of the ap...
asw::Texture create_radial_gradient(int size, asw::Color inner, asw::Color outer)
Create a square texture with a radial gradient. The colour goes smoothly from inner at the centre to ...
asw::Texture get_texture(const std::string &key)
Get a cached texture.
void unload_sample(const std::string &key)
Remove a cached sample.
std::string get_save_path(const std::string &org, const std::string &app)
Get a writable folder for save files and settings, unique to the organisation and application....
void unload_font(const std::string &key)
Remove a cached font.
asw::Music load_music(const std::string &filename)
Loads a music file from a file. Formats supported are WAV, AIFF, RIFF, OGG and VOC....
asw::Font load_font(const std::string &filename, float size, asw::FontStyle style=asw::FontStyle::Smooth)
Loads a TTF font from a file. This will abort if the file is not found.
void clear_all()
Clear all cached assets. This will remove all cached textures, fonts, samples, and music.
void unload_texture(const std::string &key)
Remove a cached texture.
asw::Music get_music(const std::string &key)
Get a cached music.
asw::Sample load_sample(const std::string &filename)
Loads a sample from a file. Formats supported are WAV, AIFF, RIFF, OGG and VOC. This will abort if th...
asw::Font get_font(const std::string &key)
Get a cached font.
asw::Texture load_texture(const std::string &filename)
Loads a texture from a file. Formats supported are PNG, ICO, CUR, BMP, GIF, JPG, LBM,...
asw::Renderer * get_renderer()
Get the SDL renderer.
void clear_text_cache()
Clear cached rendered text textures.
MIX_Mixer * get_mixer()
Get the SDL mixer device.
void clear_text_size_cache()
Clear cached text metrics.
void abort_on_error(const std::string &message)
Abort program and display error message.
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
std::shared_ptr< MIX_Audio > Sample
Alias for a shared pointer to an MIX_Audio.
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
std::shared_ptr< MIX_Audio > Music
Alias for a shared pointer to an MIX_Audio.
FontStyle
How a font's glyphs are rendered.
Sound module for the ASW library.
RGBA color struct with 8-bit channels.
Types used throughout the ASW library.
General utility functions.