Further cleaning of the renderer.

This commit is contained in:
Zed A. Shaw 2024-11-02 17:41:19 -04:00
parent fd8180bc61
commit 24b1e4a500
2 changed files with 21 additions and 21 deletions

View file

@ -64,6 +64,8 @@ bool SFMLRender::resize_map(int new_size) {
$map_font_size = new_size;
$base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false);
$line_spacing = $font.getLineSpacing($map_font_size);
$bg_sprite = get_text_sprite(BG_TILE);
$bg_bounds = $bg_sprite.getLocalBounds();
return true;
} else {
// something else here
@ -78,19 +80,9 @@ void SFMLRender::draw_main_ui() {
$window.draw($ui_text);
}
void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
if(clear) $window.clear();
draw_main_ui();
std::string map_screenout = $map_screen.ToString();
float y = 0.0f;
float x = GAME_MAP_POS;
void SFMLRender::render_text(std::string &text, float x, float y) {
// make a copy so we don't modify the cached one
auto bg_sprite = get_text_sprite(BG_TILE);
auto bg_bounds = bg_sprite.getLocalBounds();
$ansi.parse(map_screenout, [&](sf::Color bg, sf::Color fg, wchar_t tile) {
$ansi.parse(text, [&](sf::Color bg, sf::Color fg, wchar_t tile) {
if(tile == '\n') {
// don't bother processing newlines, just skip
y += $line_spacing;
@ -98,29 +90,33 @@ void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
} else if(tile == L'\r') {
return; // skip these, just windows junk
} else {
// fmt::println("FG: {},{},{},{}; BG: {},{},{},{}; ch: {}",
// fg.r, fg.g, fg.b, fg.a, bg.r, bg.g, bg.b, bg.a, int(tile));
// it's a visual cell
bg_sprite.setPosition({x+map_off_x, y+map_off_y});
$bg_sprite.setPosition({x, y});
sf::Sprite &sprite = get_text_sprite(tile);
bg_sprite.setColor(bg);
$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;
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;
sprite.setPosition({x+width_delta+map_off_x, y+height_delta+map_off_y});
sprite.setPosition({x+width_delta, y+height_delta});
sprite.setColor(fg);
$window.draw(bg_sprite);
$window.draw($bg_sprite);
$window.draw(sprite);
// next cell
x += $base_glyph.advance;
}
});
}
void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
if(clear) $window.clear();
draw_main_ui();
std::string map_screenout = $map_screen.ToString();
render_text(map_screenout, GAME_MAP_POS+map_off_x, map_off_y);
$window.display();
}