// Numeric geometry and raster checks for the read-only commute-route developer overlay. #include "sc2k/debug/commute_overlay.hpp" #include using namespace sc2k; #include "test_harness.hpp" namespace { bool changed(render::Rgba pixel) { return pixel.r != 0 || pixel.g != 0 || pixel.b != 0; } } // namespace int main() { City city; city.altm.at(20, 20) = 2; city.altm.at(20, 21) = 2; city.altm.at(21, 21) = 2; city.altm.at(21, 22) = 2; CommuteRouteHistory history; CommuteRouteTrace accepted; accepted.sampleKey = 1; accepted.originRow = 20; accepted.originColumn = 19; accepted.terminalRow = 21; accepted.terminalColumn = 23; accepted.stop = CommuteRouteStop::Destination; accepted.points = { {20, 20, CommuteTravelMode::Car}, {20, 21, CommuteTravelMode::Bus}, {21, 21, CommuteTravelMode::Rail}, {21, 22, CommuteTravelMode::Subway}, }; history.consider(std::move(accepted)); CommuteRouteTrace failed; failed.sampleKey = 2; failed.originRow = 24; failed.originColumn = 24; failed.stop = CommuteRouteStop::CostLimit; history.consider(std::move(failed)); const auto trafficBefore = city.xtrf.cells; const auto altitudeBefore = city.altm.cells; for (int rotation = 0; rotation < 4; ++rotation) { int anchorViewRow, anchorViewColumn; render::rotate_index_cw_n(city.dimension(), 20, 20, rotation, anchorViewRow, anchorViewColumn); const render::ScreenPos anchor = render::tile_to_screen( anchorViewRow, anchorViewColumn, 2, 2, 0, 0); const int cameraX = 700 - anchor.x - 16; const int cameraY = 600 - anchor.y - 8; render::Framebuffer target(1400, 1200); target.clear({0, 0, 0, 0xFF}); debug::draw_commute_route_overlay(target, city, history, cameraX, cameraY, rotation, 2); int changedPixels = 0; for (render::Rgba pixel : target.pixels()) changedPixels += changed(pixel); CHECK(changedPixels > 450); const render::ScreenPos failure = debug::commute_route_screen_point( city, 24, 24, cameraX, cameraY, rotation, 2); CHECK(failure.x >= 0 && failure.x < target.width()); CHECK(failure.y >= 0 && failure.y < target.height()); CHECK(changed(target.at(failure.x, failure.y))); int viewRow, viewColumn; render::rotate_index_cw_n(city.dimension(), 20, 20, rotation, viewRow, viewColumn); render::ScreenPos expected = render::tile_to_screen( viewRow, viewColumn, 2, 2, cameraX, cameraY); expected.x += 16; expected.y += 8; const render::ScreenPos actual = debug::commute_route_screen_point( city, 20, 20, cameraX, cameraY, rotation, 2); CHECK(actual.x == expected.x && actual.y == expected.y); } const render::ScreenPos unscaled = debug::commute_route_screen_point( city, 20, 20, 650, -100, 0, 2); const render::ScreenPos scaled = debug::commute_route_screen_point( city, 20, 20, 650, -100, 0, 2, 1.5f); CHECK(scaled.x == int(std::lround(unscaled.x * 1.5f))); CHECK(scaled.y == int(std::lround(unscaled.y * 1.5f))); CHECK(debug::commute_route_stroke_radius(1.f) == 2); CHECK(debug::commute_route_stroke_radius(1.5f) == 3); CHECK(debug::commute_route_dash_length(1.f) == 4); CHECK(debug::commute_route_dash_length(1.5f) == 6); render::Framebuffer stroke(48, 32); stroke.clear({0, 0, 0, 0xFF}); debug::blend_commute_line(stroke, {8, 16}, {40, 16}, {0xFF, 0xFF, 0xFF, 0xFF}, 255, debug::commute_route_stroke_radius(1.f)); CHECK(changed(stroke.at(24, 14))); CHECK(changed(stroke.at(24, 18))); CHECK(!changed(stroke.at(24, 13))); CHECK(!changed(stroke.at(24, 19))); CHECK(city.xtrf.cells == trafficBefore); CHECK(city.altm.cells == altitudeBefore); CHECK(debug::commute_mode_color(CommuteTravelMode::Car).r != debug::commute_mode_color(CommuteTravelMode::Rail).r); CHECK(debug::commute_failure_color(CommuteRouteStop::CostLimit).r == 0xFF); CHECK(debug::commute_failure_color(CommuteRouteStop::ServiceUnavailable).g == 0xE0); if (failures == 0) { std::puts("commute overlay: route geometry, rotations, scaling, and failure markers passed"); return 0; } std::printf("commute overlay: %d failure(s)\n", failures); return 1; }