From 111429b974472ef0b0c234842405c97f0b43c7c1 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 18 Nov 2024 22:12:40 -0500 Subject: [PATCH] A slight improvement to avoid rendering boxes behind text that is default bg color. --- render.cpp | 34 ++++++++++++++++++---------------- render.hpp | 2 +- scratchpad/img2ansi.cpp | 2 ++ 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/render.cpp b/render.cpp index a4155cb..6398394 100644 --- a/render.cpp +++ b/render.cpp @@ -133,31 +133,30 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) { } inline sf::FloatRect draw_chunk(sf::RenderWindow& window, - sf::FloatRect ui_bounds, sf::Text& text, + sf::FloatRect ui_bounds, sf::Text& text, sf::Color default_bg, sf::Color bgcolor, float x, float y, std::wstring &out) { text.setString(out); text.setPosition({x, y}); // get a base character for the cell size sf::FloatRect bounds(x, y, ui_bounds.width * out.size(), ui_bounds.height); - sf::RectangleShape backing({bounds.width, bounds.height}); - backing.setFillColor(bgcolor); - backing.setPosition({bounds.left, bounds.top + BG_BOX_OFFSET}); -#ifdef DEBUG - backing.setOutlineColor(int(y) % 4 ? sf::Color::Red : sf::Color::Yellow); - backing.setOutlineThickness(1); -#endif - window.draw(backing); + if(default_bg != bgcolor) { + sf::RectangleShape backing({bounds.width, bounds.height}); + backing.setFillColor(bgcolor); + backing.setPosition({bounds.left, bounds.top + BG_BOX_OFFSET}); + window.draw(backing); + } + window.draw(text); out.clear(); return bounds; } -void SFMLRender::render_text(const std::wstring &text, float start_x, float start_y) { +void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, float start_x, float start_y) { std::wstring out; float x = start_x; float y = start_y; - sf::Color bgcolor = $default_bg; + sf::Color cur_bg = default_bg; // start with the default_fg until it's changed $ui_text.setFillColor($default_fg); @@ -165,10 +164,12 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star $ansi.parse(text, [&](sf::Color fg, sf::Color bg) { if(out.size() > 0 ) { - auto bounds = draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out); + auto bounds = draw_chunk($window, + $ui_bounds, $ui_text, + default_bg, cur_bg, x, y, out); x += bounds.width; } - bgcolor = bg; + cur_bg = bg; $ui_text.setFillColor(fg); }, [&](wchar_t tile) { @@ -178,7 +179,8 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star sf::FloatRect bounds; if(out.size() > 0) { - bounds = draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out); + bounds = draw_chunk($window, $ui_bounds, + $ui_text, default_bg, cur_bg, x, y, out); } else { bounds = $ui_text.getLocalBounds(); } @@ -195,7 +197,7 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star ); if(out.size() > 0) { - draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out); + draw_chunk($window, $ui_bounds, $ui_text, default_bg, cur_bg, x, y, out); } } @@ -219,6 +221,6 @@ void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) { backing.setPosition(panel.x + x_offset, panel.y + y_offset); $window.draw(backing); - render_text(panelout, panel.x, panel.y); + render_text(panelout, panel.default_bg, panel.x, panel.y); } } diff --git a/render.hpp b/render.hpp index d4a412f..6eb12e4 100644 --- a/render.hpp +++ b/render.hpp @@ -49,7 +49,7 @@ struct SFMLRender { sf::Sprite &get_text_sprite(wchar_t tile); bool resize_map(int new_size, Point &view_port); void render_grid(const std::wstring &text, float x, float y); - void render_text(const std::wstring &text, float x, float y); + void render_text(const std::wstring &text, sf::Color bgcolor, float x, float y); void draw(Panel &panel, float x_offset=0.0f, float y_offset=0.0f); diff --git a/scratchpad/img2ansi.cpp b/scratchpad/img2ansi.cpp index 21d991a..90bea16 100644 --- a/scratchpad/img2ansi.cpp +++ b/scratchpad/img2ansi.cpp @@ -21,7 +21,9 @@ int main(int argc, char *argv[]) { image.loadFromFile(image_file); // create a grid panel to hold the cells + Panel panel(40,40, 0, 0, true); // divide the image into cells + // for each cell: // get the color // calculate value