From 4a41f47a58d983004d0df10ab8f484a8b3f8e085 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Fri, 7 Mar 2025 06:19:44 +0300 Subject: [PATCH] twn_textures: fix freeing of missed texture --- src/twn_textures.c | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/src/twn_textures.c b/src/twn_textures.c index a86e095..fadeb25 100644 --- a/src/twn_textures.c +++ b/src/twn_textures.c @@ -73,19 +73,20 @@ static SDL_Surface *gen_missing_texture_surface(void) { SDL_LockMutex(textures_load_mutex); if (!missing_texture_surface) { - uint8_t *data = SDL_malloc(64 * 64 * 3); - for (int y = 0; y < 64; ++y) { - for (int x = 0; x < 64; ++x) { + int const dim = + TEXTURE_BORDER_REPEAT_SIZE * 2; + uint8_t *data = SDL_malloc(dim * dim * 3); + for (int y = 0; y < dim; ++y) { + for (int x = 0; x < dim; ++x) { /* diagonal stripes, chosen so that asked pixel uvs are corresponding to final output */ - data[(y * 64 + x) * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0; - data[(y * 64 + x) * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0; - data[(y * 64 + x) * 3 + 2] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0; + data[(y * dim + x) * 3 + 0] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0; + data[(y * dim + x) * 3 + 1] = (x / 2 + y / 2) % 2 == 0 ? 0 : 0; + data[(y * dim + x) * 3 + 2] = (x / 2 + y / 2) % 2 == 0 ? 175 : 0; } } - missing_texture_surface = SDL_CreateRGBSurfaceFrom(data, 64, 64, + missing_texture_surface = SDL_CreateRGBSurfaceFrom(data, dim, dim, 3 * 8, - 64 * 3, + dim * 3, rmask, gmask, bmask, 0); } @@ -299,10 +300,8 @@ static stbrp_rect *create_rects_from_cache(TextureCache *cache) { continue; /* only put it once */ - if (!missing_texture_used && cache->hash[i].value.data == missing_texture_surface) { + if (!missing_texture_used && cache->hash[i].value.data == missing_texture_surface) missing_texture_used = true; - continue; - } const SDL_Surface *surface_data = cache->hash[i].value.data; stbrp_rect new_rect = { @@ -412,12 +411,12 @@ void textures_cache_deinit(TextureCache *cache) { /* free cache hashes */ for (size_t i = 0; i < shlenu(cache->hash); ++i) { /* TODO: better to have field that stores the source of memory directly, ugh */ - if (cache->hash[i].value.srcrect.w < 2048 && cache->hash[i].value.srcrect.h < 2048) - (void)0; /* do nothing, memory owned by surface */ - else if (missing_texture_surface == NULL || cache->hash[i].value.data->pixels != missing_texture_surface->pixels) - stbi_image_free(cache->hash[i].value.data->pixels); - else + if (missing_texture_surface != NULL && cache->hash[i].value.data->pixels == missing_texture_surface->pixels) SDL_free(cache->hash[i].value.data->pixels); + else if (cache->hash[i].value.srcrect.w < 2048 && cache->hash[i].value.srcrect.h < 2048) + (void)0; /* do nothing, memory owned by surface */ + else + stbi_image_free(cache->hash[i].value.data->pixels); SDL_FreeSurface(cache->hash[i].value.data); } shfree(cache->hash);