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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107// Numeric tests for on-map sign labels (render::sign_layout, a port of the original's sign draw). Per the project// rule "verify visuals numerically, not by eye", this locks the plaque + signpost geometry against the// constants measured from the original game, and pins the two bugs the user reported://// 1. Signs "spread out" at different zoom levels — the draw code hardcoded the zoom-2 projection while// the camera tracked the live zoom, so a sign was laid out 4×/2× too far from its tile at zoom 0/1// (and at the 2× extra zoom 3). FIX: project at the live zoom. Part 3 is the load-bearing A/B.// 2. The signpost was a fixed 6 px stub floating ~14 px above the tile. FIX: the post runs from the// plaque bottom DOWN to the tile anchor (height 15·zoom+20), so it points at the labelled tile.// Part 2 pins post bottom == anchor and the per-zoom length.#include "sc2k/render/sign_layout.hpp"#include "sc2k/render/iso_projection.hpp"#include <cstdio>
using namespace sc2k::render;
#include "test_harness.hpp"
int main() { CHECK(sign_font_pixel_size(1.f) == 12); CHECK(sign_font_pixel_size(1.5f) == 18); CHECK(sign_font_pixel_size(2.f) == 24); // ── Part 1: the per-zoom constants are the original's (x += 4<<zoom horizontally; plaque bottom = // anchor − 15·zoom − 20; post height = 15·zoom + 20; plaque width = text+16). The VERTICAL anchor is // p.y + 2<<zoom (the tile-diamond CENTRE) — the original shifts y by −2<<zoom, but its anchor // is the tile's front/surface reference while Libreopolis's tile_to_screen returns the top-back vertex // (one diamond-height, 4<<zoom, higher), so re-deriving in our frame flips the sign — see sign_layout.hpp. ── // Literals: zoom 0 (+4,+2), zoom 1 (+8,+4), zoom 2 (+16,+8). struct { int zoom, dx, dy, post; } K[] = {{0, 4, 2, 20}, {1, 8, 4, 35}, {2, 16, 8, 50}}; const ScreenPos p{500, 400}; const int textW = 37, fontH = 7; for (auto k : K) { const SignLayout s = sign_layout(p, k.zoom, textW, fontH); // anchor = projected point + tile-centre shift (x to the horizontal centre, y down to the diamond centre) CHECK(s.anchorX == p.x + k.dx); CHECK(s.anchorY == p.y + k.dy); // post is a 4 px column centred on the anchor, running from the plaque bottom to the anchor CHECK(s.postW == 4); CHECK(s.postX == s.anchorX - 2); CHECK(s.postY == s.plaqueY + s.plaqueH); // post top == plaque bottom (flush) CHECK(s.postY + s.postH == s.anchorY); // post BOTTOM rests on the tile anchor (bug 2) CHECK(s.postH == k.post); // 15·zoom + 20 // plaque: centred on the anchor, width text+16, height font+5, text inset (left+8, top+2) CHECK(s.plaqueW == textW + 16); CHECK(s.plaqueH == fontH + 5); CHECK(s.plaqueX + s.plaqueW / 2 == s.anchorX); // plaque horizontally centred on the post CHECK(s.postX + s.postW / 2 == s.anchorX); CHECK(s.textX == s.plaqueX + 8); CHECK(s.textY == s.plaqueY + 2); CHECK(s.plaqueY + s.plaqueH == s.anchorY - 15 * k.zoom - 20); // plaque bottom }
// ── Part 2: the post always reaches the tile, and grows with zoom (no floating stub at any zoom, // including the 2× extra zoom 3 = 15·3+20 = 65). ── for (int zoom = 0; zoom <= 3; ++zoom) { const SignLayout s = sign_layout(p, zoom, textW, fontH); CHECK(s.postH == 15 * zoom + 20); CHECK(s.postH > 0); CHECK(s.postY + s.postH == s.anchorY); // tip on the anchor at EVERY zoom }
// ── Part 3 (load-bearing): a sign tracks its tile across all zoom levels. ── // Centre the camera so tile (r,c) at altitude `alt` sits at a fixed screen point (sx,sy) for the // current zoom (origin_centering_tile, the engine's own centring inverse). The sign anchor must then // land at (sx + 4<<zoom, sy − 2<<zoom) at EVERY zoom — i.e. it follows the tile. The OLD code, which // projected at a hardcoded zoom 2 while the camera used the live zoom, drifted away as zoom changed. const int r = 40, c = 70, alt = 9, sx = 640, sy = 360; for (int zoom = 0; zoom <= 3; ++zoom) { const ScreenPos cam = origin_centering_tile(r, c, alt, zoom, sx, sy); const ScreenPos anchorP = tile_to_screen(r, c, alt, zoom, cam.x, cam.y); CHECK(anchorP.x == sx && anchorP.y == sy); // sanity: the tile lands where we centred it const SignLayout s = sign_layout(anchorP, zoom, textW, fontH); CHECK(s.anchorX == sx + (4 << zoom)); // sign stays glued to the tile, all zooms CHECK(s.anchorY == sy + (2 << zoom)); // …and down at the diamond centre (see Part 1)
// Demonstrate the bug the fix removes: the buggy path projected at zoom 2 regardless. At any // zoom != 2 that misplaces the anchor — by a lot at zoom 0 (≈4× spacing) — proving the live-zoom // projection is load-bearing, not cosmetic. const ScreenPos buggyP = tile_to_screen(r, c, alt, /*hardcoded=*/2, cam.x, cam.y); if (zoom != 2) CHECK(buggyP.x != anchorP.x || buggyP.y != anchorP.y); else CHECK(buggyP.x == anchorP.x && buggyP.y == anchorP.y); }
// ── Part 4: modern display-space signs use the GPU world's exact camera transform. At 2x, a // half-world-pixel threshold is one physical pixel: the sign and its underlying tile must advance // on the same frame. The former path truncated cameraX to an int first, so both anchors were 302. const ScreenPos projectedWithoutCamera{125, 80}; const ScreenPos beforeThreshold = sign_output_anchor( projectedWithoutCamera, 2, 10.24, -3.0, 2.f); const ScreenPos afterThreshold = sign_output_anchor( projectedWithoutCamera, 2, 10.26, -3.0, 2.f); CHECK(beforeThreshold.x == 302); CHECK(afterThreshold.x == 303); CHECK(beforeThreshold.y == afterThreshold.y);
if (failures == 0) { std::printf("signs: all checks passed\n"); return 0; } std::printf("signs: %d failure(s)\n", failures); return 1;}