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);
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.");

View File

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