From 2dccc6b17b5efc96e200107d4aa865607a26c191 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Sat, 9 Nov 2024 10:14:53 -0500 Subject: [PATCH] Initial fix of the crash with different map sizes but that's not the ultimate fix. --- gui.cpp | 12 ++++++++---- map.hpp | 10 ++++++++-- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/gui.cpp b/gui.cpp index 92064da..0e80455 100644 --- a/gui.cpp +++ b/gui.cpp @@ -5,6 +5,7 @@ #include // for string, operator<< #include // for sleep_for #include +#include #include // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element #include // for Render @@ -50,10 +51,13 @@ GUI::GUI(DinkyECS::World &world, Map& game_map) : void GUI::resize_map(int new_size) { if($renderer.resize_map(new_size)) { auto bounds = $renderer.$base_glyph.bounds; - $view_port = { - size_t(std::ceil((VIDEO_X - GAME_MAP_POS) / bounds.width)), - size_t(std::ceil(VIDEO_Y / bounds.height)) - }; + 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)}; // set canvas to best size $canvas = Canvas($view_port.x * 2, $view_port.y * 4); diff --git a/map.hpp b/map.hpp index d172f83..0716555 100644 --- a/map.hpp +++ b/map.hpp @@ -96,8 +96,14 @@ public: } Point center_camera(const Point &around, size_t view_x, size_t view_y) { - size_t start_x = std::clamp(int(around.x - view_x / 2), 0, int(width() - view_x)); - size_t start_y = std::clamp(int(around.y - view_y / 2), 0, int(height() - view_y)); + int high_x = int(width() - view_x); + int high_y = int(height() - view_y); + int center_x = int(around.x - view_x / 2); + int center_y = int(around.y - view_y / 2); + + size_t start_x = std::clamp(center_x, 0, high_x); + size_t start_y = std::clamp(center_y, 0, high_y); + return {start_x, start_y}; } };