textures.c: remove by one offsetiing, use macro for validity checking

This commit is contained in:
veclav talica 2024-07-19 23:38:10 +03:00
parent 765e6bb8a0
commit 867dea1958
2 changed files with 14 additions and 16 deletions

View File

@ -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); upload_texture_from_surface(new_texture.loner_texture, surface);
new_texture.srcrect = (t_rect) { .w = surface->w, .h = surface->h }; new_texture.srcrect = (t_rect) { .w = surface->w, .h = surface->h };
shput(cache->hash, path, new_texture); 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 { } else {
new_texture.atlas_index = cache->atlas_index; new_texture.atlas_index = cache->atlas_index;
shput(cache->hash, path, new_texture); shput(cache->hash, path, new_texture);
cache->is_dirty = true; 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) { if (texture == -1) {
return textures_load(cache, path); return textures_load(cache, path);
} else } 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) { t_rect textures_get_srcrect(const struct texture_cache *cache, t_texture_key key) {
if (key.id != 0) { if (m_texture_key_is_valid(key)) {
return cache->hash[key.id - 1].value.srcrect; return cache->hash[key.id].value.srcrect;
} else { } else {
CRY("Texture lookup failed.", CRY("Texture lookup failed.",
"Tried to get texture that isn't loaded."); "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) { t_rect textures_get_dims(const struct texture_cache *cache, t_texture_key key) {
if (key.id != 0) { if (m_texture_key_is_valid(key)) {
if (cache->hash[key.id - 1].value.loner_texture != 0) if (cache->hash[key.id].value.loner_texture != 0)
return cache->hash[key.id - 1].value.srcrect; return cache->hash[key.id].value.srcrect;
else else
return (t_rect){ .w = TEXTURE_ATLAS_SIZE, .h = TEXTURE_ATLAS_SIZE }; return (t_rect){ .w = TEXTURE_ATLAS_SIZE, .h = TEXTURE_ATLAS_SIZE };
} else { } 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) { void textures_bind(const struct texture_cache *cache, t_texture_key key, GLenum target) {
if (key.id != 0) { if (m_texture_key_is_valid(key)) {
if (cache->hash[key.id - 1].value.loner_texture == 0) if (cache->hash[key.id].value.loner_texture == 0)
glBindTexture(target, cache->atlas_textures[cache->hash[key.id - 1].value.atlas_index]); glBindTexture(target, cache->atlas_textures[cache->hash[key.id].value.atlas_index]);
else 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) { } else if (key.id == 0) {
CRY("Texture binding failed.", CRY("Texture binding failed.",
"Tried to get texture that isn't loaded."); "Tried to get texture that isn't loaded.");

View File

@ -11,10 +11,7 @@
typedef struct { int id; } t_texture_key; typedef struct { int id; } t_texture_key;
/* tests whether given key structure corresponds to any texture */ /* tests whether given key structure corresponds to any texture */
#define m_texture_key_is_valid(p_key) ((p_key).id != 0) #define m_texture_key_is_valid(p_key) ((p_key).id != -1)
/* tests whether given key corresponds to atlas texture or a loner */
#define m_texture_is_atlas(p_key) ((p_key).id > 0)
void textures_cache_init(struct texture_cache *cache, SDL_Window *window); void textures_cache_init(struct texture_cache *cache, SDL_Window *window);
void textures_cache_deinit(struct texture_cache *cache); void textures_cache_deinit(struct texture_cache *cache);