From ae43dad499cb4916470d2982d41a0cf2be2ca4e2 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 4 Dec 2024 08:19:04 -0500 Subject: [PATCH] Minor fixes to make initializing the terminal more consistent and to remove a magic number for wall limits. --- ansi_parser.cpp | 16 ++++++---------- ansi_parser.rl | 4 ---- gui.cpp | 2 +- main.cpp | 7 ------- map.cpp | 6 +++--- map.hpp | 9 +++++---- render.cpp | 15 +++++++++++++++ render.hpp | 1 + status.txt | 12 ++++-------- tests/ansi_parser.cpp | 9 +++++++-- tests/panel.cpp | 6 +++--- tests/render.cpp | 2 ++ 12 files changed, 47 insertions(+), 42 deletions(-) diff --git a/ansi_parser.cpp b/ansi_parser.cpp index c23479e..9e70b7a 100644 --- a/ansi_parser.cpp +++ b/ansi_parser.cpp @@ -109,10 +109,6 @@ ANSIParser::ANSIParser(sf::Color default_fg, sf::Color default_bg) : $default_fg(default_fg), $default_bg(default_bg) { - // the parser only handles full color so force it - if(ftxui::Terminal::ColorSupport() != ftxui::Terminal::Color::TrueColor) { - ftxui::Terminal::SetColorSupport(ftxui::Terminal::Color::TrueColor); - } } bool ANSIParser::parse(std::wstring_view codes, ColorCB color_cb, WriteCB write_cb) { @@ -127,14 +123,14 @@ bool ANSIParser::parse(std::wstring_view codes, ColorCB color_cb, WriteCB write_ sf::Color* target = &color; -#line 122 "ansi_parser.cpp" +#line 118 "ansi_parser.cpp" { cs = ansi_parser_start; } -#line 149 "ansi_parser.rl" +#line 145 "ansi_parser.rl" -#line 125 "ansi_parser.cpp" +#line 121 "ansi_parser.cpp" { int _klen; unsigned int _trans; @@ -325,7 +321,7 @@ _match: #line 85 "ansi_parser.rl" {} break; -#line 298 "ansi_parser.cpp" +#line 294 "ansi_parser.cpp" } } @@ -345,7 +341,7 @@ _again: #line 85 "ansi_parser.rl" {} break; -#line 316 "ansi_parser.cpp" +#line 312 "ansi_parser.cpp" } } } @@ -353,7 +349,7 @@ _again: _out: {} } -#line 150 "ansi_parser.rl" +#line 146 "ansi_parser.rl" bool good = pe - p == 0; diff --git a/ansi_parser.rl b/ansi_parser.rl index e73b679..f31f64f 100644 --- a/ansi_parser.rl +++ b/ansi_parser.rl @@ -128,10 +128,6 @@ ANSIParser::ANSIParser(sf::Color default_fg, sf::Color default_bg) : $default_fg(default_fg), $default_bg(default_bg) { - // the parser only handles full color so force it - if(ftxui::Terminal::ColorSupport() != ftxui::Terminal::Color::TrueColor) { - ftxui::Terminal::SetColorSupport(ftxui::Terminal::Color::TrueColor); - } } bool ANSIParser::parse(std::wstring_view codes, ColorCB color_cb, WriteCB write_cb) { diff --git a/gui.cpp b/gui.cpp index e8adf0f..cc2dfc6 100644 --- a/gui.cpp +++ b/gui.cpp @@ -63,7 +63,7 @@ void GUI::save_world() { } void GUI::create_renderer() { - Terminal::SetColorSupport(Terminal::Color::TrueColor); + $renderer.init_terminal(); auto player = $world.get_the(); $map_view.set_renderer(Renderer([&] { diff --git a/main.cpp b/main.cpp index 9ade6f5..bb81b9e 100644 --- a/main.cpp +++ b/main.cpp @@ -13,10 +13,6 @@ #include #include -#if defined(_WIN64) || defined(_WIN32) - #include -#endif - using namespace ftxui; using namespace components; using lighting::LightSource; @@ -68,9 +64,6 @@ const int GAME_MAP_X = 40; const int GAME_MAP_Y = 40; int main(int argc, char *argv[]) { -#if defined(_WIN64) || defined(_WIN32) - _setmode(_fileno(stdout), _O_U16TEXT); -#endif DinkyECS::World world; Map game_map(GAME_MAP_X, GAME_MAP_Y); save::load_configs(world); diff --git a/map.cpp b/map.cpp index 00c8e4d..869a635 100644 --- a/map.cpp +++ b/map.cpp @@ -17,7 +17,7 @@ void dump_map(const std::string &msg, Matrix &map, int show_x, int show_y) { if(int(x) == show_x && int(y) == show_y) { print("{:x}<", col); - } else if(col == 1000) { + } else if(col == WALL_PATH_LIMIT) { print("# "); } else if(col > 15) { print("* "); @@ -30,11 +30,11 @@ void dump_map(const std::string &msg, Matrix &map, int show_x, int show_y) { } Map::Map(size_t width, size_t height) : - $limit(1000), + $limit(WALL_PATH_LIMIT), $width(width), $height(height), $walls(height, MatrixRow(width, INV_WALL)), - $paths(height, width, 1000) + $paths(height, width, WALL_PATH_LIMIT) {} Map::Map(Matrix &walls, Pathing &paths, int limit) : diff --git a/map.hpp b/map.hpp index 865987c..7370cb6 100644 --- a/map.hpp +++ b/map.hpp @@ -11,10 +11,11 @@ #include "pathing.hpp" #include "matrix.hpp" -#define INV_WALL 0 -#define INV_SPACE 1 -#define WALL_VALUE 1 -#define SPACE_VALUE 0 +const int INV_WALL = 0; +const int INV_SPACE = 1; +const int WALL_VALUE = 1; +const int SPACE_VALUE = 0; +const int WALL_PATH_LIMIT = 1000; using lighting::LightSource; diff --git a/render.cpp b/render.cpp index d6d29fc..5bc518c 100644 --- a/render.cpp +++ b/render.cpp @@ -6,6 +6,12 @@ #include "map.hpp" #include #include "color.hpp" +#if defined(_WIN64) || defined(_WIN32) + #include + #include + #include +#endif + using namespace fmt; @@ -220,3 +226,12 @@ void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) { render_text(panelout, panel.default_fg, panel.default_bg, panel.x + x_offset, panel.y + y_offset); } } + +void SFMLRender::init_terminal() { +#if defined(_WIN64) || defined(_WIN32) + _setmode(_fileno(stdout), _O_U16TEXT); +#endif + + // the parser only handles full color so force it + ftxui::Terminal::SetColorSupport(ftxui::Terminal::Color::TrueColor); +} diff --git a/render.hpp b/render.hpp index 1ab4bc2..bc3f107 100644 --- a/render.hpp +++ b/render.hpp @@ -67,4 +67,5 @@ struct SFMLRender { int font_size() { return $map_font_size; } void clear() { $window.clear(); } void display() { $window.display(); } + static void init_terminal(); }; diff --git a/status.txt b/status.txt index 7b051a7..0f74c0b 100644 --- a/status.txt +++ b/status.txt @@ -1,16 +1,14 @@ TODAY'S GOAL: -* Neighbors needs a rewrite -* Neighbors algo isn't using greater parameter -* Clean up and document as much code as possible. -* Add a method to render.cpp that sets terminal true color. -* limit as 1000 should be a constant * Big Code Review +* Clean up and document as much code as possible. +* color namespace is too ambiguous + +* Lua integration TODO: * I can do headless windows in renderer for testing. - renderer.$window.setVisible(false); * Think up an enemy system. -* Revisit map generation. * Write a method for renderer that can translate coordinates. * Can std::any be defaulted to a noop in the events? * Save file isn't saving gold. @@ -19,9 +17,7 @@ TODO: * Devise a more complete map/world generator that can use the loot and enemies better. * Maybe an LOS system, but the hearing version works pretty well so far. * Probably a system for mapping collision types to sound effects, rather than having the GUI do it. - * Write a test that generates a ton of maps then confirms there's a path from one room to every other room? -* Lua integration * check out SoLoud. diff --git a/tests/ansi_parser.cpp b/tests/ansi_parser.cpp index adaa1a4..3796d2d 100644 --- a/tests/ansi_parser.cpp +++ b/tests/ansi_parser.cpp @@ -10,12 +10,16 @@ #include "ftxui/screen/color.hpp" // for Color, Color::Black, Color::Blue, Color::BlueLight, Color::Cyan, Color::CyanLight, Color::Default, Color::GrayDark, Color::GrayLight, Color::Green, Color::GreenLight, Color::Magenta, Color::MagentaLight, Color::Red, Color::RedLight, Color::White, Color::Yellow, Color::YellowLight, Color::Palette256, ftxui #include // for ColorInfo #include // for Full, Screen +#include // for Full, Screen #include // for ColorSupport, Color, Palette16, Palette256, TrueColor +#include "render.hpp" using namespace fmt; using namespace ftxui; std::string generate_colors() { + SFMLRender::init_terminal(); + REQUIRE(ftxui::Terminal::ColorSupport() == ftxui::Terminal::Color::TrueColor); const int max_value = 255; @@ -30,8 +34,8 @@ std::string generate_colors() { for (int hue = 0; hue < max_value; hue += hue_increment) { line.push_back( text("#") // - | color(Color::HSV(hue, saturation, value)) // - | bgcolor(Color::HSV(hue, saturation, value + value_increment))); + | ftxui::color(Color::HSV(hue, saturation, value)) // + | ftxui::bgcolor(Color::HSV(hue, saturation, value + value_increment))); count++; } array.push_back(hbox(std::move(line))); @@ -51,6 +55,7 @@ std::string generate_colors() { } TEST_CASE("test out ragel parser", "[gui]") { + SFMLRender::init_terminal(); sf::Color default_fg(0,0,0); sf::Color default_bg(100,100,100); diff --git a/tests/panel.cpp b/tests/panel.cpp index 6e28bfe..5f6ec4b 100644 --- a/tests/panel.cpp +++ b/tests/panel.cpp @@ -4,6 +4,7 @@ #include // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element #include "panel.hpp" #include "ansi_parser.hpp" +#include "render.hpp" using namespace ftxui; using namespace fmt; @@ -27,8 +28,7 @@ void test_ansi_parsing(Panel &panel) { } TEST_CASE("can render a simple text panel", "[panel]") { - ftxui::Terminal::SetColorSupport(ftxui::Terminal::Color::TrueColor); - + SFMLRender::init_terminal(); Panel text_panel(0, 0, 20, 5); bool show_modal = false; @@ -59,7 +59,7 @@ TEST_CASE("can render a simple text panel", "[panel]") { } TEST_CASE("can render a simple grid panel", "[panel]") { - Terminal::SetColorSupport(Terminal::Color::TrueColor); + SFMLRender::init_terminal(); Panel grid_panel(20, 20, 20, 5, true); diff --git a/tests/render.cpp b/tests/render.cpp index cc52dfb..e1aec1f 100644 --- a/tests/render.cpp +++ b/tests/render.cpp @@ -25,6 +25,7 @@ void run_renderer(SFMLRender &renderer, Panel &panel) { TEST_CASE("can render a text panel", "[render]") { SFMLRender renderer; + renderer.init_terminal(); Panel panel(0, 0, 20, 5); @@ -42,6 +43,7 @@ TEST_CASE("can render a text panel", "[render]") { TEST_CASE("can render a text", "[render]") { SFMLRender renderer; + renderer.init_terminal(); DinkyECS::World world; save::load_configs(world);