From 2ced72a475465f329577d9fea744d55cc5380695 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 9 Nov 2024 12:19:09 -0500 Subject: [PATCH] Slight improvement in the renderer efficiency. --- render.cpp | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/render.cpp b/render.cpp index 018ef71..b3fd04f 100644 --- a/render.cpp +++ b/render.cpp @@ -103,7 +103,22 @@ void SFMLRender::draw_main_ui() { $window.draw($ui_text); } +inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) { + // should look into caching all this instead of calcing it each time + sp_bounds = sprite.getLocalBounds(); + + // calculate where to center the sprite, but only if it's smaller + width_delta = bg_bounds.width > sp_bounds.width ? (bg_bounds.width - sp_bounds.width) / 2 : 0; + height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0; +} + void SFMLRender::render_text(std::string &text, float x, float y) { + wchar_t last_tile = '#'; + sf::FloatRect sp_bounds; + float width_delta = 0; + float height_delta = 0; + sf::Sprite &sprite = get_text_sprite(last_tile); + // make a copy so we don't modify the cached one $ansi.parse(text, [&](sf::Color bg, sf::Color fg, wchar_t tile) { if(tile == '\n') { @@ -114,15 +129,14 @@ void SFMLRender::render_text(std::string &text, float x, float y) { return; // skip these, just windows junk } else { $bg_sprite.setPosition({x, y}); - sf::Sprite &sprite = get_text_sprite(tile); $bg_sprite.setColor(bg); - // should look into caching all this instead of calcing it each time - auto sp_bounds = sprite.getLocalBounds(); - - // calculate where to center the sprite, but only if it's smaller - auto width_delta = $bg_bounds.width > sp_bounds.width ? ($bg_bounds.width - sp_bounds.width) / 2 : 0; - auto height_delta = $bg_bounds.height > sp_bounds.width ? ($bg_bounds.height - sp_bounds.height) / 2 : 0; + // only get a new sprite if the tile changed + if(last_tile != tile) { + last_tile = tile; // update last tile seen + sprite = get_text_sprite(tile); + configure_tile(sprite, sp_bounds, $bg_bounds, width_delta, height_delta); + } sprite.setPosition({x+width_delta, y+height_delta}); sprite.setColor(fg);