From e864e14eab4f8e7e7035b26a10d507065eb13502 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 2 Nov 2024 06:02:13 -0400 Subject: [PATCH] Rendering with color is working now but still has problems with enabling/resetting the default colors. --- ansi_parser.cpp | 58 ++++++++++++++++++++++++++----------------------- ansi_parser.rl | 10 ++++++--- main.cpp | 5 +++++ map.hpp | 5 +++-- render.cpp | 4 ++-- render.hpp | 2 ++ status.txt | 9 -------- systems.cpp | 9 ++++++-- 8 files changed, 57 insertions(+), 45 deletions(-) diff --git a/ansi_parser.cpp b/ansi_parser.cpp index 0c36f62..4fc4228 100644 --- a/ansi_parser.cpp +++ b/ansi_parser.cpp @@ -1,5 +1,5 @@ -#line 1 ".\\ansi_parser.rl" +#line 1 "ansi_parser.rl" #include #include #include @@ -10,11 +10,11 @@ using namespace fmt; -#line 77 ".\\ansi_parser.rl" +#line 81 "ansi_parser.rl" -#line 13 ".\\ansi_parser.cpp" +#line 13 "ansi_parser.cpp" static const char _foo_actions[] = { 0, 1, 0, 1, 3, 1, 4, 1, 5, 1, 6, 1, 7, 1, 11, 1, @@ -85,7 +85,7 @@ static const int foo_error = 0; static const int foo_en_main = 19; -#line 80 ".\\ansi_parser.rl" +#line 84 "ansi_parser.rl" bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default_bg, WriteCB write) { const wchar_t *start = NULL; @@ -96,17 +96,17 @@ bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default const wchar_t *eof = pe; sf::Color bgcolor(default_bg); sf::Color color(default_fg); - sf::Color &target = bgcolor; + sf::Color &target = color; -#line 94 ".\\ansi_parser.cpp" +#line 94 "ansi_parser.cpp" { cs = foo_start; } -#line 93 ".\\ansi_parser.rl" +#line 97 "ansi_parser.rl" -#line 97 ".\\ansi_parser.cpp" +#line 97 "ansi_parser.cpp" { int _klen; unsigned int _trans; @@ -180,13 +180,13 @@ _match: switch ( *_acts++ ) { case 0: -#line 14 ".\\ansi_parser.rl" +#line 14 "ansi_parser.rl" { start = p; } break; case 1: -#line 18 ".\\ansi_parser.rl" +#line 18 "ansi_parser.rl" { value = 0; size_t len = p - start; @@ -210,52 +210,56 @@ _match: } break; case 2: -#line 40 ".\\ansi_parser.rl" +#line 40 "ansi_parser.rl" { } break; case 3: -#line 41 ".\\ansi_parser.rl" - { target = color; } +#line 41 "ansi_parser.rl" + { + target = color; + } break; case 4: -#line 42 ".\\ansi_parser.rl" - { target = bgcolor; } +#line 44 "ansi_parser.rl" + { + target = bgcolor; + } break; case 5: -#line 44 ".\\ansi_parser.rl" +#line 48 "ansi_parser.rl" { write(bgcolor, color, (*p)); } break; case 6: -#line 48 ".\\ansi_parser.rl" +#line 52 "ansi_parser.rl" { color = default_fg; } break; case 7: -#line 49 ".\\ansi_parser.rl" +#line 53 "ansi_parser.rl" { bgcolor = default_bg; } break; case 8: -#line 51 ".\\ansi_parser.rl" +#line 55 "ansi_parser.rl" { target.r = value; } break; case 9: -#line 52 ".\\ansi_parser.rl" +#line 56 "ansi_parser.rl" { target.g = value; } break; case 10: -#line 53 ".\\ansi_parser.rl" +#line 57 "ansi_parser.rl" { target.b = value; } break; case 11: -#line 54 ".\\ansi_parser.rl" +#line 58 "ansi_parser.rl" { value = 0; } break; case 12: -#line 55 ".\\ansi_parser.rl" +#line 59 "ansi_parser.rl" { } break; -#line 232 ".\\ansi_parser.cpp" +#line 236 "ansi_parser.cpp" } } @@ -272,10 +276,10 @@ _again: while ( __nacts-- > 0 ) { switch ( *__acts++ ) { case 12: -#line 55 ".\\ansi_parser.rl" +#line 59 "ansi_parser.rl" { } break; -#line 250 ".\\ansi_parser.cpp" +#line 254 "ansi_parser.cpp" } } } @@ -283,7 +287,7 @@ _again: _out: {} } -#line 94 ".\\ansi_parser.rl" +#line 98 "ansi_parser.rl" return p - pe == 0; } diff --git a/ansi_parser.rl b/ansi_parser.rl index 7434b9d..07f8c6f 100644 --- a/ansi_parser.rl +++ b/ansi_parser.rl @@ -38,8 +38,12 @@ using namespace fmt; } action color24b { } - action is_fg { target = color; } - action is_bg { target = bgcolor; } + action is_fg { + target = color; + } + action is_bg { + target = bgcolor; + } action out { write(bgcolor, color, fc); @@ -87,7 +91,7 @@ bool parse_ansi(std::wstring_view codes, sf::Color default_fg, sf::Color default const wchar_t *eof = pe; sf::Color bgcolor(default_bg); sf::Color color(default_fg); - sf::Color &target = bgcolor; + sf::Color &target = color; %% write init; %% write exec; diff --git a/main.cpp b/main.cpp index fbe700f..7c2c0ff 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,10 @@ #include "collider.hpp" #include "render.hpp" +#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor + +using namespace ftxui; + /* * This needs to be turned into a real world generator * system. @@ -47,6 +51,7 @@ void configure_world(DinkyECS::World &world, Map &game_map) { int main() { + Terminal::SetColorSupport(Terminal::Color::TrueColor); DinkyECS::World world; Map game_map(GAME_MAP_X, GAME_MAP_Y); game_map.generate(); diff --git a/map.hpp b/map.hpp index 80bdd98..680fa22 100644 --- a/map.hpp +++ b/map.hpp @@ -12,9 +12,10 @@ #define WALL_VALUE 1 #define SPACE_VALUE 0 #define WALL_TILE "█" -#define FLOOR_TILE "." -#define PLAYER_TILE "☺" +#define FLOOR_TILE "\u2849" +#define PLAYER_TILE "♣" #define ENEMY_TILE "Ω" +#define BG_TILE L'█' struct Room { size_t x = 0; diff --git a/render.cpp b/render.cpp index a737def..03a7f64 100644 --- a/render.cpp +++ b/render.cpp @@ -59,7 +59,7 @@ bool SFMLRender::resize_map(int new_size) { if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) { $sprites.clear(); // need to reset the sprites for the new size $map_font_size = new_size; - $base_glyph = $font.getGlyph(L'█', $map_font_size, false); + $base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false); $line_spacing = $font.getLineSpacing($map_font_size); return true; } else { @@ -85,7 +85,7 @@ void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) { 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(L'█'); + 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)); diff --git a/render.hpp b/render.hpp index a4d41d5..e264fc9 100644 --- a/render.hpp +++ b/render.hpp @@ -10,6 +10,8 @@ using ftxui::Canvas, ftxui::Screen; +#define BG_TILE L'█' + constexpr int VIDEO_X = 1600; constexpr int VIDEO_Y = 900; constexpr int MIN_FONT_SIZE = 20; diff --git a/status.txt b/status.txt index 89bdc81..9cfba0f 100644 --- a/status.txt +++ b/status.txt @@ -4,18 +4,9 @@ NOTES: * src/ftxui/screen/color.cpp * Just search for ugrep x1B * https://man7.org/linux/man-pages/man4/console_codes.4.html -* amit note: - -Struct A { - int data; - template A(T t) : data(static_cast(t)) {} -}; -A a(my_enum); TODO: -* Actually render FTXUI ansi output instead of the gui.cpp hack. -* Rewrite collider to return a real struct not tuple. * Write a test that generates a ton of maps then confirms there's a path from one room to every other room? * Lua integration? diff --git a/systems.cpp b/systems.cpp index 6d3a258..9cf08e3 100644 --- a/systems.cpp +++ b/systems.cpp @@ -6,6 +6,7 @@ #include "collider.hpp" #include "events.hpp" #include "ftxui/screen/color.hpp" +#include "ftxui/screen/terminal.hpp" // for SetColorSupport, Color, TrueColor using std::string; using namespace fmt; @@ -126,7 +127,7 @@ void System::draw_entities(DinkyECS::World &world, Map &game_map, ftxui::Canvas && pos.location.y >= cam_orig.y && pos.location.y <= cam_orig.y + view_y) { Point loc = game_map.map_to_camera(pos.location, cam_orig); // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing - canvas.DrawText(loc.x*2, loc.y*4, tile.chr); + canvas.DrawText(loc.x*2, loc.y*4, tile.chr, Color::RGB(255, 50, 50)); } }); } @@ -144,7 +145,11 @@ void System::draw_map(DinkyECS::World &world, Map &game_map, ftxui::Canvas &canv for(size_t y = 0; y < end_y; ++y) { string tile = walls[start.y+y][start.x+x] == 1 ? WALL_TILE : FLOOR_TILE; // the 2 and 4 are from ftxui::Canvas since it does a kind of "subpixel" drawing - canvas.DrawText(x * 2, y * 4, tile, Color::HSV(100, 10, 10)); + if(tile == WALL_TILE) { + canvas.DrawText(x * 2, y * 4, tile, Color::RGB(70, 70, 70)); + } else { + canvas.DrawText(x * 2, y * 4, tile, Color::RGB(20, 20, 20)); + } } }