From 7d3605f58bbb70eedabf2cd74f325a14e6b139a3 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 12 Nov 2024 08:06:09 -0500 Subject: [PATCH] A bit of cleanup and testing of the panel, then some optimization to avoid re-rendering and multiple wchar converts. --- gui.cpp | 12 +++++++++++- panel.cpp | 19 +++++++++++++------ panel.hpp | 8 +++++--- render.cpp | 14 ++++++++++---- render.hpp | 4 ++-- 5 files changed, 41 insertions(+), 16 deletions(-) diff --git a/gui.cpp b/gui.cpp index db39afb..ebcbd1d 100644 --- a/gui.cpp +++ b/gui.cpp @@ -201,9 +201,19 @@ void GUI::shake() { void GUI::render_scene() { $renderer.clear(); - $renderer.draw_text_ui($status_ui); + $renderer.draw_text_ui($status_ui, true); $renderer.draw_screen($map_view); + /* + Panel prompt(30, 10, GAME_MAP_POS + 30, 200); + prompt.set_renderer([&] { + return hbox({ + hflow(vbox(text("GOLD!"))) + }); + }); + $renderer.draw_text_ui(prompt, true); + */ + $renderer.display(); } diff --git a/panel.cpp b/panel.cpp index 8069dbc..01e53e7 100644 --- a/panel.cpp +++ b/panel.cpp @@ -1,24 +1,31 @@ #include "panel.hpp" void Panel::resize(int width, int height) { + $dirty = true; $screen = Screen(width, height); } void Panel::set_renderer(std::function< Element()> render) { + $dirty = true; $component = Renderer(render); } -Screen &Panel::render() { +void Panel::render() { + $dirty = true; if($must_clear) $screen.Clear(); Render($screen, $component->Render()); - return $screen; } -std::wstring Panel::to_string() { - std::string screenout = $screen.ToString(); - return $converter.from_bytes(screenout); +const std::wstring& Panel::to_string() { + if($dirty) { + std::string as_text = $screen.ToString(); + $screenout = $converter.from_bytes(as_text); + $dirty = false; + } + + return $screenout; } -Screen &Panel::screen() { +const Screen &Panel::screen() { return $screen; } diff --git a/panel.hpp b/panel.hpp index 5e64ca2..3be0c85 100644 --- a/panel.hpp +++ b/panel.hpp @@ -17,6 +17,8 @@ struct Panel { int y; int width; int height; + std::wstring $screenout; + bool $dirty = true; Component $component; Screen $screen; bool $must_clear = true; @@ -33,7 +35,7 @@ struct Panel { void resize(int width, int height); void set_renderer(std::function< Element()> render); - Screen &render(); - std::wstring to_string(); - Screen &screen(); + void render(); + const std::wstring &to_string(); + const Screen &screen(); }; diff --git a/render.cpp b/render.cpp index a9c2796..62226b2 100644 --- a/render.cpp +++ b/render.cpp @@ -104,7 +104,7 @@ inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, height_delta = bg_bounds.height > sp_bounds.width ? (bg_bounds.height - sp_bounds.height) / 2 : 0; } -void SFMLRender::render_text(std::wstring &text, float x, float y) { +void SFMLRender::render_text(const std::wstring &text, float x, float y) { wchar_t last_tile = '#'; sf::FloatRect sp_bounds; float width_delta = 0; @@ -142,17 +142,23 @@ void SFMLRender::render_text(std::wstring &text, float x, float y) { }); } -void SFMLRender::draw_text_ui(Panel &panel) { +void SFMLRender::draw_text_ui(Panel &panel, bool with_border) { sf::RectangleShape backing( sf::Vector2f($ui_bounds.width * panel.width, $ui_bounds.height * panel.height)); backing.setFillColor(sf::Color(0, 0, 0)); + + if(with_border) { + backing.setOutlineColor(color(Value::MID)); + backing.setOutlineThickness(5); + } + backing.setPosition(panel.x, panel.y); $window.draw(backing); panel.render(); - std::wstring panelout = panel.to_string(); + const std::wstring &panelout = panel.to_string(); $ui_text.setPosition(panel.x, panel.y); $ui_text.setString(panelout); $window.draw($ui_text); @@ -160,6 +166,6 @@ void SFMLRender::draw_text_ui(Panel &panel) { void SFMLRender::draw_screen(Panel &panel, float x, float y) { panel.render(); - std::wstring panelout = panel.to_string(); + const std::wstring &panelout = panel.to_string(); render_text(panelout, panel.x + x, panel.y + y); } diff --git a/render.hpp b/render.hpp index 8719863..453c182 100644 --- a/render.hpp +++ b/render.hpp @@ -55,8 +55,8 @@ struct SFMLRender { sf::Color color(Value val); sf::Sprite &get_text_sprite(wchar_t tile); bool resize_map(int new_size, Point &view_port); - void render_text(std::wstring &text, float x, float y); - void draw_text_ui(Panel &panel); + void render_text(const std::wstring &text, float x, float y); + void draw_text_ui(Panel &panel, bool with_border=false); void draw_screen(Panel &panel, float map_off_x=0.0f, float map_off_y=0.0f); bool poll_event(sf::Event &event) {