From 867dea195804a82015a8d4e73a39410eb4d681e8 Mon Sep 17 00:00:00 2001 From: veclavtalica Date: Fri, 19 Jul 2024 23:38:10 +0300 Subject: [PATCH] textures.c: remove by one offsetiing, use macro for validity checking --- src/textures.c | 25 +++++++++++++------------ src/textures.h | 5 +---- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/textures.c b/src/textures.c index e10ff1a..9042b35 100644 --- a/src/textures.c +++ b/src/textures.c @@ -307,12 +307,12 @@ static t_texture_key textures_load(struct texture_cache *cache, const char *path upload_texture_from_surface(new_texture.loner_texture, surface); new_texture.srcrect = (t_rect) { .w = surface->w, .h = surface->h }; shput(cache->hash, path, new_texture); - return (t_texture_key){ (int)shgeti(cache->hash, path) + 1 }; + return (t_texture_key){ (int)shgeti(cache->hash, path) }; } else { new_texture.atlas_index = cache->atlas_index; shput(cache->hash, path, new_texture); cache->is_dirty = true; - return (t_texture_key){ (int)shgeti(cache->hash, path) + 1 }; + return (t_texture_key){ (int)shgeti(cache->hash, path) }; } } @@ -370,13 +370,14 @@ t_texture_key textures_get_key(struct texture_cache *cache, const char *path) { if (texture == -1) { return textures_load(cache, path); } else - return (t_texture_key){ (int)texture + 1 }; + return (t_texture_key){ (int)texture }; } +#endif /* generic implementation of textures_get_key() */ t_rect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key) { - if (key.id != 0) { - return cache->hash[key.id - 1].value.srcrect; + if (m_texture_key_is_valid(key)) { + return cache->hash[key.id].value.srcrect; } else { CRY("Texture lookup failed.", "Tried to get texture that isn't loaded."); @@ -386,9 +387,9 @@ t_rect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key t_rect textures_get_dims(const struct texture_cache *cache, t_texture_key key) { - if (key.id != 0) { - if (cache->hash[key.id - 1].value.loner_texture != 0) - return cache->hash[key.id - 1].value.srcrect; + if (m_texture_key_is_valid(key)) { + if (cache->hash[key.id].value.loner_texture != 0) + return cache->hash[key.id].value.srcrect; else return (t_rect){ .w = TEXTURE_ATLAS_SIZE, .h = TEXTURE_ATLAS_SIZE }; } else { @@ -400,11 +401,11 @@ t_rect textures_get_dims(const struct texture_cache *cache, t_texture_key key) { void textures_bind(const struct texture_cache *cache, t_texture_key key, GLenum target) { - if (key.id != 0) { - if (cache->hash[key.id - 1].value.loner_texture == 0) - glBindTexture(target, cache->atlas_textures[cache->hash[key.id - 1].value.atlas_index]); + if (m_texture_key_is_valid(key)) { + if (cache->hash[key.id].value.loner_texture == 0) + glBindTexture(target, cache->atlas_textures[cache->hash[key.id].value.atlas_index]); else - glBindTexture(target, cache->hash[key.id - 1].value.loner_texture); + glBindTexture(target, cache->hash[key.id].value.loner_texture); } else if (key.id == 0) { CRY("Texture binding failed.", "Tried to get texture that isn't loaded."); diff --git a/src/textures.h b/src/textures.h index b244208..411d13f 100644 --- a/src/textures.h +++ b/src/textures.h @@ -11,10 +11,7 @@ typedef struct { int id; } t_texture_key; /* tests whether given key structure corresponds to any texture */ -#define m_texture_key_is_valid(p_key) ((p_key).id != 0) - -/* tests whether given key corresponds to atlas texture or a loner */ -#define m_texture_is_atlas(p_key) ((p_key).id > 0) +#define m_texture_key_is_valid(p_key) ((p_key).id != -1) void textures_cache_init(struct texture_cache *cache, SDL_Window *window); void textures_cache_deinit(struct texture_cache *cache);