Further cleaning of the renderer.

main
Zed A. Shaw 2 months ago
parent fd8180bc61
commit 24b1e4a500
  1. 38
      render.cpp
  2. 4
      render.hpp

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

@ -5,6 +5,7 @@
#include <SFML/Window.hpp>
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/Graphics/Rect.hpp>
#include "point.hpp"
#include <codecvt>
#include "ansi_parser.hpp"
@ -37,6 +38,8 @@ struct SFMLRender {
sf::Font $font;
sf::Texture $font_texture;
sf::Glyph $base_glyph;
sf::Sprite $bg_sprite;
sf::FloatRect $bg_bounds;
Canvas& $canvas;
Screen& $map_screen;
Screen& $screen;
@ -55,6 +58,7 @@ struct SFMLRender {
sf::Color color(Value val);
sf::Sprite &get_text_sprite(wchar_t tile);
bool resize_map(int new_size);
void render_text(std::string &text, float x, float y);
void draw_main_ui();
void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f);

Loading…
Cancel
Save