A bit of some clean up, API unifying, and some performance tweaks.

This commit is contained in:
Zed A. Shaw 2024-11-22 23:12:18 -05:00
parent a4926bedcb
commit fb1fd9d8bc
8 changed files with 72 additions and 42 deletions

View file

@ -69,19 +69,21 @@ inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds,
height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0;
}
void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
void SFMLRender::render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, 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);
const float start_x = x;
sf::Color cur_fg = $default_fg;
// BUG: get default_fg from panel too
sf::Color cur_fg = default_fg;
sf::Color cur_bg = default_bg;
// make a copy so we don't modify the cached one
$ansi.parse(text, [&](sf::Color fg, sf::Color bg) {
cur_fg = fg;
$bg_sprite.setColor(bg);
cur_bg = bg;
},
[&](wchar_t tile) {
@ -94,7 +96,6 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
}
break;
default: {
$bg_sprite.setPosition({x, y});
// only get a new sprite if the tile changed
if(last_tile != tile) {
@ -106,7 +107,13 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) {
sprite.setPosition({x+width_delta, y+height_delta});
sprite.setColor(cur_fg);
$window.draw($bg_sprite);
// only draw background char if it's different from default
if(cur_bg != default_bg) {
$bg_sprite.setPosition({x, y});
$bg_sprite.setColor(cur_bg);
$window.draw($bg_sprite);
}
$window.draw(sprite);
// next cell
x += $base_glyph.advance;
@ -135,14 +142,14 @@ inline sf::FloatRect draw_chunk(sf::RenderWindow& window,
return bounds;
}
void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, float start_x, float start_y) {
void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float start_x, float start_y) {
std::wstring out;
float x = start_x;
float y = start_y;
sf::Color cur_bg = default_bg;
// start with the default_fg until it's changed
$ui_text.setFillColor($default_fg);
$ui_text.setFillColor(default_fg);
$ansi.parse(text,
[&](sf::Color fg, sf::Color bg) {
@ -184,26 +191,33 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, flo
}
}
/*
* Does not render the panel, you have to do that so you can control
* when things render.
*/
void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) {
panel.render();
const std::wstring &panelout = panel.to_string();
// BUG: ui vs bg doesn't make sense. maybe grid vs. text?
auto bounds = panel.grid ? $bg_bounds : $ui_bounds;
sf::RectangleShape backing(
sf::Vector2f(bounds.width * panel.width + panel.border_px,
bounds.height * panel.height + panel.border_px));
backing.setFillColor(panel.default_bg);
if(panel.has_border) {
backing.setOutlineColor(panel.border_color);
backing.setOutlineThickness(panel.border_px);
}
backing.setPosition(panel.x + x_offset, panel.y + y_offset);
$window.draw(backing);
if(panel.grid) {
render_grid(panelout, panel.x + x_offset, panel.y + y_offset);
render_grid(panelout, panel.default_fg, panel.default_bg, panel.x + x_offset, panel.y + y_offset);
} else {
sf::RectangleShape backing(
sf::Vector2f($ui_bounds.width * panel.width + panel.border_px,
$ui_bounds.height * panel.height + panel.border_px));
backing.setFillColor(panel.default_bg);
if(panel.has_border) {
backing.setOutlineColor(panel.border_color);
backing.setOutlineThickness(panel.border_px);
}
backing.setPosition(panel.x + x_offset, panel.y + y_offset);
$window.draw(backing);
render_text(panelout, panel.default_bg, panel.x, panel.y);
render_text(panelout, panel.default_fg, panel.default_bg, panel.x + x_offset, panel.y + y_offset);
}
}