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}; } };