don't die after encountering a NULL texture

This commit is contained in:
fgsfds 2020-05-29 19:35:35 +03:00
parent 540a0387c9
commit 93030b02a3
2 changed files with 33 additions and 13 deletions

View File

@ -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);

View File

@ -1,6 +1,7 @@
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
@ -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();