From b4d0ad9e95226d225d5361b1182866884aaa6366 Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Thu, 30 Apr 2026 19:19:27 +0200 Subject: Structural refactor --- assets.c | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 assets.c (limited to 'assets.c') diff --git a/assets.c b/assets.c new file mode 100644 index 0000000..0b1f66c --- /dev/null +++ b/assets.c @@ -0,0 +1,81 @@ +#include "all.h" + +#include +#include +#include + +static array(CachedTexture) texture_cache; + +Texture2D GetTexture(const char *name) { + if (texture_cache.data == NULL) { + array_init(texture_cache); + } + + for (int i = 0; i < texture_cache.length; i++) { + if (strcmp(texture_cache.data[i].name, name) == 0) return texture_cache.data[i].tex; + } + + char lname[256]; + strncpy(lname, name, sizeof(lname)); + lname[sizeof(lname)-1] = '\0'; + for (int i = 0; lname[i]; i++) lname[i] = tolower(lname[i]); + + char path[256]; + void *data = NULL; + size_t size = 0; + const char *ext = ".png"; + + snprintf(path, sizeof(path), "textures/%s.png", lname); + data = vfs_read(path, &size); + if (!data) { + snprintf(path, sizeof(path), "textures/%s.jpg", lname); + data = vfs_read(path, &size); + ext = ".jpg"; + } + + Texture2D tex = { 0 }; + if (data) { + Image img = LoadImageFromMemory(ext, data, (int)size); + if (img.data) { + tex = LoadTextureFromImage(img); + UnloadImage(img); + } + vfs_free(data); + } + + if (tex.id == 0) { + TraceLog(LOG_WARNING, "Failed to load texture: '%s'", name); + Image img = GenImageChecked(64, 64, 8, 8, (Color){128, 128, 128, 255}, (Color){200, 200, 200, 255}); + tex = LoadTextureFromImage(img); + UnloadImage(img); + } else { + GenTextureMipmaps(&tex); + SetTextureFilter(tex, TEXTURE_FILTER_BILINEAR); + SetTextureWrap(tex, TEXTURE_WRAP_REPEAT); + } + + CachedTexture cached; + strncpy(cached.name, name, sizeof(cached.name)); + cached.tex = tex; + array_push(texture_cache, cached); + + return tex; +} + +void UnloadAssets(void) { + for (int i = 0; i < texture_cache.length; i++) { + UnloadTexture(texture_cache.data[i].tex); + } + array_clear(texture_cache); +} + +Font LoadFontVFS(const char *path, int fontSize) { + size_t size = 0; + void *data = vfs_read(path, &size); + if (data) { + Font font = LoadFontFromMemory(".ttf", data, (int)size, fontSize, NULL, 0); + vfs_free(data); + return font; + } + return GetFontDefault(); +} -- cgit v1.2.3