// Numeric contract for typed connection arrows: visibility, rotation, zoom and direct-colour rasterisation. #include "sc2k/render/connection_arrow.hpp" #include "test_harness.hpp" #include #include using namespace sc2k; int main() { using sim::BorderConnection; using sim::BorderConnectionKind; const std::array links = {{ {63, 10, 0, 10, BorderConnectionKind::Road, sim::BorderConnectionLayer::Surface, 1}, {20, 63, 1, 20, BorderConnectionKind::Rail, sim::BorderConnectionLayer::Surface, 1}, {0, 30, 2, 30, BorderConnectionKind::Highway, sim::BorderConnectionLayer::Surface, 2}, {40, 0, 3, 40, BorderConnectionKind::Electricity, sim::BorderConnectionLayer::Electricity, 1}, {63, 50, 0, 50, BorderConnectionKind::WaterPipe, sim::BorderConnectionLayer::Underground, 1}, }}; auto altitude = [](int, int) { return 0; }; const render::ConnectionArrowVisibility surface{}; for (int rotation = 0; rotation < 4; ++rotation) { for (int zoom = 0; zoom < 3; ++zoom) { const auto arrows = render::describe_connection_arrows( std::span(links), 64, rotation, zoom, surface, 7, altitude); CHECK(arrows.size() == 4); // water pipe is an underground-layer overlay CHECK(arrows[2].connection.kind == BorderConnectionKind::Highway); CHECK(arrows[2].pulse_phase == 3); CHECK(render::connection_arrow_decals(arrows, zoom).size() == arrows.size()); } } // Physical edge zero rotates through all four screen-space directions, without changing identity. std::array directions{}; for (int rotation = 0; rotation < 4; ++rotation) { const auto one = render::describe_connection_arrows( std::span(links.data(), 1), 64, rotation, 1, surface, 0, altitude); CHECK(one.size() == 1); directions[static_cast(rotation)] = one.front().direction; CHECK(one.front().connection == links.front()); } for (std::size_t left = 0; left < directions.size(); ++left) for (std::size_t right = left + 1; right < directions.size(); ++right) CHECK(directions[left] != directions[right]); render::ConnectionArrowVisibility underground{ .transportation = true, .power_lines = true, .underground = true, .water_pipes = true}; auto underground_arrows = render::describe_connection_arrows( std::span(links), 64, 0, 1, underground, 0, altitude); CHECK(underground_arrows.size() == 1); CHECK(underground_arrows.front().connection.kind == BorderConnectionKind::WaterPipe); underground.water_pipes = false; CHECK(render::describe_connection_arrows( std::span(links), 64, 0, 1, underground, 0, altitude) .empty()); // The code-generated sprites have transparent coverage and alpha-blend numerically in the same // Framebuffer target used by the software city renderer. GPU upload consumes the same direct RGBA. std::array covered{}; const std::array unused_palette{}; for (int zoom = 0; zoom < 3; ++zoom) { const render::Sprite& sprite = render::connection_arrow_sprite( zoom, render::ConnectionArrowDirection::ColumnPositive); CHECK(sprite.direct_colour()); CHECK(sprite.mask.size() == sprite.rgba.size()); covered[static_cast(zoom)] = static_cast(std::count(sprite.mask.begin(), sprite.mask.end(), std::uint8_t{1})); CHECK(covered[static_cast(zoom)] > 0); CHECK(covered[static_cast(zoom)] < sprite.mask.size()); render::Framebuffer framebuffer(sprite.width + 4, sprite.height + 4); framebuffer.clear({8, 12, 16, 255}); framebuffer.blit_sprite(sprite, 2, 2, false, unused_palette); std::size_t changed = 0; for (const render::Rgba pixel : framebuffer.pixels()) if (pixel.r != 8 || pixel.g != 12 || pixel.b != 16) ++changed; CHECK(changed == covered[static_cast(zoom)]); } CHECK(covered[0] < covered[1]); CHECK(covered[1] < covered[2]); if (failures) { std::printf("test_connection_arrows: %d FAILED\n", failures); return 1; } std::printf("test_connection_arrows: visibility, rotation, zoom and raster coverage passed\n"); return 0; }