From fb1fd9d8bc3e8a729d4cce04551b5e2821403295 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Fri, 22 Nov 2024 23:12:18 -0500 Subject: [PATCH] A bit of some clean up, API unifying, and some performance tweaks. --- gui.cpp | 2 ++ main.cpp | 1 - panel.cpp | 1 + panel.hpp | 1 + render.cpp | 58 +++++++++++++++++++++++++---------------- render.hpp | 4 +-- scratchpad/img2ansi.cpp | 41 ++++++++++++++++++++--------- status.txt | 4 --- 8 files changed, 71 insertions(+), 41 deletions(-) diff --git a/gui.cpp b/gui.cpp index 448dbb0..ceb0df7 100644 --- a/gui.cpp +++ b/gui.cpp @@ -240,7 +240,9 @@ void GUI::shake() { void GUI::render_scene() { $renderer.clear(); + $status_ui.render(); $renderer.draw($status_ui); + $map_view.render(); $renderer.draw($map_view); $renderer.display(); diff --git a/main.cpp b/main.cpp index ce5e9f1..bd8f93b 100644 --- a/main.cpp +++ b/main.cpp @@ -15,7 +15,6 @@ #include #endif - using namespace ftxui; namespace fs = std::filesystem; diff --git a/panel.cpp b/panel.cpp index 08fd79e..fa822c6 100644 --- a/panel.cpp +++ b/panel.cpp @@ -13,6 +13,7 @@ void Panel::set_renderer(Component renderer) { } void Panel::add(Component child) { + $dirty = true; $component->Add(child); } diff --git a/panel.hpp b/panel.hpp index 68afed1..e2e427a 100644 --- a/panel.hpp +++ b/panel.hpp @@ -24,6 +24,7 @@ struct Panel { bool must_clear = true; bool grid = false; sf::Color default_bg = color::BLACK; + sf::Color default_fg = color::LIGHT_LIGHT; sf::Color border_color = color::MID; int border_px = UI_PANEL_BORDER_PX; diff --git a/render.cpp b/render.cpp index 7b363fc..d61074e 100644 --- a/render.cpp +++ b/render.cpp @@ -69,19 +69,21 @@ 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_grid(const std::wstring &text, float x, float y) { +void SFMLRender::render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y) { wchar_t last_tile = '#'; sf::FloatRect sp_bounds; float width_delta = 0; float height_delta = 0; sf::Sprite &sprite = get_text_sprite(last_tile); const float start_x = x; - sf::Color cur_fg = $default_fg; + // BUG: get default_fg from panel too + sf::Color cur_fg = default_fg; + sf::Color cur_bg = default_bg; // make a copy so we don't modify the cached one $ansi.parse(text, [&](sf::Color fg, sf::Color bg) { cur_fg = fg; - $bg_sprite.setColor(bg); + cur_bg = bg; }, [&](wchar_t tile) { @@ -94,7 +96,6 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) { } break; default: { - $bg_sprite.setPosition({x, y}); // only get a new sprite if the tile changed if(last_tile != tile) { @@ -106,7 +107,13 @@ void SFMLRender::render_grid(const std::wstring &text, float x, float y) { sprite.setPosition({x+width_delta, y+height_delta}); sprite.setColor(cur_fg); - $window.draw($bg_sprite); + // only draw background char if it's different from default + if(cur_bg != default_bg) { + $bg_sprite.setPosition({x, y}); + $bg_sprite.setColor(cur_bg); + $window.draw($bg_sprite); + } + $window.draw(sprite); // next cell x += $base_glyph.advance; @@ -135,14 +142,14 @@ inline sf::FloatRect draw_chunk(sf::RenderWindow& window, return bounds; } -void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, float start_x, float start_y) { +void SFMLRender::render_text(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float start_x, float start_y) { std::wstring out; float x = start_x; float y = start_y; sf::Color cur_bg = default_bg; // start with the default_fg until it's changed - $ui_text.setFillColor($default_fg); + $ui_text.setFillColor(default_fg); $ansi.parse(text, [&](sf::Color fg, sf::Color bg) { @@ -184,26 +191,33 @@ void SFMLRender::render_text(const std::wstring &text, sf::Color default_bg, flo } } +/* + * Does not render the panel, you have to do that so you can control + * when things render. + */ void SFMLRender::draw(Panel &panel, float x_offset, float y_offset) { - panel.render(); const std::wstring &panelout = panel.to_string(); - 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)); + // BUG: ui vs bg doesn't make sense. maybe grid vs. text? + auto bounds = panel.grid ? $bg_bounds : $ui_bounds; - backing.setFillColor(panel.default_bg); + sf::RectangleShape backing( + sf::Vector2f(bounds.width * panel.width + panel.border_px, + bounds.height * panel.height + panel.border_px)); - if(panel.has_border) { - backing.setOutlineColor(panel.border_color); - backing.setOutlineThickness(panel.border_px); - } + backing.setFillColor(panel.default_bg); - backing.setPosition(panel.x + x_offset, panel.y + y_offset); - $window.draw(backing); - render_text(panelout, panel.default_bg, panel.x, panel.y); + if(panel.has_border) { + backing.setOutlineColor(panel.border_color); + backing.setOutlineThickness(panel.border_px); + } + + backing.setPosition(panel.x + x_offset, panel.y + y_offset); + $window.draw(backing); + + if(panel.grid) { + render_grid(panelout, panel.default_fg, panel.default_bg, panel.x + x_offset, panel.y + y_offset); + } else { + render_text(panelout, panel.default_fg, panel.default_bg, panel.x + x_offset, panel.y + y_offset); } } diff --git a/render.hpp b/render.hpp index c22c410..0848e61 100644 --- a/render.hpp +++ b/render.hpp @@ -51,8 +51,8 @@ struct SFMLRender { sf::Sprite &get_text_sprite(wchar_t tile); bool resize_grid(int new_size, Panel &panel_out); - void render_grid(const std::wstring &text, float x, float y); - void render_text(const std::wstring &text, sf::Color bgcolor, float x, float y); + void render_grid(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y); + void render_text(const std::wstring &text, sf::Color default_fg, sf::Color default_bg, float x, float y); void draw(Panel &panel, float x_offset=0.0f, float y_offset=0.0f); diff --git a/scratchpad/img2ansi.cpp b/scratchpad/img2ansi.cpp index 8e9a00c..0c5ddad 100644 --- a/scratchpad/img2ansi.cpp +++ b/scratchpad/img2ansi.cpp @@ -81,10 +81,7 @@ int main(int argc, char *argv[]) { // divide the image into cells auto size = image.getSize(); - const Point cell = {4, 6}; - - // create a grid panel to hold the cells - Panel panel(0, 0, 0, 0, true); + const Point cell = {3, 6}; println("IMAGE SIZE {},{}", size.x, size.y); RGBColor avg{0,0,0}; @@ -108,8 +105,11 @@ int main(int argc, char *argv[]) { } // average it for the cell size - RGBColor color = {avg.r / int(cell.x * cell.y), - avg.g / int(cell.x * cell.y), avg.b / int(cell.x * cell.y)}; + RGBColor color = { + avg.r / int(cell.x * cell.y), + avg.g / int(cell.x * cell.y), + avg.b / int(cell.x * cell.y), + }; // add it colors[i][j] = color; @@ -120,10 +120,9 @@ int main(int argc, char *argv[]) { } Canvas drawing; - - + // create a grid panel to hold the cells + Panel panel(0, 0, 0, 0, true); SFMLRender renderer; - renderer.resize_grid(26, panel); drawing = Canvas(panel.width * 2, panel.height * 4); @@ -142,8 +141,28 @@ int main(int argc, char *argv[]) { sf::Event event; + int start_x = 0; + int start_y = 0; + int end_x = 600; + int end_y = 300; + int cur_x = start_x; + int cur_y = start_y; + + sf::Texture texture; + texture.loadFromFile(image_file); + sf::Sprite sprite(texture); + + panel.render(); + while(renderer.is_open()) { - renderer.draw(panel); + cur_x = cur_x < end_x ? cur_x + 1 : start_x; + cur_y = cur_y < end_y ? cur_y + 1 : start_y; + + // sprite.setPosition(cur_x, cur_y); + // renderer.$window.draw(sprite); + // renderer.display(); + + renderer.draw(panel, cur_x, cur_y); renderer.display(); while(renderer.poll_event(event)) { @@ -151,8 +170,6 @@ int main(int argc, char *argv[]) { renderer.close(); } } - - std::this_thread::sleep_for(100ms); } return 0; diff --git a/status.txt b/status.txt index fbd31ec..e44b30e 100644 --- a/status.txt +++ b/status.txt @@ -1,9 +1,5 @@ TODAY'S GOAL: -* Image -> Text converter. -* Renderer needs to not have fixed setting for its size from the map. -* Refactor out GAME_MAP_* to be a parameter. - TODO: * Keep panel unified by implementing border and backing on grids too.