Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103// Known-answer test for the isometric tile→screen projection (the renderer's core transform).#include "sc2k/render/iso_projection.hpp"#include "../app/snapshot_layout.hpp"#include <cstdio>#include <cstdlib>#include <utility>
#include "test_harness.hpp"
int main() { using sc2k::render::tile_to_screen; auto eq = [](sc2k::render::ScreenPos p, int x, int y) { return p.x == x && p.y == y; };
// Snapshot framing retains the classic canvas and grows by the zoom-2 map projection span. { const auto small = sc2k::app::snapshot_layout(128); CHECK(small.width == 4200 && small.height == 2600 && small.cameraX == 2100 && small.cameraY == 420); const auto medium = sc2k::app::snapshot_layout(192); CHECK(medium.width == 6248 && medium.height == 3624 && medium.cameraX == medium.width / 2); const auto large = sc2k::app::snapshot_layout(256); CHECK(large.width == 8296 && large.height == 4648 && large.cameraX == large.width / 2); }
// The iso diamond at zoom 0, origin (0,0): row+ moves down-right, col+ moves down-left. CHECK(eq(tile_to_screen(0, 0, 0, 0, 0, 0), 0, 0)); // origin tile CHECK(eq(tile_to_screen(1, 0, 0, 0, 0, 0), 4, 2)); // +row → +x, +y CHECK(eq(tile_to_screen(0, 1, 0, 0, 0, 0), -4, 2)); // +col → −x, +y CHECK(eq(tile_to_screen(1, 1, 0, 0, 0, 0), 0, 4)); // diagonal → straight down
// Altitude lifts the tile up the screen (smaller y) by 3<<zoom per level. CHECK(eq(tile_to_screen(0, 0, 1, 0, 0, 0), 0, -3)); CHECK(eq(tile_to_screen(0, 0, 5, 0, 0, 0), 0, -15));
// Zoom doubles every factor. CHECK(eq(tile_to_screen(1, 0, 0, 1, 0, 0), 8, 4)); // 4<<1=8, 2<<1=4 CHECK(eq(tile_to_screen(1, 0, 1, 2, 0, 0), 16, -4)); // x=4<<2=16; y=(2<<2)·1 − (3<<2)·1 = 8−12 = −4
// The extra zoom (3) is the large (zoom-2) view pixel-doubled: every factor doubles once more, so a // tile's camera-relative offset is exactly 2× its zoom-2 offset (the renderer 2×-blits the large sprites // to match). This is what keeps picking + hover aligned at the closest zoom. CHECK(eq(tile_to_screen(1, 0, 1, 3, 0, 0), 32, -8)); // 2× of the zoom-2 (16,−4) above for (int alt : {0, 3, 31}) for (auto rc : {std::pair{10, 20}, std::pair{63, 63}, std::pair{127, 0}}) { const auto z2 = tile_to_screen(rc.first, rc.second, alt, 2, 0, 0); const auto z3 = tile_to_screen(rc.first, rc.second, alt, 3, 0, 0); CHECK(z3.x == 2 * z2.x && z3.y == 2 * z2.y); }
// Scroll origin is a pure translation. CHECK(eq(tile_to_screen(5, 3, 2, 2, 100, 200), 132, 240)); // 100+16*2 , 200+8*8−12*2
// origin_centering_tile is the exact inverse of tile_to_screen: a tile centred through it lands // on the requested screen point at *every* altitude and zoom. This is what makes Goto/Auto-Goto // and the view reset centre on hills instead of floating the target above the middle — the bug // being fixed was that the map-centre path dropped the altitude term and so mis-centred raised // cities. (See render::origin_centering_tile / CenterOnTileCoords.) using sc2k::render::origin_centering_tile; for (int zoom = 0; zoom <= 3; ++zoom) // incl. the extra zoom (3) for (int alt : {0, 1, 5, 16, 31}) for (auto rc : {std::pair{63, 63}, std::pair{40, 70}, std::pair{0, 0}, std::pair{127, 127}}) { const int sx = 640, sy = 400; // arbitrary target (a view centre) const auto o = origin_centering_tile(rc.first, rc.second, alt, zoom, sx, sy); CHECK(eq(tile_to_screen(rc.first, rc.second, alt, zoom, o.x, o.y), sx, sy)); }
// ── Zoom about the screen-centre focus point (render::zoom_origin_about, the original's zoom-in / // out). Centre an arbitrary tile at a focus point, change zoom one level via the origin rescale, and the // SAME tile (at its altitude) stays under the focus — "zoom about the focus", which the map-middle // recenter_map snap violated. Zoom-in is exact; zoom-out round-trips within 1px (signed integer /2). ── using sc2k::render::zoom_origin_about; for (int zoom = 0; zoom <= 2; ++zoom) // transitions 0→1, 1→2, 2→3 (the extra zoom) for (int alt : {0, 1, 7, 20, 31}) for (auto rc : {std::pair{63, 63}, std::pair{40, 70}, std::pair{10, 120}, std::pair{100, 25}}) { const int fx = 612, fy = 388; // an off-centre focus point const auto cam = origin_centering_tile(rc.first, rc.second, alt, zoom, fx, fy); CHECK(eq(tile_to_screen(rc.first, rc.second, alt, zoom, cam.x, cam.y), fx, fy)); // tile at focus const auto in = zoom_origin_about(cam, fx, fy, true); // zoom IN about the focus CHECK(eq(tile_to_screen(rc.first, rc.second, alt, zoom + 1, in.x, in.y), fx, fy)); // still at focus const auto out = zoom_origin_about(in, fx, fy, false); // zoom OUT back const auto p = tile_to_screen(rc.first, rc.second, alt, zoom, out.x, out.y); CHECK(std::abs(p.x - fx) <= 1 && std::abs(p.y - fy) <= 1); // returns within rounding } // Contrast: zoom is about the FOCUS, not the map middle. A tile NOT at the focus moves as p' = 2p − focus // on zoom-in, so a fixed map-middle recentre (which would pin tile 63,63 regardless of where you look) is // the wrong behaviour — this is what the user reported as "snapping to the centre of the map". { const int fx = 640, fy = 400, z = 1; const auto cam = origin_centering_tile(63, 63, 5, z, 200, 150); // tile 63,63 sits OFF the focus const auto p0 = tile_to_screen(63, 63, 5, z, cam.x, cam.y); const auto in = zoom_origin_about(cam, fx, fy, true); const auto p1 = tile_to_screen(63, 63, 5, z + 1, in.x, in.y); CHECK(p1.x == 2 * p0.x - fx && p1.y == 2 * p0.y - fy); // transforms about the focus CHECK(!(p1.x == p0.x && p1.y == p0.y)); // i.e. it does NOT stay put (≠ map-middle snap) }
if (failures == 0) { std::printf("iso_projection: all checks passed\n"); return 0; } std::printf("iso_projection: %d failure(s)\n", failures); return 1;}