From 9083582420a8c6d5baf115a4b696f2b9f1e8ae1c Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 21 Oct 2024 20:31:59 -0400 Subject: [PATCH] Make it possible to zoom in/out, but I may make this a combat thing where it's zoomed out until you encounter an enemy. --- gui.cpp | 45 +++++++++++++++++++++++++++------------------ gui.hpp | 6 +++++- 2 files changed, 32 insertions(+), 19 deletions(-) diff --git a/gui.cpp b/gui.cpp index c89b38b..7fec19a 100644 --- a/gui.cpp +++ b/gui.cpp @@ -54,22 +54,11 @@ GUI::GUI() : $game_map(GAME_MAP_X, GAME_MAP_Y), $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), $screen(SCREEN_X, SCREEN_Y), - $map_screen(0,0) + $map_screen(0,0), + $map_font_size(BASE_MAP_FONT_SIZE) { $font.loadFromFile("./assets/text.otf"); - // calculate display size - sf::Glyph base_glyph = $font.getGlyph(L'█', MAP_FONT_SIZE, false); - auto bounds = base_glyph.bounds; - $view_port = { - size_t(std::ceil((VIDEO_X - GAME_MAP_POS) / bounds.width)), - size_t(std::ceil(VIDEO_Y / bounds.height)) - }; - - // set canvas to best size - $canvas = Canvas($view_port.x * 2, $view_port.y * 4); - - $map_screen = Screen($view_port.x, $view_port.y); - + resize_map(BASE_MAP_FONT_SIZE); int res = $hit_buf.loadFromFile("./assets/hit.wav"); dbc::check(res, "failed to load hit.wav"); $hit_sound.setBuffer($hit_buf); @@ -128,6 +117,10 @@ bool GUI::handle_events() { } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)) { player_motion.dy = 1; event_happened = true; + } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Equal)) { + resize_map($map_font_size + 10); + } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Hyphen)) { + resize_map($map_font_size - 10); } } } @@ -138,11 +131,12 @@ bool GUI::handle_events() { sf::Sprite &GUI::get_text_sprite(wchar_t tile) { if(!$sprites.contains(tile)) { - sf::Glyph glyph = $font.getGlyph(tile, MAP_FONT_SIZE, false); + $sprites.clear(); + sf::Glyph glyph = $font.getGlyph(tile, $map_font_size, false); // WARNING! we actually have to do this here because SFML caches // the glyphs on the font texture, so this gets loaded each time // we get a new glyph from the font. - $font_texture = $font.getTexture(MAP_FONT_SIZE); + $font_texture = $font.getTexture($map_font_size); sf::Sprite sprite($font_texture); sprite.setTextureRect(glyph.textureRect); $sprites[tile] = sprite; @@ -158,6 +152,21 @@ void GUI::run_systems() { System::combat($world, player); } +void GUI::resize_map(int new_size) { + if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) { + $map_font_size = new_size; + sf::Glyph base_glyph = $font.getGlyph(L'█', $map_font_size, false); + auto bounds = base_glyph.bounds; + $view_port = { + size_t(std::ceil((VIDEO_X - GAME_MAP_POS) / bounds.width)), + size_t(std::ceil(VIDEO_Y / bounds.height)) + }; + // set canvas to best size + $canvas = Canvas($view_port.x * 2, $view_port.y * 4); + $map_screen = Screen($view_port.x, $view_port.y); + } +} + void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { if(clear) $window.clear(); std::string screenout = $screen.ToString(); @@ -170,8 +179,8 @@ void GUI::draw_screen(bool clear, float map_off_x, float map_off_y) { float y = 0.0f; float x = GAME_MAP_POS; - const float line_spacing = $font.getLineSpacing(MAP_FONT_SIZE); - sf::Glyph base_glyph = $font.getGlyph(L'█', MAP_FONT_SIZE, false); + const float line_spacing = $font.getLineSpacing($map_font_size); + sf::Glyph base_glyph = $font.getGlyph(L'█', $map_font_size, false); auto bg_sprite = get_text_sprite(L'█'); bg_sprite.setColor(sf::Color(20,20,20)); auto add_sprite = get_text_sprite(L'!'); diff --git a/gui.hpp b/gui.hpp index 2d037f4..1e77333 100644 --- a/gui.hpp +++ b/gui.hpp @@ -17,6 +17,8 @@ using std::string; using ftxui::Canvas, ftxui::Component, ftxui::Screen; +constexpr int MIN_FONT_SIZE = 20; +constexpr int MAX_FONT_SIZE = 140; constexpr int GAME_MAP_X = 90; constexpr int GAME_MAP_Y = 90; constexpr int GAME_MAP_POS = 600; @@ -24,8 +26,8 @@ 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=90; constexpr int UI_FONT_SIZE=30; +constexpr int BASE_MAP_FONT_SIZE=90; enum class Value { BLACK=0, DARK_DARK, DARK_MID, @@ -51,6 +53,7 @@ class GUI { sf::Texture $font_texture; std::unordered_map $sprites; Point $view_port = {0,0}; + int $map_font_size; public: GUI(); @@ -66,6 +69,7 @@ public: void shake(); void configure_world(); void run_systems(); + void resize_map(int new_size); sf::Sprite &get_text_sprite(wchar_t tile); int main();