From 93030b02a3451f95c97103bec3f114e448c6ed7f Mon Sep 17 00:00:00 2001 From: fgsfds Date: Fri, 29 May 2020 19:35:35 +0300 Subject: [PATCH] don't die after encountering a NULL texture --- src/game/ingame_menu.c | 2 ++ src/pc/gfx/gfx_pc.c | 44 +++++++++++++++++++++++++++++------------- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/game/ingame_menu.c b/src/game/ingame_menu.c index 6f3d84b9..9711ec7e 100644 --- a/src/game/ingame_menu.c +++ b/src/game/ingame_menu.c @@ -245,6 +245,7 @@ static inline u8 *convert_ia8_char(u8 c, u16 *tex, s16 w, s16 h) { #ifdef EXTERNAL_TEXTURES return (u8 *)tex; // the data's just a name #else + if (!tex) return NULL; if (!charCache[c].used) { charCache[c].used = 1; alloc_ia8_text_from_i1(charCache[c].data, tex, w, h); @@ -305,6 +306,7 @@ static u8 *convert_ia4_char(u8 c, u8 *tex, s16 w, s16 h) { #ifdef EXTERNAL_TEXTURES return tex; // the data's just a name #else + if (!tex) return NULL; if (!charCache[c].used) { charCache[c].used = 1; alloc_ia4_tex_from_i1(charCache[c].data, tex, w, h); diff --git a/src/pc/gfx/gfx_pc.c b/src/pc/gfx/gfx_pc.c index c0900831..6bd2fca4 100644 --- a/src/pc/gfx/gfx_pc.c +++ b/src/pc/gfx/gfx_pc.c @@ -1,6 +1,7 @@ #include #include #include +#include #include #include #include @@ -573,30 +574,47 @@ static bool preload_texture(const char *path) { return true; } +static inline void load_texture(const char *name) { + static char fpath[SYS_MAX_PATH]; + int w, h; + const char *texname = name; + + if (!texname[0]) { + fprintf(stderr, "empty texture name at %p\n", texname); + return; + } + + snprintf(fpath, sizeof(fpath), "%s/%s.png", sys_data_path(), texname); + u8 *data = stbi_load(fpath, &w, &h, NULL, 4); + if (!data) { + fprintf(stderr, "could not load texture: `%s`\n", fpath); + return; + } + + gfx_rapi->upload_texture(data, w, h); + stbi_image_free(data); // don't need this anymore +} + #endif // EXTERNAL_TEXTURES static void import_texture(int tile) { uint8_t fmt = rdp.texture_tile.fmt; uint8_t siz = rdp.texture_tile.siz; - + + if (!rdp.loaded_texture[tile].addr) { + fprintf(stderr, "NULL texture: tile %d, format %d/%d, size %d\n", + tile, (int)fmt, (int)siz, (int)rdp.loaded_texture[tile].size_bytes); + return; + } + if (gfx_texture_cache_lookup(tile, &rendering_state.textures[tile], rdp.loaded_texture[tile].addr, fmt, siz)) { return; } - + #ifdef EXTERNAL_TEXTURES // 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 - static char fpath[SYS_MAX_PATH]; - int w, h; - const char *texname = (const char*)rdp.loaded_texture[tile].addr; - snprintf(fpath, sizeof(fpath), "%s/%s.png", sys_data_path(), texname); - u8 *data = stbi_load(fpath, &w, &h, NULL, 4); - if (!data) { - fprintf(stderr, "could not load texture: `%s`\n", fpath); - abort(); - } - gfx_rapi->upload_texture(data, w, h); - stbi_image_free(data); // don't need this anymore + load_texture((const char*)rdp.loaded_texture[tile].addr); #else // the texture data is actual texture data int t0 = get_time();