From 824a384ffd375428d29605420f5b35a902ad9300 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 9 Nov 2024 10:56:22 -0500 Subject: [PATCH] Font sizes and map view now work with arbitrary map sizes. --- gui.cpp | 13 ++----------- render.cpp | 31 ++++++++++++++++++++++++++----- render.hpp | 2 +- status.txt | 4 +--- 4 files changed, 30 insertions(+), 20 deletions(-) diff --git a/gui.cpp b/gui.cpp index 0e80455..23e240a 100644 --- a/gui.cpp +++ b/gui.cpp @@ -45,20 +45,11 @@ GUI::GUI(DinkyECS::World &world, Map& game_map) : $sounds.load("combat_player_hit", "combat_player_hit.mp3"); $sounds.load("combat_enemy_hit", "combat_enemy_hit.mp3"); $sounds.load("combat_miss", "combat_miss.mp3"); - resize_map(BASE_MAP_FONT_SIZE); + resize_map(MAX_FONT_SIZE); } void GUI::resize_map(int new_size) { - if($renderer.resize_map(new_size)) { - auto bounds = $renderer.$base_glyph.bounds; - int view_x = std::ceil((VIDEO_X - GAME_MAP_POS) / bounds.width); - int view_y = std::ceil(VIDEO_Y / bounds.height); - - // don't allow resizing beyond/below game map size - if(view_x > GAME_MAP_X || view_y > GAME_MAP_Y) return; - - $view_port = {size_t(view_x), size_t(view_y)}; - + if($renderer.resize_map(new_size, $view_port)) { // set canvas to best size $canvas = Canvas($view_port.x * 2, $view_port.y * 4); $map_screen = Screen($view_port.x, $view_port.y); diff --git a/render.cpp b/render.cpp index 884494b..018ef71 100644 --- a/render.cpp +++ b/render.cpp @@ -3,6 +3,9 @@ #include #include #include +#include "map.hpp" + +using namespace fmt; std::array VALUES{ sf::Color{1, 4, 2}, // black @@ -27,7 +30,7 @@ sf::Color SFMLRender::color(Value val) { SFMLRender::SFMLRender(Canvas &canvas, Screen &map_screen, Screen &screen) : $window(sf::VideoMode(VIDEO_X,VIDEO_Y), "Roguish"), - $map_font_size(BASE_MAP_FONT_SIZE), + $map_font_size(0), $line_spacing(0), $canvas(canvas), $map_screen(map_screen), @@ -59,12 +62,30 @@ sf::Sprite &SFMLRender::get_text_sprite(wchar_t tile) { return $sprites[tile]; } +inline bool base_glyph_check(sf::Font &font, sf::Glyph &base_glyph, Point &view_port, int &font_size, int new_size) { + auto glyph = font.getGlyph(BG_TILE, new_size, false); + + int view_x = std::ceil((VIDEO_X - GAME_MAP_POS) / glyph.bounds.width); + int view_y = std::ceil(VIDEO_Y / glyph.bounds.height); + + // don't allow resizing beyond/below game map size + if(view_x <= GAME_MAP_X && view_y <= GAME_MAP_Y) { + // looks good, set 'em all + base_glyph = glyph; + view_port = {size_t(view_x), size_t(view_y)}; + font_size = new_size; + return true; + } else { + return false; + } +} + + +bool SFMLRender::resize_map(int new_size, Point &view_port) { + if($map_font_size == new_size || new_size < MIN_FONT_SIZE || new_size > MAX_FONT_SIZE) return false; -bool SFMLRender::resize_map(int new_size) { - if(MIN_FONT_SIZE < new_size && new_size < MAX_FONT_SIZE) { + if(base_glyph_check($font, $base_glyph, view_port, $map_font_size, new_size)) { $sprites.clear(); // need to reset the sprites for the new size - $map_font_size = new_size; - $base_glyph = $font.getGlyph(BG_TILE, $map_font_size, false); $line_spacing = $font.getLineSpacing($map_font_size); $bg_sprite = get_text_sprite(BG_TILE); $bg_bounds = $bg_sprite.getLocalBounds(); diff --git a/render.hpp b/render.hpp index 4ed3b55..afba9fb 100644 --- a/render.hpp +++ b/render.hpp @@ -55,7 +55,7 @@ struct SFMLRender { sf::Color color(int val); sf::Color color(Value val); sf::Sprite &get_text_sprite(wchar_t tile); - bool resize_map(int new_size); + 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); diff --git a/status.txt b/status.txt index 6ff4992..7a871b6 100644 --- a/status.txt +++ b/status.txt @@ -15,6 +15,4 @@ TODO: * Write a test that generates a ton of maps then confirms there's a path from one room to every other room? * Lua integration? -* BUG: If map is < 90 zome out crashes. - -* Bring back sounds, check out SoLoud. +* check out SoLoud.