From 8eb0a7d838d93ebec8e8f62a8e4f22c93527d5c8 Mon Sep 17 00:00:00 2001 From: MysterD Date: Sat, 7 May 2022 21:15:38 -0700 Subject: [PATCH] Fix custom textures with EXTERNAL_DATA --- src/pc/gfx/gfx_pc.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/src/pc/gfx/gfx_pc.c b/src/pc/gfx/gfx_pc.c index 0238b8be..32bb7f7e 100644 --- a/src/pc/gfx/gfx_pc.c +++ b/src/pc/gfx/gfx_pc.c @@ -332,8 +332,6 @@ static bool gfx_texture_cache_lookup(int tile, struct TextureHashmapNode **n, co #undef CMPADDR } -#ifndef EXTERNAL_DATA - static void import_texture_rgba32(int tile) { uint32_t width = rdp.texture_tile.line_size_bytes / 2; uint32_t height = (rdp.loaded_texture[tile].size_bytes / 2) / rdp.texture_tile.line_size_bytes; @@ -506,7 +504,8 @@ static void import_texture_ci8(int tile) { gfx_rapi->upload_texture(rgba32_buf, width, height); } -#else // EXTERNAL_DATA + +#ifdef EXTERNAL_DATA static inline void load_texture(const char *fullpath) { int w, h; @@ -620,10 +619,25 @@ static void import_texture(int tile) { #ifdef EXTERNAL_DATA // the "texture data" is actually a C string with the path to our texture in it // load it from an external image in our data path - char texname[SYS_MAX_PATH]; - snprintf(texname, sizeof(texname), FS_TEXTUREDIR "/%s.png", (const char*)rdp.loaded_texture[tile].addr); - load_texture(texname); -#else + const char* texid = (const char*)rdp.loaded_texture[tile].addr; + + // make sure the texture id is a printable ascii string + bool texidIsPrintable = true; + char* c = (char*)texid; + while (c != NULL && *c != '\0') { + if (*c < 33 || *c > 126) { + texidIsPrintable = false; + break; + } + c++; + } + if (texidIsPrintable) { + char texname[SYS_MAX_PATH]; + snprintf(texname, sizeof(texname), FS_TEXTUREDIR "/%s.png", texid); + load_texture(texname); + return; + } +#endif // the texture data is actual texture data //int t0 = get_time(); if (fmt == G_IM_FMT_RGBA) { @@ -666,7 +680,6 @@ static void import_texture(int tile) { } //int t1 = get_time(); //printf("Time diff: %d\n", t1 - t0); -#endif } static void gfx_normalize_vector(float v[3]) {