diff options
| author | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-04-30 19:19:27 +0200 |
|---|---|---|
| committer | Mitja Felicijan <mitja.felicijan@gmail.com> | 2026-04-30 19:44:07 +0200 |
| commit | b4d0ad9e95226d225d5361b1182866884aaa6366 (patch) | |
| tree | 5440b34d97e89a08fc01abd49bb80e5b50dda945 /assets.c | |
| parent | ddde2e8fdf05efac5914ca2b812b7762b78ba9ec (diff) | |
| download | stalag-b4d0ad9e95226d225d5361b1182866884aaa6366.tar.gz | |
Structural refactor
Diffstat (limited to 'assets.c')
| -rw-r--r-- | assets.c | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/assets.c b/assets.c new file mode 100644 index 0000000..0b1f66c --- /dev/null +++ b/assets.c | |||
| @@ -0,0 +1,81 @@ | |||
| 1 | #include "all.h" | ||
| 2 | |||
| 3 | #include <stdio.h> | ||
| 4 | #include <string.h> | ||
| 5 | #include <ctype.h> | ||
| 6 | |||
| 7 | static array(CachedTexture) texture_cache; | ||
| 8 | |||
| 9 | Texture2D GetTexture(const char *name) { | ||
| 10 | if (texture_cache.data == NULL) { | ||
| 11 | array_init(texture_cache); | ||
| 12 | } | ||
| 13 | |||
| 14 | for (int i = 0; i < texture_cache.length; i++) { | ||
| 15 | if (strcmp(texture_cache.data[i].name, name) == 0) return texture_cache.data[i].tex; | ||
| 16 | } | ||
| 17 | |||
| 18 | char lname[256]; | ||
| 19 | strncpy(lname, name, sizeof(lname)); | ||
| 20 | lname[sizeof(lname)-1] = '\0'; | ||
| 21 | for (int i = 0; lname[i]; i++) lname[i] = tolower(lname[i]); | ||
| 22 | |||
| 23 | char path[256]; | ||
| 24 | void *data = NULL; | ||
| 25 | size_t size = 0; | ||
| 26 | const char *ext = ".png"; | ||
| 27 | |||
| 28 | snprintf(path, sizeof(path), "textures/%s.png", lname); | ||
| 29 | data = vfs_read(path, &size); | ||
| 30 | if (!data) { | ||
| 31 | snprintf(path, sizeof(path), "textures/%s.jpg", lname); | ||
| 32 | data = vfs_read(path, &size); | ||
| 33 | ext = ".jpg"; | ||
| 34 | } | ||
| 35 | |||
| 36 | Texture2D tex = { 0 }; | ||
| 37 | if (data) { | ||
| 38 | Image img = LoadImageFromMemory(ext, data, (int)size); | ||
| 39 | if (img.data) { | ||
| 40 | tex = LoadTextureFromImage(img); | ||
| 41 | UnloadImage(img); | ||
| 42 | } | ||
| 43 | vfs_free(data); | ||
| 44 | } | ||
| 45 | |||
| 46 | if (tex.id == 0) { | ||
| 47 | TraceLog(LOG_WARNING, "Failed to load texture: '%s'", name); | ||
| 48 | Image img = GenImageChecked(64, 64, 8, 8, (Color){128, 128, 128, 255}, (Color){200, 200, 200, 255}); | ||
| 49 | tex = LoadTextureFromImage(img); | ||
| 50 | UnloadImage(img); | ||
| 51 | } else { | ||
| 52 | GenTextureMipmaps(&tex); | ||
| 53 | SetTextureFilter(tex, TEXTURE_FILTER_BILINEAR); | ||
| 54 | SetTextureWrap(tex, TEXTURE_WRAP_REPEAT); | ||
| 55 | } | ||
| 56 | |||
| 57 | CachedTexture cached; | ||
| 58 | strncpy(cached.name, name, sizeof(cached.name)); | ||
| 59 | cached.tex = tex; | ||
| 60 | array_push(texture_cache, cached); | ||
| 61 | |||
| 62 | return tex; | ||
| 63 | } | ||
| 64 | |||
| 65 | void UnloadAssets(void) { | ||
| 66 | for (int i = 0; i < texture_cache.length; i++) { | ||
| 67 | UnloadTexture(texture_cache.data[i].tex); | ||
| 68 | } | ||
| 69 | array_clear(texture_cache); | ||
| 70 | } | ||
| 71 | |||
| 72 | Font LoadFontVFS(const char *path, int fontSize) { | ||
| 73 | size_t size = 0; | ||
| 74 | void *data = vfs_read(path, &size); | ||
| 75 | if (data) { | ||
| 76 | Font font = LoadFontFromMemory(".ttf", data, (int)size, fontSize, NULL, 0); | ||
| 77 | vfs_free(data); | ||
| 78 | return font; | ||
| 79 | } | ||
| 80 | return GetFontDefault(); | ||
| 81 | } | ||
