From 1962b0c24eb6edab6a05e2e796b61712a86ce223 Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Wed, 1 Jan 2025 10:09:18 -0500 Subject: [PATCH] Started prototyping a viewport iterator but couldn't get it right so I'll test drive it next. --- gui.cpp | 15 ++++++++------- matrix.hpp | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/gui.cpp b/gui.cpp index 6b8b661..aaeca80 100644 --- a/gui.cpp +++ b/gui.cpp @@ -98,7 +98,8 @@ void MapViewUI::draw_map() { size_t end_y = std::min(size_t(height), $game_map.height() - start.y); for(size_t y = 0; y < end_y; ++y) { - for(size_t x = 0; x < end_x; ++x) { + for(size_t x = 0; x < end_x; ++x) + { const TileCell& tile = tiles.at(start.x+x, start.y+y); // light value is an integer that's a percent float light_value = debug.LIGHT ? 80 * PERCENT : lighting[start.y+y][start.x+x] * PERCENT; @@ -108,14 +109,14 @@ void MapViewUI::draw_map() { string num = dnum > 15 ? "*" : format("{:x}", dnum); $canvas.DrawText(x * 2, y * 4, num, [dnum, tile, light_value](auto &pixel) { - pixel.foreground_color = Color::HSV(dnum * 20, 150, 200); - pixel.background_color = Color::HSV(30, 20, tile.bg_v * 50 * PERCENT); - }); + pixel.foreground_color = Color::HSV(dnum * 20, 150, 200); + pixel.background_color = Color::HSV(30, 20, tile.bg_v * 50 * PERCENT); + }); } else { $canvas.DrawText(x * 2, y * 4, tile.display, [tile, light_value](auto &pixel) { - pixel.foreground_color = Color::HSV(tile.fg_h, tile.fg_s, tile.fg_v * light_value); - pixel.background_color = Color::HSV(tile.bg_h, tile.bg_s, tile.bg_v * light_value); - }); + pixel.foreground_color = Color::HSV(tile.fg_h, tile.fg_s, tile.fg_v * light_value); + pixel.background_color = Color::HSV(tile.bg_h, tile.bg_s, tile.bg_v * light_value); + }); } } } diff --git a/matrix.hpp b/matrix.hpp index 52224c7..e9266fa 100644 --- a/matrix.hpp +++ b/matrix.hpp @@ -78,6 +78,41 @@ namespace matrix { } }; + template + struct viewport_t { + Point start; + // this is the point in the map + size_t x; + size_t y; + // this is the point inside the box, start at 0 + size_t view_x = ~0; + size_t view_y = ~0; + // viewport width/height + size_t width; + size_t height; + + viewport_t(MAT &mat, Point start, int max_x, int max_y) : + start(start), + x(start.x-1), + y(start.y-1) + { + width = std::min(size_t(max_x), matrix::width(mat) - start.x); + height = std::min(size_t(max_y), matrix::height(mat) - start.y); + fmt::println("viewport_t max_x, max_y {},{} vs matrix {},{}, x={}, y={}", + max_x, max_y, matrix::width(mat), matrix::height(mat), x, y); + } + + bool next() { + y = next_y(x, y); + x = next_x(x, width); + view_x = next_x(view_x, width); + view_y = next_y(view_x, view_y); + return at_end(y, height); + } + }; + + using viewport = viewport_t; + using each_cell = each_cell_t; template