diff --git a/ansi_parser.cpp b/ansi_parser.cpp index 4fc4228..7380bc7 100644 --- a/ansi_parser.cpp +++ b/ansi_parser.cpp @@ -87,26 +87,39 @@ static const int foo_en_main = 19; #line 84 "ansi_parser.rl" -bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default_bg, WriteCB write) { + +ANSIParser::ANSIParser(sf::Color default_fg, sf::Color default_bg) : + $default_fg(default_fg), + $default_bg(default_bg) +{ + +} + +bool ANSIParser::parse(const std::string &screen, WriteCB write) { + std::wstring screen_utf8 = $converter.from_bytes(screen); + return parse(screen_utf8, write); +} + +bool ANSIParser::parse(std::wstring_view codes, WriteCB write) { const wchar_t *start = NULL; int cs = 0; unsigned int value = 0; const wchar_t *p = codes.data(); const wchar_t *pe = p + codes.size(); const wchar_t *eof = pe; - sf::Color bgcolor(default_bg); - sf::Color color(default_fg); + sf::Color bgcolor($default_bg); + sf::Color color($default_fg); sf::Color &target = color; -#line 94 "ansi_parser.cpp" +#line 107 "ansi_parser.cpp" { cs = foo_start; } -#line 97 "ansi_parser.rl" +#line 110 "ansi_parser.rl" -#line 97 "ansi_parser.cpp" +#line 110 "ansi_parser.cpp" { int _klen; unsigned int _trans; @@ -233,11 +246,11 @@ _match: break; case 6: #line 52 "ansi_parser.rl" - { color = default_fg; } + { color = $default_fg; } break; case 7: #line 53 "ansi_parser.rl" - { bgcolor = default_bg; } + { bgcolor = $default_bg; } break; case 8: #line 55 "ansi_parser.rl" @@ -259,7 +272,7 @@ _match: #line 59 "ansi_parser.rl" { } break; -#line 236 "ansi_parser.cpp" +#line 249 "ansi_parser.cpp" } } @@ -279,7 +292,7 @@ _again: #line 59 "ansi_parser.rl" { } break; -#line 254 "ansi_parser.cpp" +#line 267 "ansi_parser.cpp" } } } @@ -287,7 +300,7 @@ _again: _out: {} } -#line 98 "ansi_parser.rl" +#line 111 "ansi_parser.rl" return p - pe == 0; } diff --git a/ansi_parser.hpp b/ansi_parser.hpp index f0b0043..b3239d9 100644 --- a/ansi_parser.hpp +++ b/ansi_parser.hpp @@ -1,7 +1,21 @@ #pragma once #include #include +#include typedef std::function WriteCB; -bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default_bg, WriteCB write); +class ANSIParser { + sf::Color $default_fg; + sf::Color $default_bg; + std::wstring_convert> $converter; + +public: + ANSIParser(sf::Color default_fg, sf::Color default_bg); + + // disable copying + ANSIParser(ANSIParser& ap) = delete; + + bool parse(const std::string &screen, WriteCB write); + bool parse(std::wstring_view codes, WriteCB write); +}; diff --git a/ansi_parser.rl b/ansi_parser.rl index 07f8c6f..e000928 100644 --- a/ansi_parser.rl +++ b/ansi_parser.rl @@ -49,8 +49,8 @@ using namespace fmt; write(bgcolor, color, fc); } - action reset_fg { color = default_fg; } - action reset_bg { bgcolor = default_bg; } + action reset_fg { color = $default_fg; } + action reset_bg { bgcolor = $default_bg; } action red { target.r = value; } action blue { target.g = value; } @@ -82,15 +82,28 @@ using namespace fmt; %% write data; -bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default_bg, WriteCB write) { + +ANSIParser::ANSIParser(sf::Color default_fg, sf::Color default_bg) : + $default_fg(default_fg), + $default_bg(default_bg) +{ + +} + +bool ANSIParser::parse(const std::string &screen, WriteCB write) { + std::wstring screen_utf8 = $converter.from_bytes(screen); + return parse(screen_utf8, write); +} + +bool ANSIParser::parse(std::wstring_view codes, WriteCB write) { const wchar_t *start = NULL; int cs = 0; unsigned int value = 0; const wchar_t *p = codes.data(); const wchar_t *pe = p + codes.size(); const wchar_t *eof = pe; - sf::Color bgcolor(default_bg); - sf::Color color(default_fg); + sf::Color bgcolor($default_bg); + sf::Color color($default_fg); sf::Color &target = color; %% write init; diff --git a/render.cpp b/render.cpp index 03a7f64..3fd898b 100644 --- a/render.cpp +++ b/render.cpp @@ -30,7 +30,10 @@ SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) : $line_spacing(0), $canvas(canvas), $map_screen(map_screen), - $screen(screen) + $screen(screen), + $default_fg(color(Value::LIGHT_MID)), + $default_bg(color(Value::BLACK)), + $ansi($default_fg, $default_bg) { $font.loadFromFile("./assets/text.otf"); $ui_text.setFont($font); @@ -80,17 +83,14 @@ void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) { draw_main_ui(); std::string map_screenout = $map_screen.ToString(); - std::wstring map_screen_utf8 = $converter.from_bytes(map_screenout); float y = 0.0f; float x = GAME_MAP_POS; // 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(); - sf::Color def_fg(color(Value::MID)); - sf::Color def_bg(color(Value::BLACK)); - parse_ansi(map_screen_utf8, def_fg, def_bg, [&](sf::Color bg, sf::Color fg, wchar_t tile) { + $ansi.parse(map_screenout, [&](sf::Color bg, sf::Color fg, wchar_t tile) { if(tile == '\n') { // don't bother processing newlines, just skip y += $line_spacing; diff --git a/render.hpp b/render.hpp index e264fc9..a9ad4f0 100644 --- a/render.hpp +++ b/render.hpp @@ -7,6 +7,7 @@ #include #include "point.hpp" #include +#include "ansi_parser.hpp" using ftxui::Canvas, ftxui::Screen; @@ -41,6 +42,9 @@ struct SFMLRender { Screen& $screen; sf::Text $ui_text; std::wstring_convert> $converter; + sf::Color $default_fg; + sf::Color $default_bg; + ANSIParser $ansi; SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen); diff --git a/tests/ansi_parser.cpp b/tests/ansi_parser.cpp index fd35f29..8cb96b4 100644 --- a/tests/ansi_parser.cpp +++ b/tests/ansi_parser.cpp @@ -53,14 +53,11 @@ std::string generate_colors() { TEST_CASE("test out ragel parser", "[gui]") { std::string colors = generate_colors(); - - std::wstring_convert> converter; - std::wstring colors_utf8 = converter.from_bytes(colors); - sf::Color default_fg(0,0,0); sf::Color default_bg(100,100,100); + ANSIParser parser(default_fg, default_bg); - bool good = parse_ansi(colors_utf8, default_fg, default_bg, [&](sf::Color bgcolor, sf::Color color, wchar_t ch) { + bool good = parser.parse(colors, [&](sf::Color bgcolor, sf::Color color, wchar_t ch) { bool correct_char = ch == '#' || ch == ' ' || ch == '\n' || ch == '\r'; // println("FG: {},{},{},{}; BG: {},{},{},{}; ch: {}", // color.r, color.g, color.b, color.a,