Initial fix of the crash with different map sizes but that's not the ultimate fix.

main
Zed A. Shaw 1 month ago
parent 809ec9ed0d
commit 2dccc6b17b
  1. 12
      gui.cpp
  2. 10
      map.hpp

@ -5,6 +5,7 @@
#include <string> // for string, operator<<
#include <thread> // for sleep_for
#include <array>
#include <algorithm>
#include <ftxui/dom/elements.hpp> // for hflow, paragraph, separator, hbox, vbox, filler, operator|, border, Element
#include <ftxui/dom/node.hpp> // 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);

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

Loading…
Cancel
Save