From 7cb03594a33a6a701a15e45fe17e82cb3dc952ae Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 18 Nov 2024 12:51:57 -0500 Subject: [PATCH] Renderer now knows how to render panels as grid/text based on how the panel is configured. --- gui.cpp | 8 ++++---- panel.hpp | 6 ++++-- render.cpp | 40 +++++++++++++++++++--------------------- render.hpp | 4 ++-- status.txt | 1 - 5 files changed, 29 insertions(+), 30 deletions(-) diff --git a/gui.cpp b/gui.cpp index f9c1926..1dd5493 100644 --- a/gui.cpp +++ b/gui.cpp @@ -33,7 +33,7 @@ GUI::GUI(DinkyECS::World &world, Map& game_map) : $game_map(game_map), $log({{"Welcome to the game!"}}), $status_ui(SCREEN_X, SCREEN_Y, 0, 0), - $map_view(0, 0, GAME_MAP_POS, 0, false), + $map_view(0, 0, GAME_MAP_POS, 0, true), $view_port{0,0}, $world(world), $sounds("./assets"), @@ -231,7 +231,7 @@ void GUI::shake() { int x = Random::uniform(-20,20); int y = Random::uniform(-20,20); // add x/y back to draw screen - $renderer.draw_grid($map_view, x, y); + $renderer.draw($map_view, x, y); $renderer.display(); std::this_thread::sleep_for(1ms); } @@ -240,8 +240,8 @@ void GUI::shake() { void GUI::render_scene() { $renderer.clear(); - $renderer.draw_text($status_ui); - $renderer.draw_grid($map_view); + $renderer.draw($status_ui); + $renderer.draw($map_view); $renderer.display(); } diff --git a/panel.hpp b/panel.hpp index 4cf784f..61cad1a 100644 --- a/panel.hpp +++ b/panel.hpp @@ -21,6 +21,7 @@ struct Panel { int height; bool has_border = false; bool must_clear = true; + bool grid = false; sf::Color default_bg = sf::Color(0,0,0); sf::Color border_color = sf::Color::Red; int border_px = UI_PANEL_BORDER_PX; @@ -31,14 +32,15 @@ struct Panel { std::wstring_convert> $converter; std::wstring $screenout; - Panel(int width, int height, int x, int y, bool must_clear=true) : + Panel(int width, int height, int x, int y, bool is_grid=false) : x(x), y(y), width(width), height(height), - must_clear(must_clear), + grid(is_grid), $screen(Screen(width, height)) { + must_clear = !is_grid; }; void resize(int width, int height); diff --git a/render.cpp b/render.cpp index 0d19413..b67d9f8 100644 --- a/render.cpp +++ b/render.cpp @@ -180,7 +180,7 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star $ui_text.setFillColor($default_fg); $ansi.parse(text, - [&](sf::Color fg, sf::Color bg){ + [&](sf::Color fg, sf::Color bg) { if(out.size() > 0 ) { auto bounds = draw_chunk($window, $ui_bounds, $ui_text, bgcolor, x, y, out); x += bounds.width; @@ -216,28 +216,26 @@ void SFMLRender::render_text(const std::wstring &text, float start_x, float star } } -void SFMLRender::draw_text(Panel &panel) { - sf::RectangleShape backing( - sf::Vector2f($ui_bounds.width * panel.width + panel.border_px, - $ui_bounds.height * panel.height + panel.border_px)); - - backing.setFillColor(panel.default_bg); +void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) { + panel.render(); + const std::wstring &panelout = panel.to_string(); - if(panel.has_border) { - backing.setOutlineColor(panel.border_color); - backing.setOutlineThickness(panel.border_px); - } + if(panel.grid) { + render_grid(panelout, panel.x + x_offset, panel.y + y_offset); + } else { + sf::RectangleShape backing( + sf::Vector2f($ui_bounds.width * panel.width + panel.border_px, + $ui_bounds.height * panel.height + panel.border_px)); - backing.setPosition(panel.x, panel.y); - $window.draw(backing); + backing.setFillColor(panel.default_bg); - panel.render(); - const std::wstring &panelout = panel.to_string(); - render_text(panelout, panel.x, panel.y); -} + if(panel.has_border) { + backing.setOutlineColor(panel.border_color); + backing.setOutlineThickness(panel.border_px); + } -void SFMLRender::draw_grid(Panel &panel, float x, float y) { - panel.render(); - const std::wstring &panelout = panel.to_string(); - render_grid(panelout, panel.x + x, panel.y + y); + backing.setPosition(panel.x + x_offset, panel.y + y_offset); + $window.draw(backing); + render_text(panelout, panel.x, panel.y); + } } diff --git a/render.hpp b/render.hpp index 9c2dfd1..88cef4f 100644 --- a/render.hpp +++ b/render.hpp @@ -58,8 +58,8 @@ struct SFMLRender { bool resize_map(int new_size, Point &view_port); void render_grid(const std::wstring &text, float x, float y); void render_text(const std::wstring &text, float x, float y); - void draw_text(Panel &panel); - void draw_grid(Panel &panel, float map_off_x=0.0f, float map_off_y=0.0f); + + void draw(Panel &panel, float x_offset=0.0f, float y_offset=0.0f); bool poll_event(sf::Event &event) { return $window.pollEvent(event); diff --git a/status.txt b/status.txt index 18a377b..1dfdcf3 100644 --- a/status.txt +++ b/status.txt @@ -2,7 +2,6 @@ TODAY'S GOAL: * Clean up renderer. * panels and everything except renderer should use character coodinates -* panels should know if they're text vs. grid rendered * Image -> Text converter.