From cd688b8abeba11969bc7b95328f0892e42bded73 Mon Sep 17 00:00:00 2001 From: Dejvino Date: Sat, 14 Nov 2026 15:33:25 +0100 Subject: [PATCH] NetDeck: transparent color-key dither for the freeze tint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - stampImage16_P(): PROGMEM 16bpp stamp with color-key transparency (ND_TRANS16 pixels leave the destination untouched) — TFT_eSprite has no transparent pushImage variant, so it composites via drawPixel on both backends (sprite RAM on device, host framebuffer in the emulator) - shim: pgm_read_byte/word/dword macros (flash is memory-mapped on host and ESP32 alike) - pattern: 2px checker of black dots + transparent cells — half-visible dither, map/units stay readable underneath - E2E now proves the transparency itself: wall pixels survive under transparent cells, dots render black, key color never rendered --- emulator/src/shim/Arduino.h | 3 +++ emulator/tests/e2e_tests.cpp | 14 ++++++----- src/netdeck/NetDeck.ino | 49 +++++++++++++++++++++++++----------- 3 files changed, 45 insertions(+), 21 deletions(-) diff --git a/emulator/src/shim/Arduino.h b/emulator/src/shim/Arduino.h index caacd1b..5e3150b 100644 --- a/emulator/src/shim/Arduino.h +++ b/emulator/src/shim/Arduino.h @@ -12,6 +12,9 @@ // Host flash is memory-mapped, so flash-resident data needs no special read // macro (same as on ESP32; real AVR pgm_read is not used by our sketches). #define PROGMEM +#define pgm_read_byte(addr) (*(const unsigned char *)(addr)) +#define pgm_read_word(addr) (*(const unsigned short *)(addr)) +#define pgm_read_dword(addr) (*(const unsigned long *)(addr)) #include #include diff --git a/emulator/tests/e2e_tests.cpp b/emulator/tests/e2e_tests.cpp index 200aaef..105564d 100644 --- a/emulator/tests/e2e_tests.cpp +++ b/emulator/tests/e2e_tests.cpp @@ -106,6 +106,7 @@ TEST(e2e/boot_and_controls) schedule(2.2, 'p'); // -> Network view schedule(2.6, ' '); // freeze OFF schedule(3.0, 'r'); // reset (re-pauses) + schedule(3.4, 'p'); // back to Cameras (full map) test::serialClear(); EXPECT(pumpFor(4.5)); // run the inputs through loop() EXPECT(test::serialHas("[key] view -> NETWORK")); @@ -114,12 +115,13 @@ TEST(e2e/boot_and_controls) // HUD separator rendered across the top of the HUD bar EXPECT(test::pixelIs565(160, nd::HUD_Y, nd::C_BORDER)); - // paused -> checkered freeze tint: 2px checker of black + dark teal - // P(x,y): black when ((x>>1)+(y>>1)) is even - EXPECT(test::pixelIs565(50, 0, 0x1F1F)); // (25+0) odd -> teal - EXPECT(test::pixelIs565(50, 2, 0x0000)); // (25+1) even -> black - EXPECT(test::pixelIs565(52, 0, 0x0000)); // (26+0) even -> black - EXPECT(!test::pixelIs565(52, 0, 0x1F1F)); + // paused -> dithered freeze tint with color-key transparency: + // black dots where ((x>>1)+(y>>1)) is even, map shows through elsewhere + EXPECT(test::pixelIs565(50, 2, 0x0000)); // dither dot drawn + EXPECT(test::pixelIs565(52, 0, 0x0000)); // dither dot drawn + EXPECT(test::pixelIs565(50, 0, nd::C_WALL)); // transparent: wall pixel + EXPECT(test::pixelIs565(50, 4, nd::C_WALL)); // survives untouched + EXPECT(!test::pixelIs565(50, 0, 0x1F1F)); // key color never rendered } TEST(e2e/network_journey) diff --git a/src/netdeck/NetDeck.ino b/src/netdeck/NetDeck.ino index 900aa7e..bc13dfd 100644 --- a/src/netdeck/NetDeck.ino +++ b/src/netdeck/NetDeck.ino @@ -1264,28 +1264,47 @@ static void renderCursor() } } -// 8x8 stamp for the tactical-freeze tint: 2px checkerboard of black and a -// dark teal (the map stays readable underneath). RGB565 has no alpha, so a -// dither is the way to "tint" — same trick the old scanlines used. +// Transparency key for 16bpp stamps: RGB565 has no alpha channel, so a +// stamped pixel equal to this key leaves the destination pixel untouched +// (color-key transparency). Chosen outside the nd_layout.h palette so it +// can never collide with real drawing colors. +#define ND_TRANS16 0x1F1F + +// 8x8 dither stamp for the tactical-freeze tint: 2px checkerboard — black +// dots are drawn, transparent cells let the frozen game view show through. +// Half coverage dims the map without hiding it. static const uint16_t PROGMEM checkered8x8[64] = { - 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, - 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, - 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, - 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, - 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, - 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, - 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, - 0x1F1F, 0x1F1F, 0x0000, 0x0000, 0x1F1F, 0x1F1F, 0x0000, 0x0000, + 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, + 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, + ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, + ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, + 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, + 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, + ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, + ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, ND_TRANS16, ND_TRANS16, 0x0000, 0x0000, }; +// Stamp a PROGMEM 16bpp image with color-key transparency. TFT_eSprite has no +// transparent pushImage() variant (only pushSprite() to the TFT does), so the +// composite is done pixel by pixel via drawPixel(); on the emulator the same +// call writes the host framebuffer. Clipping is drawPixel's job on both. +static void stampImage16_P(int x, int y, int w, int h, + const uint16_t *data, uint16_t transp) +{ + for (int j = 0; j < h; j++) { + for (int i = 0; i < w; i++) { + uint16_t c = pgm_read_word(&data[j * w + i]); + if (c != transp) GFXP->drawPixel(x + i, y + j, c); + } + } +} + static void renderFreezeTint() { - // Stamp the checkered 8x8 pattern across the view. pushImage() exists on - // BOTH backends: device = TFT_eSprite::pushImage (blits into the fb - // sprite RAM), emulator = the shim blit into the host framebuffer. + // Stamp the dithered, transparent-keyed 8x8 pattern across the view for (int by = 0; by < nd::VIEW_H; by += 8) { for (int bx = 0; bx < nd::VIEW_W; bx += 8) { - GFXP->pushImage(bx, by, 8, 8, checkered8x8); + stampImage16_P(bx, by, 8, 8, checkered8x8, ND_TRANS16); } } GFXP->setTextDatum(TR_DATUM);