ANSI code renderer starts working but I have to make it utf8/wchar_t friendly.

This commit is contained in:
Zed A. Shaw 2024-11-01 18:07:47 -04:00
parent 6ca4614fcb
commit ae484bf425
9 changed files with 136 additions and 139 deletions

View file

@ -1,5 +1,7 @@
#include "render.hpp"
#include "ansi_parser.hpp"
#include <cmath>
#include <fmt/core.h>
std::array<sf::Color, 10> VALUES{
sf::Color{1, 4, 2}, // black
@ -66,13 +68,16 @@ bool SFMLRender::resize_map(int new_size) {
}
}
void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
if(clear) $window.clear();
void SFMLRender::draw_main_ui() {
std::string screenout = $screen.ToString();
std::wstring main_screen_utf8 = $converter.from_bytes(screenout);
$ui_text.setString(main_screen_utf8);
$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();
std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout);
@ -82,23 +87,23 @@ void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
// make a copy so we don't modify the cached one
auto bg_sprite = get_text_sprite(L'');
auto bg_bounds = bg_sprite.getLocalBounds();
bg_sprite.setColor(sf::Color(20,20,20));
auto add_sprite = get_text_sprite(L'!');
bool has_add = false;
sf::Color def_fg(color(Value::LIGHT_LIGHT));
sf::Color def_bg(color(Value::BLACK));
for(size_t i = 0; i < map_screen_utf8.size(); i++) {
wchar_t tile = map_screen_utf8[i];
if(tile == L'\n') {
parse_ansi(map_screenout, def_fg, def_bg, [&](sf::Color bg, sf::Color fg, char tile) {
if(tile == '\n') {
// don't bother processing newlines, just skip
y += $line_spacing;
x = GAME_MAP_POS;
} else if(tile == L'\r') {
continue; // skip these, just windows junk
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});
sf::Sprite &sprite = get_text_sprite(tile);
bg_sprite.setColor(bg);
// should look into caching all this instead of calcing it each time
auto sp_bounds = sprite.getLocalBounds();
@ -107,38 +112,15 @@ void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) {
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;
// TODO: need to center it inside the bg_sprite
sprite.setPosition({x+width_delta+map_off_x, y+height_delta+map_off_y});
sprite.setColor(fg);
// get the entity combat and make them light gray if dead
if(tile == L'') {
sprite.setColor(sf::Color(80,80,80));
} else if(tile == L'') {
sprite.setColor(sf::Color::Blue);
} else if(tile == L'Ω') {
sprite.setColor(sf::Color::Red);
// HACK: just playing with adding multiple characters for drawing
add_sprite.setColor(sf::Color::Red);
add_sprite.setPosition({x-3,y-3});
has_add = true;
} else if(tile == L'#') {
sprite.setColor(sf::Color(5,5,5));
} else {
sprite.setColor(color(Value::MID));
}
// now draw the background sprite and sprite
// TODO: this can become a standard sprite description
$window.draw(bg_sprite);
$window.draw(sprite);
if(has_add) {
$window.draw(add_sprite);
has_add = false;
}
// next cell
x += $base_glyph.advance;
}
}
});
$window.display();
}