// Pure (no-asset) test of the framebuffer: masked sprite blit, horizontal flip, clipping, BMP write. // Numeric verification of the blitter — the renderer's one mutable primitive. #include "sc2k/render/framebuffer.hpp" #include using namespace sc2k::render; #include "test_harness.hpp" // Palette stand-in: index i -> grey (i,i,i). struct GreyPal { Rgba operator[](std::uint8_t i) const { return Rgba{i, i, i, 0xFF}; } }; int main() { GreyPal pal; // A 2x2 sprite; top-left masked-out at (0,1) to test transparency. Sprite s; s.width = 2; s.height = 2; s.index = {10, 11, 12, 13}; // (0,0)(1,0) / (0,1)(1,1) s.mask = {1, 1, 0, 1}; // (0,1) transparent Framebuffer fb(4, 4); fb.clear(Rgba{0, 0, 0, 0xFF}); fb.blit_sprite(s, 1, 1, /*flip=*/false, pal); CHECK(fb.at(1, 1).r == 10); // idx10 CHECK(fb.at(2, 1).r == 11); // idx11 CHECK(fb.at(1, 2).r == 0); // masked -> background unchanged CHECK(fb.at(2, 2).r == 13); // idx13 // Horizontal flip mirrors each row: column 0<->1. fb.clear(Rgba{0, 0, 0, 0xFF}); fb.blit_sprite(s, 1, 1, /*flip=*/true, pal); CHECK(fb.at(1, 1).r == 11); // was (1,0)=idx11 CHECK(fb.at(2, 1).r == 10); // was (0,0)=idx10 CHECK(fb.at(1, 2).r == 13); // was (1,1)=idx13, mask 1 CHECK(fb.at(2, 2).r == 0); // was (0,1)=idx12, mask 0 -> transparent // Object highlight: tint follows the same flipped mask and leaves transparent pixels untouched. fb.clear(Rgba{0, 0, 0, 0xFF}); fb.blit_highlightable_sprite(s, 1, 1, /*flip=*/true, pal, /*scale=*/1, /*ownerRow=*/10, /*ownerCol=*/20, /*highlighted=*/true); CHECK(fb.at(1, 1).g > 11 && fb.at(1, 1).b > 11); // covered pixel shifted toward cyan CHECK(fb.at(2, 2).r == 0 && fb.at(2, 2).g == 0); // transparent source stays untouched // A selection-only mask never repaints the sprite's ordinary pixels. It is inert when this owner // is not highlighted, then tints covered pixels while preserving the existing background. fb.clear(Rgba{5, 6, 7, 0xFF}); fb.blit_selection_sprite(s, 1, 1, /*flip=*/false, pal, /*scale=*/1, /*ownerRow=*/10, /*ownerCol=*/20, /*highlighted=*/false); CHECK(fb.at(1, 1).r == 5 && fb.at(1, 1).g == 6 && fb.at(1, 1).b == 7); fb.blit_selection_sprite(s, 1, 1, /*flip=*/false, pal, /*scale=*/1, /*ownerRow=*/10, /*ownerCol=*/20, /*highlighted=*/true); CHECK(fb.at(1, 1).g > 6 && fb.at(1, 1).b > 7); CHECK(fb.at(1, 2).r == 5 && fb.at(1, 2).g == 6 && fb.at(1, 2).b == 7); // Direct-colour authored sprite bypasses the classic palette and supports straight alpha. Sprite rgba; rgba.width = 1; rgba.height = 1; rgba.mask = {1}; rgba.rgba = {Rgba{200, 100, 50, 128}}; fb.clear(Rgba{0, 0, 0, 0xFF}); fb.blit_sprite(rgba, 0, 0, false, pal); CHECK(fb.at(0, 0).r == 100 && fb.at(0, 0).g == 50 && fb.at(0, 0).b == 25); // Clipping: blit partially off the top-left and bottom-right; no crash, only in-bounds written. fb.clear(Rgba{5, 5, 5, 0xFF}); fb.blit_sprite(s, -1, -1, false, pal); // only (0,0) of fb gets sprite (1,1)=idx13 CHECK(fb.at(0, 0).r == 13); fb.blit_sprite(s, 3, 3, false, pal); // only fb(3,3) gets sprite(0,0)=idx10 CHECK(fb.at(3, 3).r == 10); // BMP writer round-trips to a file (header + size sane). Framebuffer img(3, 2); img.clear(Rgba{1, 2, 3, 0xFF}); CHECK(img.write_bmp("build/_fb_test.bmp")); if (std::FILE* f = std::fopen("build/_fb_test.bmp", "rb")) { std::fseek(f, 0, SEEK_END); long n = std::ftell(f); CHECK(n == 14 + 40 + (3 * 3 + 3) * 2); // 3px*3B = 9, padded to 12, *2 rows std::fclose(f); } else CHECK(false); // --- alpha blend (the debug overlay/HUD primitive): integer src-over, rounded --- Framebuffer bl(4, 4); bl.clear(Rgba{0, 0, 0, 0xFF}); bl.blend(0, 0, Rgba{255, 255, 255, 0xFF}, 0); // alpha 0 -> destination unchanged CHECK(bl.at(0, 0).r == 0); bl.blend(1, 0, Rgba{200, 100, 50, 0xFF}, 255); // alpha 255 -> full overwrite CHECK(bl.at(1, 0).r == 200 && bl.at(1, 0).g == 100 && bl.at(1, 0).b == 50); bl.blend(2, 0, Rgba{255, 255, 255, 0xFF}, 128); // 128 over black -> ~127.5 (within +/-1) CHECK(bl.at(2, 0).r >= 127 && bl.at(2, 0).r <= 128); bl.clear(Rgba{100, 100, 100, 0xFF}); bl.blend(0, 0, Rgba{200, 200, 200, 0xFF}, 128); // exact midpoint of 100 and 200 = 150 CHECK(bl.at(0, 0).r == 150); bl.blend(-1, -1, Rgba{255, 0, 0, 0xFF}, 200); // out of bounds -> clipped, no crash bl.blend(99, 99, Rgba{255, 0, 0, 0xFF}, 200); bl.blend_rect(0, 0, 2, 2, Rgba{0, 0, 200, 0xFF}, 255); // fill a 2x2 (overwrite) CHECK(bl.at(0, 0).b == 200 && bl.at(1, 1).b == 200 && bl.at(2, 2).b == 100); if (failures == 0) { std::printf("framebuffer: all checks passed\n"); return 0; } std::printf("framebuffer: %d failure(s)\n", failures); return 1; }