diff --git a/src/rendering/twn_sprites.c b/src/rendering/twn_sprites.c index 32e4704..acaa540 100644 --- a/src/rendering/twn_sprites.c +++ b/src/rendering/twn_sprites.c @@ -130,6 +130,10 @@ void render_sprite_batch(const Primitive2D primitives[], const Rect dims = textures_get_dims(&ctx.texture_cache, primitives->sprite.texture_key); + /* cached srcrect */ + Rect cached_srcrect; + TextureKey cached_srcrect_key = TEXTURE_KEY_INVALID; + /* vertex population over a vertex buffer builder interface */ { VertexBufferBuilder payload = build_vertex_buffer(vertex_array, get_quad_payload_size(batch) * batch.size); @@ -139,9 +143,12 @@ void render_sprite_batch(const Primitive2D primitives[], const size_t cur = batch.mode == TEXTURE_MODE_GHOSTLY ? i : batch.size - i - 1; const SpritePrimitive sprite = primitives[cur].sprite; - /* TODO: try caching it */ - const Rect srcrect = - textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key); + if (primitives[cur].sprite.texture_key.id != cached_srcrect_key.id) { + cached_srcrect = textures_get_srcrect(&ctx.texture_cache, primitives[cur].sprite.texture_key); + cached_srcrect_key = primitives[cur].sprite.texture_key; + } + + Rect const srcrect = cached_srcrect; Vec2 uv0, uv1, uv2, uv3; diff --git a/src/twn_textures_c.h b/src/twn_textures_c.h index c76a8c6..081d1f4 100644 --- a/src/twn_textures_c.h +++ b/src/twn_textures_c.h @@ -50,6 +50,7 @@ typedef struct TextureCache { typedef struct TextureKey { uint16_t id; } TextureKey; /* tests whether given key structure corresponds to any texture */ +#define TEXTURE_KEY_INVALID (TextureKey) { (uint16_t)-1 } #define m_texture_key_is_valid(p_key) ((p_key).id != (uint16_t)-1) void textures_cache_init(struct TextureCache *cache, SDL_Window *window);