From b8a0d9bbd14fea094420dd051d56419961d53391 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 5 Oct 2024 17:24:07 -0400 Subject: [PATCH] Now able to render the map at a different size from the rest of the UI and also only shake the map. --- gui.cpp | 63 +++++++++++++++++++++++++++++++++--------------------- gui.hpp | 18 ++++++++++++---- status.txt | 2 ++ 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/gui.cpp b/gui.cpp index 1ea9c11..3ff318e 100644 --- a/gui.cpp +++ b/gui.cpp @@ -48,16 +48,25 @@ sf::Color GUI::color(Value val) { GUI::GUI() : $game_map(GAME_MAP_X, GAME_MAP_Y), $canvas(GAME_MAP_X * 2, GAME_MAP_Y * 4), $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), - $screen(SCREEN_X, SCREEN_Y) + $screen(SCREEN_X, SCREEN_Y), + $map_screen(GAME_MAP_X, GAME_MAP_Y) { int res = $hit_buf.loadFromFile("./assets/hit.wav"); dbc::check(res, "failed to load hit.wav"); $hit_sound.setBuffer($hit_buf); $font.loadFromFile("./assets/text.otf"); - $text.setFont($font); - $text.setCharacterSize(30); - $text.setFillColor(color(Value::LIGHT_DARK)); + + $ui_text.setFont($font); + $ui_text.setPosition(0,0); + $ui_text.setCharacterSize(UI_FONT_SIZE); + $ui_text.setFillColor(color(Value::LIGHT_LIGHT)); + + $map_text.setFont($font); + $map_text.setPosition(GAME_MAP_POS,0); + $map_text.setCharacterSize(MAP_FONT_SIZE); + $map_text.setFillColor(color(Value::LIGHT_DARK)); + $game_map.generate(); $player.location = $game_map.place_entity(0); $enemy.location = $game_map.place_entity(1); @@ -77,20 +86,20 @@ void GUI::create_renderer() { for(size_t x = 0; x < walls[0].size(); ++x) { for(size_t y = 0; y < walls.size(); ++y) { - string tile = walls[y][x] == 1 ? "#" : format("{}", paths[y][x]); - if(tile == "#") { + string tile = walls[y][x] == 1 ? WALL_TILE : format("{}", paths[y][x]); + if(tile == WALL_TILE) { $canvas.DrawText(x*2, y*4, tile); } else if($show_paths) { //int pnum = paths[y][x]; $canvas.DrawText(x*2, y*4, tile); } else { - $canvas.DrawText(x*2, y*4, "."); + $canvas.DrawText(x*2, y*4, FLOOR_TILE); } } } - $canvas.DrawText($enemy.location.x*2, $enemy.location.y*4, "!"); - $canvas.DrawText($player.location.x*2, $player.location.y*4, "@"); + $canvas.DrawText($enemy.location.x*2, $enemy.location.y*4, ENEMY_TILE); + $canvas.DrawText($player.location.x*2, $player.location.y*4, PLAYER_TILE); $canvas.DrawText($goal.x*2, $goal.y*4, "$"); return canvas($canvas); @@ -105,7 +114,7 @@ void GUI::create_renderer() { ) | xflex_grow ), separator(), - hbox($map_view->Render()), + hbox(), }); }); } @@ -158,39 +167,45 @@ void GUI::handle_events() { void GUI::burn() { for(int i = 0; i < 20; ++i) { - $text.setFillColor(color(i % VALUES.size())); + $map_text.setFillColor(color(i % VALUES.size())); int x = Random::rand_int(-10,10); int y = Random::rand_int(-10,10); - $text.setPosition({(float)x,(float)y}); - $window.draw($text); - $window.display(); + draw_screen(false, x, y); std::this_thread::sleep_for(2ms); } - $text.setFillColor(color(Value::LIGHT_DARK)); + $map_text.setFillColor(color(Value::LIGHT_DARK)); } -inline void draw_screen(sf::RenderWindow &window, sf::Text &text, float x=0, float y=0) { - text.setPosition({x,y}); - window.clear(); - window.draw(text); - window.display(); +void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { + if(clear) $window.clear(); + $window.draw($ui_text); + $map_text.setPosition(GAME_MAP_POS+map_off_x, map_off_y); + $window.draw($map_text); + $window.display(); } void GUI::shake() { for(int i = 0; i < 10; ++i) { int x = Random::rand_int(-10,10); int y = Random::rand_int(-10,10); - draw_screen($window, $text, (float)x, (float)y); + // add x/y back to draw screen + draw_screen(true, x, y); std::this_thread::sleep_for(1ms); } } void GUI::render_scene() { + Render($map_screen, $map_view->Render()); Render($screen, $document->Render()); + std::string $screenout = $screen.ToString(); - std::wstring utf8 = $converter.from_bytes($screenout); - $text.setString(utf8); + std::wstring main_screen_utf8 = $converter.from_bytes($screenout); + $ui_text.setString(main_screen_utf8); + + std::string $map_screenout = $map_screen.ToString(); + std::wstring map_screen_utf8 = $converter.from_bytes($map_screenout); + $map_text.setString(map_screen_utf8); if($shake_it) { shake(); @@ -202,7 +217,7 @@ void GUI::render_scene() { $burn_baby_burn = false; } - draw_screen($window, $text, 0, 0); + draw_screen(); } int GUI::main() { diff --git a/gui.hpp b/gui.hpp index c440b96..5ebb438 100644 --- a/gui.hpp +++ b/gui.hpp @@ -16,12 +16,19 @@ using std::string; using ftxui::Canvas, ftxui::Component, ftxui::Screen; -constexpr int GAME_MAP_X = 60; -constexpr int GAME_MAP_Y = 30; -constexpr int SCREEN_X = 106; +constexpr int GAME_MAP_X = 30; +constexpr int GAME_MAP_Y = 15; +constexpr int GAME_MAP_POS = 600; +constexpr int SCREEN_X = 40; constexpr int SCREEN_Y = 30; constexpr int VIDEO_X = 1600; constexpr int VIDEO_Y = 900; +constexpr int MAP_FONT_SIZE=60; +constexpr int UI_FONT_SIZE=30; +#define WALL_TILE "█" +#define FLOOR_TILE "·" +#define PLAYER_TILE "☺" +#define ENEMY_TILE "Ω" enum class Value { BLACK=0, DARK_DARK, DARK_MID, @@ -42,12 +49,14 @@ class GUI { Component $map_view; Canvas $canvas; sf::Font $font; - sf::Text $text; + sf::Text $ui_text; + sf::Text $map_text; bool $shake_it = false; bool $burn_baby_burn = false; std::wstring_convert> $converter; sf::RenderWindow $window; Screen $screen; + Screen $map_screen; public: GUI(); @@ -59,6 +68,7 @@ public: void create_renderer(); void render_scene(); void handle_events(); + void draw_screen(bool clear=true, float map_off_x=0.0f, float map_off_y=0.0f); void shake(); void burn(); diff --git a/status.txt b/status.txt index 7b2a39b..48d4293 100644 --- a/status.txt +++ b/status.txt @@ -4,4 +4,6 @@ TODO: * Dynamically determine the font vs. screensize to get an exact FTXUI screen size. * Write a test that generates a ton of maps then confirms there's a path from one room to every other room? * If the player is trapped in a room the enemy just travles through walls. +* Add FLECS. +* Render a different screen for the map to use a different font size. * Lua integration?