A slight improvement to avoid rendering boxes behind text that is default bg color.

main
Zed A. Shaw 1 month ago
parent be144e2a05
commit 111429b974
  1. 34
      render.cpp
  2. 2
      render.hpp
  3. 2
      scratchpad/img2ansi.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);
}
}

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

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

Loading…
Cancel
Save