ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
asset_manager.h
Go to the documentation of this file.
1
8
9#ifndef ASW_ASSET_MANAGER_H
10#define ASW_ASSET_MANAGER_H
11
12#include <string>
13#include <unordered_map>
14
15#include "./assets.h"
16#include "./types.h"
17
18namespace asw {
19
27 public:
28 // --- Textures ---
29
35 Texture getTexture(const std::string& path) {
36 auto it = textures.find(path);
37 if (it != textures.end()) {
38 return it->second;
39 }
40 auto tex = asw::assets::loadTexture(path);
41 textures[path] = tex;
42 return tex;
43 }
44
50 bool hasTexture(const std::string& path) const {
51 return textures.find(path) != textures.end();
52 }
53
54 // --- Fonts ---
55
62 Font getFont(const std::string& path, float size) {
63 std::string key = path + ":" + std::to_string(size);
64 auto it = fonts.find(key);
65 if (it != fonts.end()) {
66 return it->second;
67 }
68 auto font = asw::assets::loadFont(path, size);
69 fonts[key] = font;
70 return font;
71 }
72
79 bool hasFont(const std::string& path, float size) const {
80 std::string key = path + ":" + std::to_string(size);
81 return fonts.find(key) != fonts.end();
82 }
83
84 // --- Samples ---
85
91 Sample getSample(const std::string& path) {
92 auto it = samples.find(path);
93 if (it != samples.end()) {
94 return it->second;
95 }
96 auto sample = asw::assets::loadSample(path);
97 samples[path] = sample;
98 return sample;
99 }
100
106 bool hasSample(const std::string& path) const {
107 return samples.find(path) != samples.end();
108 }
109
110 // --- Music ---
111
117 Music getMusic(const std::string& path) {
118 auto it = music.find(path);
119 if (it != music.end()) {
120 return it->second;
121 }
122 auto m = asw::assets::loadMusic(path);
123 music[path] = m;
124 return m;
125 }
126
132 bool hasMusic(const std::string& path) const {
133 return music.find(path) != music.end();
134 }
135
136 // --- Management ---
137
142 void unloadTexture(const std::string& path) { textures.erase(path); }
143
149 void unloadFont(const std::string& path, float size) {
150 fonts.erase(path + ":" + std::to_string(size));
151 }
152
157 void unloadSample(const std::string& path) { samples.erase(path); }
158
163 void unloadMusic(const std::string& path) { music.erase(path); }
164
167 void clear() {
168 textures.clear();
169 fonts.clear();
170 samples.clear();
171 music.clear();
172 }
173
178 size_t getTextureCount() const { return textures.size(); }
179
184 size_t getFontCount() const { return fonts.size(); }
185
190 size_t getSampleCount() const { return samples.size(); }
191
196 size_t getMusicCount() const { return music.size(); }
197
202 size_t getTotalCount() const {
203 return textures.size() + fonts.size() + samples.size() + music.size();
204 }
205
206 private:
207 std::unordered_map<std::string, Texture> textures;
208 std::unordered_map<std::string, Font> fonts;
209 std::unordered_map<std::string, Sample> samples;
210 std::unordered_map<std::string, Music> music;
211 };
212
213} // namespace asw
214
215#endif // ASW_ASSET_MANAGER_H
Asset routines for the ASW library.
Centralized asset manager with caching.
void unloadFont(const std::string &path, float size)
Unload a font from the cache.
void unloadMusic(const std::string &path)
Unload music from the cache.
size_t getMusicCount() const
Get the number of cached music tracks.
std::unordered_map< std::string, Sample > samples
size_t getFontCount() const
Get the number of cached fonts.
void unloadSample(const std::string &path)
Unload a sample from the cache.
bool hasMusic(const std::string &path) const
Check if music is cached.
Texture getTexture(const std::string &path)
Get a texture by path (loads and caches on first access).
std::unordered_map< std::string, Texture > textures
Music getMusic(const std::string &path)
Get music by path (loads and caches on first access).
Font getFont(const std::string &path, float size)
Get a font by path and size (loads and caches on first access).
bool hasSample(const std::string &path) const
Check if a sample is cached.
std::unordered_map< std::string, Font > fonts
size_t getSampleCount() const
Get the number of cached samples.
Sample getSample(const std::string &path)
Get a sample by path (loads and caches on first access).
bool hasTexture(const std::string &path) const
Check if a texture is cached.
void clear()
Clear all cached assets.
bool hasFont(const std::string &path, float size) const
Check if a font is cached.
std::unordered_map< std::string, Music > music
size_t getTotalCount() const
Get the total number of cached assets.
void unloadTexture(const std::string &path)
Unload a texture from the cache.
size_t getTextureCount() const
Get the number of cached textures.
asw::Sample loadSample(const std::string &filename)
Loads a sample from a file. Formats supported are WAV, AIFF, RIFF, OGG and VOC. This will abort if th...
Definition assets.cpp:36
asw::Font loadFont(const std::string &filename, float size)
Loads a TTF font from a file. This will abort if the file is not found.
Definition assets.cpp:26
asw::Music loadMusic(const std::string &filename)
Loads a music file from a file. Formats supported are WAV, AIFF, RIFF, OGG and VOC....
Definition assets.cpp:46
asw::Texture loadTexture(const std::string &filename)
Loads a texture from a file. Formats supported are PNG, ICO, CUR, BMP, GIF, JPG, LBM,...
Definition assets.cpp:13
std::shared_ptr< Mix_Chunk > Sample
Alias for a shared pointer to an Mix_Chunk.
Definition types.h:37
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:34
std::shared_ptr< Mix_Music > Music
Alias for a shared pointer to an Mix_Music.
Definition types.h:40
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:31
Types used throughout the ASW library.