From 7b5c84b5f7af591a3c1c9738d116db9c4deb81a2 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sun, 10 Nov 2024 12:53:29 -0500 Subject: [PATCH] Cleaned up the gui display some more for future GUI elements. --- gui.cpp | 31 ++++++++++++++++++++++++------- gui.hpp | 5 +++-- render.cpp | 41 +++++++++++++++++++++++------------------ render.hpp | 13 ++++++------- status.txt | 2 ++ 5 files changed, 58 insertions(+), 34 deletions(-) diff --git a/gui.cpp b/gui.cpp index 23e240a..6759371 100644 --- a/gui.cpp +++ b/gui.cpp @@ -33,11 +33,11 @@ GUI::GUI(DinkyECS::World &world, Map& game_map) : $game_map(game_map), $log({{"Welcome to the game!"}}), $view_port{0,0}, - $screen(SCREEN_X, SCREEN_Y), + $status_screen(SCREEN_X, SCREEN_Y), $map_screen(0,0), $world(world), $sounds("./assets"), - $renderer($canvas, $map_screen, $screen) + $renderer() { // this needs a config file soon // $sounds.load("ambient", "ambient_sound.mp3"); @@ -70,7 +70,12 @@ void GUI::create_renderer() { return canvas($canvas); }); - $document = Renderer([&, player]{ + $prompt = Renderer([&] { + return hbox({hflow(vbox(text("HELLO!")))}) | border; + }); + + + $status_ui = Renderer([&, player]{ const auto& player_combat = $world.get(player.entity); const auto& inventory = $world.get(player.entity); $status_text = player_combat.hp > 0 ? "NOT DEAD" : "DEAD!!!!!!"; @@ -192,17 +197,29 @@ void GUI::shake() { int x = Random::uniform(-10,10); int y = Random::uniform(-10,10); // add x/y back to draw screen - $renderer.draw_screen(true, x, y); + $renderer.draw_screen($map_screen, x, y); std::this_thread::sleep_for(1ms); } } void GUI::render_scene() { - $screen.Clear(); + $renderer.clear(); + + $status_screen.Clear(); + Render($status_screen, $status_ui->Render()); + $renderer.draw_text_ui($status_screen, 0, 0); + $map_screen.Clear(); Render($map_screen, $map_view->Render()); - Render($screen, $document->Render()); - $renderer.draw_screen(); + $renderer.draw_screen($map_screen); + + /* + Screen prompt_screen(30,10); + Render(prompt_screen, $prompt->Render()); + $renderer.draw_text_ui(prompt_screen, 700, 300); + */ + + $renderer.display(); } int GUI::main() { diff --git a/gui.hpp b/gui.hpp index b6880be..c11f5a0 100644 --- a/gui.hpp +++ b/gui.hpp @@ -35,12 +35,13 @@ struct ActionLog { class GUI { string $status_text = "NOT DEAD"; Canvas $canvas; - Component $document; + Component $status_ui; + Component $prompt; Component $map_view; Map& $game_map; ActionLog $log; Point $view_port; - Screen $screen; + Screen $status_screen; Screen $map_screen; DinkyECS::World& $world; SoundManager $sounds; diff --git a/render.cpp b/render.cpp index b3fd04f..65227c1 100644 --- a/render.cpp +++ b/render.cpp @@ -28,13 +28,10 @@ sf::Color SFMLRender::color(Value val) { return VALUES[size_t(val)]; } -SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) : +SFMLRender::SFMLRender() : $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), $map_font_size(0), $line_spacing(0), - $canvas(canvas), - $map_screen(map_screen), - $screen(screen), $default_fg(color(Value::LIGHT_MID)), $default_bg(color(Value::BLACK)), $ansi($default_fg, $default_bg) @@ -44,7 +41,9 @@ SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) : $ui_text.setFont($font); $ui_text.setPosition(0,0); $ui_text.setCharacterSize(UI_FONT_SIZE); - $ui_text.setFillColor(color(Value::LIGHT_LIGHT)); + $ui_text.setFillColor(color(Value::LIGHT_MID)); + sf::Glyph glyph = $font.getGlyph(L'H', UI_FONT_SIZE, false); + $ui_bounds = glyph.bounds; } sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) { @@ -96,13 +95,6 @@ bool SFMLRender::resize_map(int new_size, Point &view_port) { } } -void SFMLRender::draw_main_ui() { - std::string screenout = $screen.ToString(); - std::wstring main_screen_utf8 = $converter.from_bytes(screenout); - $ui_text.setString(main_screen_utf8); - $window.draw($ui_text); -} - inline void configure_tile(const sf::Sprite &sprite, sf::FloatRect &sp_bounds, sf::FloatRect bg_bounds, float &width_delta, float &height_delta) { // should look into caching all this instead of calcing it each time sp_bounds = sprite.getLocalBounds(); @@ -149,11 +141,24 @@ void SFMLRender::render_text(std::string &text, float x, float y) { }); } -void SFMLRender::draw_screen(bool clear, float map_off_x, float map_off_y) { - if(clear) $window.clear(); - draw_main_ui(); +void SFMLRender::draw_text_ui(Screen &screen, float x, float y) { + sf::RectangleShape backing( + sf::Vector2f($ui_bounds.width * screen.dimx() * 1.4, + $ui_bounds.height * screen.dimy() * 1.6)); + + backing.setFillColor(sf::Color(0, 0, 0)); + backing.setPosition(x, y); + $window.draw(backing); + + std::string screenout = screen.ToString(); + std::wstring main_screen_utf8 = $converter.from_bytes(screenout); + + $ui_text.setPosition(x, y); + $ui_text.setString(main_screen_utf8); + $window.draw($ui_text); +} - std::string map_screenout = $map_screen.ToString(); - render_text(map_screenout, GAME_MAP_POS+map_off_x, map_off_y); - $window.display(); +void SFMLRender::draw_screen(Screen &screen, float x, float y) { + std::string screenout = screen.ToString(); + render_text(screenout, GAME_MAP_POS+x, y); } diff --git a/render.hpp b/render.hpp index afba9fb..cb4ea53 100644 --- a/render.hpp +++ b/render.hpp @@ -38,16 +38,14 @@ struct SFMLRender { sf::Glyph $base_glyph; sf::Sprite $bg_sprite; sf::FloatRect $bg_bounds; - Canvas& $canvas; - Screen& $map_screen; - Screen& $screen; sf::Text $ui_text; std::wstring_convert> $converter; sf::Color $default_fg; sf::Color $default_bg; ANSIParser $ansi; + sf::FloatRect $ui_bounds; - SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen); + SFMLRender(); // disable copy SFMLRender(SFMLRender &other) = delete; @@ -57,8 +55,8 @@ struct SFMLRender { sf::Sprite &get_text_sprite(wchar_t tile); bool resize_map(int new_size, Point &view_port); void render_text(std::string &text, float x, float y); - void draw_main_ui(); - void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); + void draw_text_ui(Screen &screen, float x, float y); + void draw_screen(Screen &screen, float map_off_x=0.0f, float map_off_y=0.0f); bool poll_event(sf::Event &event) { return $window.pollEvent(event); @@ -69,5 +67,6 @@ struct SFMLRender { bool is_open() { return $window.isOpen(); } int font_size() { return $map_font_size; } - + void clear() { $window.clear(); } + void display() { $window.display(); } }; diff --git a/status.txt b/status.txt index 7a871b6..0e0fea7 100644 --- a/status.txt +++ b/status.txt @@ -16,3 +16,5 @@ TODO: * Lua integration? * check out SoLoud. + +* BUG: When enemies die they can be overlapping another enemy.