diff --git a/examples/MiataDrive/MiataDrive.ino b/examples/MiataDrive/MiataDrive.ino index 9180d30..2b2da30 100644 --- a/examples/MiataDrive/MiataDrive.ino +++ b/examples/MiataDrive/MiataDrive.ino @@ -26,6 +26,17 @@ TFT_eSPI tft; SoundEngine sound; +// Render every frame into a RAM buffer and push it to the panel in one +// continuous SPI stream. Painting straight onto the panel with thousands of +// small draw calls lets the panel's own scan-out overtake the drawing, which +// shows up as flicker / tearing; a single push writes each pixel exactly once. +#ifndef EMULATOR +static TFT_eSprite fb(&tft); // full-frame framebuffer (auto-allocates in PSRAM) +static TFT_eSprite *const GFXP = &fb; +#else +static TFT_eSPI *const GFXP = &tft; // emulator shim draws straight into the SDL framebuffer +#endif + static int scrW, scrH; // --------------------------------------------------------------------------- @@ -292,16 +303,22 @@ static void fillSpan(int y, float xa, float xb, uint16_t c) { if (b < 0 || a >= scrW) return; if (a < 0) a = 0; if (b >= scrW) b = scrW - 1; - tft.drawFastHLine(a, y, b - a + 1, c); + GFXP->drawFastHLine(a, y, b - a + 1, c); } static void fillTrap(float y1, float xa1, float xb1, float y2, float xa2, float xb2, uint16_t c) { if (y1 == y2) { fillSpan(iround(y1), xa1, xb1, c); return; } int yTop = (int)ceilf(y1 < y2 ? y1 : y2); int yBot = (int)floorf(y1 < y2 ? y2 : y1); + float dyr = y2 - y1; + float dxa = (xa2 - xa1) / dyr; + float dxb = (xb2 - xb1) / dyr; + float seed = ((float)yTop - y1) / dyr; + xa1 += dxa * seed; // step to the first drawn row, then + xb1 += dxb * seed; // increment instead of dividing per row for (int y = yTop; y <= yBot; y++) { - float t = ((float)y - y1) / (y2 - y1); - fillSpan(y, xa1 + (xa2 - xa1) * t, xb1 + (xb2 - xb1) * t, c); + fillSpan(y, xa1, xb1, c); + xa1 += dxa; xb1 += dxb; } } @@ -328,14 +345,14 @@ static void drawSprite(int id, int destW, float sx, float bottomY, float clipY, if (c != lastC) { if (lastC >= 0) { int dx0 = x0 + runStart, wln = x - runStart; - if (dx0 < scrW && dx0 + wln > 0) tft.fillRect(dx0, dy, wln, 1, (uint16_t)lastC); + if (dx0 < scrW && dx0 + wln > 0) GFXP->fillRect(dx0, dy, wln, 1, (uint16_t)lastC); } runStart = x; lastC = c; } } else { if (lastC >= 0) { int dx0 = x0 + runStart, wln = x - runStart; - if (dx0 < scrW && dx0 + wln > 0) tft.fillRect(dx0, dy, wln, 1, (uint16_t)lastC); + if (dx0 < scrW && dx0 + wln > 0) GFXP->fillRect(dx0, dy, wln, 1, (uint16_t)lastC); } lastC = -2; } @@ -351,15 +368,13 @@ static Proj P1[DRAW_DIST + 2]; static Proj P2[DRAW_DIST + 2]; static int segIdxN[DRAW_DIST + 2]; static bool slotValid[DRAW_DIST + 2]; +static bool roadDraw[DRAW_DIST + 2]; static void renderRoad() { // LOW horizon (OutRun-style) -> camera feels planted on the road const float HORIZON_Y = scrH * 0.36f; - // sky down to just past the vanishing point int skyBot = (int)HORIZON_Y + 14; int skySplit = skyBot * 2 / 5; - tft.fillRect(0, 0, scrW, skySplit, colSkyHi); - tft.fillRect(0, skySplit, scrW, skyBot - skySplit, colSkyLo); float basePercent = fmodf(position_, SEG_LEN) / SEG_LEN; int baseIdx = ((int)(position_ / SEG_LEN)) % segCount; @@ -369,9 +384,11 @@ static void renderRoad() { // whiplash camera jolt if ((int32_t)(millis() - shakeUntil) < 0) camX += (float)(random(201) - 100); + // --- pass 1: project every segment and work out hill occlusion --- float x = 0; float dx = -(bs.curve * basePercent); float maxY = (float)scrH; + const float NEAR_Z = CAM_D * 30.0f; for (int n = 0; n < DRAW_DIST; n++) { int idx = (baseIdx + n) % segCount; @@ -386,7 +403,6 @@ static void renderRoad() { // (avoids a gap where the camera's own segment is skipped) float z1p = z1; float xn = x; - const float NEAR_Z = CAM_D * 30.0f; if (z1p < NEAR_Z) { float frac = (NEAR_Z - z1) / SEG_LEN; if (frac > 1.0f) frac = 1.0f; @@ -409,23 +425,53 @@ static void renderRoad() { seg.clip = maxY; segIdxN[n] = idx; slotValid[n] = (z1 > NEAR_Z) && (z1 < DRAW_DIST * SEG_LEN * 2.0f); + roadDraw[n] = false; x += dx; dx += seg.curve; if (z2 <= NEAR_Z) continue; // fully behind camera if (P2[n].y >= maxY || P2[n].y >= P1[n].y) continue; // hidden or backfacing + roadDraw[n] = true; + maxY = P2[n].y; + } + // Sky down to just past the vanishing point — or down to the topmost road + // row, so no stale pixels are left between the horizon and the far road + // (that gap used to flicker when cresting hills). + int roadTop = (maxY >= (float)scrH) ? scrH : iround(maxY); + if (roadTop < 0) roadTop = 0; + int skyFill = (skyBot > roadTop) ? skyBot : roadTop; + GFXP->fillRect(0, 0, scrW, skySplit, colSkyHi); + GFXP->fillRect(0, skySplit, scrW, skyFill - skySplit, colSkyLo); + + // --- pass 2: draw the road near -> far from the cached projections --- + bool nearest = true; + for (int n = 0; n < DRAW_DIST; n++) { + if (!roadDraw[n]) continue; + int idx = segIdxN[n]; + const Segment &seg = segments[idx]; int fog = 0; if (n > DRAW_DIST * 3 / 4) fog = 2; else if (n > DRAW_DIST / 2) fog = 1; bool light = ((idx / RUMBLE_SEGS) % 2) == 0; + // the nearest segment runs off the bottom edge: extend its near + // cross-section so no stale strip is left under the road + if (nearest && P2[n].y > P1[n].y) { + float yExt = (float)scrH; + float t = (yExt - P1[n].y) / (P2[n].y - P1[n].y); + P1[n].x += (P2[n].x - P1[n].x) * t; + P1[n].w += (P2[n].w - P1[n].w) * t; + P1[n].y = yExt; + } + nearest = false; + // grass band uint16_t grass = light ? palGrassL[fog] : palGrassD[fog]; int yTop = iround(P2[n].y), yBot = iround(P1[n].y) - 1; - if (yBot > (int)maxY - 1) yBot = (int)maxY - 1; - if (yTop <= yBot && yBot >= 0) tft.fillRect(0, yTop, scrW, yBot - yTop + 1, grass); + if (yBot > (int)seg.clip - 1) yBot = (int)seg.clip - 1; + if (yTop <= yBot && yBot >= 0) GFXP->fillRect(0, yTop, scrW, yBot - yTop + 1, grass); // rumble strips (wide, for speed sensation) float r1 = P1[n].w / 6.0f, r2 = P2[n].w / 6.0f; @@ -445,8 +491,6 @@ static void renderRoad() { fillTrap(P1[n].y, P1[n].x - l1, P1[n].x + l1, P2[n].y, P2[n].x - l2, P2[n].x + l2, palLane[fog]); } - - maxY = P2[n].y; } } @@ -509,14 +553,14 @@ static void drawPlayerCar() { int dy0 = baseY - 2 - (int)random(14); int r = 1 + (int)random(3); uint8_t g = 110 + (uint8_t)random(60); - tft.fillCircle(dx0, dy0, r, C(g, g - 20, g - 40)); + GFXP->fillCircle(dx0, dy0, r, C(g, g - 20, g - 40)); } // skid-like streaks when launching hard if (speedPct < 0.4f) { for (int i = 0; i < 4; i++) { int side = (i < 2) ? -1 : 1; int x0 = iround(cx) + side * 16; - tft.drawFastVLine(x0, baseY - 18, 12, C(150, 140, 120)); + GFXP->drawFastVLine(x0, baseY - 18, 12, C(150, 140, 120)); } } } @@ -524,7 +568,7 @@ static void drawPlayerCar() { // contact shadow -> plants the car on the road for (int dy = 0; dy < 5; dy++) { int wdt = CAR_W - 6 - dy * 7; - if (wdt > 0) tft.drawFastHLine(scrW / 2 - wdt / 2, baseY + dy, wdt, C(30, 30, 26)); + if (wdt > 0) GFXP->drawFastHLine(scrW / 2 - wdt / 2, baseY + dy, wdt, C(30, 30, 26)); } float ta = fabsf(steerInput); int view = PV_REAR; @@ -556,7 +600,7 @@ static void drawCrashCar() { for (int i = 0; i < 5; i++) { int dx0 = iround(cx) - crashDir * (28 + (int)random(26)); int dy0 = baseY - (int)random(10); - tft.fillCircle(dx0, dy0, 1 + (int)random(2), C(150, 140, 120)); + GFXP->fillCircle(dx0, dy0, 1 + (int)random(2), C(150, 140, 120)); } drawSprite(id, w, cx, baseY, scrH, crashDir > 0); } @@ -565,18 +609,18 @@ static void drawCrashCar() { // HUD // --------------------------------------------------------------------------- static void drawHUD() { - tft.setTextDatum(TL_DATUM); - tft.setTextFont(2); - tft.setTextColor(TFT_WHITE, C(15,15,18)); + GFXP->setTextDatum(TL_DATUM); + GFXP->setTextFont(2); + GFXP->setTextColor(TFT_WHITE, C(15,15,18)); char buf[48]; snprintf(buf, sizeof(buf), "TIME %d", (int)ceilf(timeLeft)); - tft.drawString(buf, 6, 4); + GFXP->drawString(buf, 6, 4); snprintf(buf, sizeof(buf), "LAP %d/%d", lap + 1 < 1 ? 1 : lap + 1, LAPS); - tft.drawString(buf, 6, 22); - tft.setTextDatum(TR_DATUM); + GFXP->drawString(buf, 6, 22); + GFXP->setTextDatum(TR_DATUM); snprintf(buf, sizeof(buf), "%d km/h", (int)(speed_ / 100.0f)); - tft.drawString(buf, scrW - 6, 4); - tft.setTextDatum(TL_DATUM); + GFXP->drawString(buf, scrW - 6, 4); + GFXP->setTextDatum(TL_DATUM); } // --------------------------------------------------------------------------- @@ -736,28 +780,28 @@ static void drawTitle() { renderRoad(); renderSprites(); - tft.setTextDatum(MC_DATUM); - tft.setFreeFont(&FreeSansBold24pt7b); - tft.setTextColor(TFT_YELLOW, C(15,15,18)); - tft.drawString("MIATADRIVE", scrW / 2, 14); - tft.setFreeFont(nullptr); - tft.setTextFont(2); - tft.setTextColor(TFT_WHITE, C(15,15,18)); - tft.drawString("W gas S brake A/D steer", scrW / 2, 60); - tft.drawString("P auto T sprites Press/tap: race", scrW / 2, 78); + GFXP->setTextDatum(MC_DATUM); + GFXP->setFreeFont(&FreeSansBold24pt7b); + GFXP->setTextColor(TFT_YELLOW, C(15,15,18)); + GFXP->drawString("MIATADRIVE", scrW / 2, 14); + GFXP->setFreeFont(nullptr); + GFXP->setTextFont(2); + GFXP->setTextColor(TFT_WHITE, C(15,15,18)); + GFXP->drawString("W gas S brake A/D steer", scrW / 2, 60); + GFXP->drawString("P auto T sprites Press/tap: race", scrW / 2, 78); // car color picker: 1 Red 2 Blue 3 White 4 Yellow static const char* names[4] = { "1 Red", "2 Blue", "3 White", "4 Yellow" }; uint16_t pal[4] = { C(220,40,40), C(60,90,220), C(235,235,235), C(240,200,40) }; for (int i = 0; i < 4; i++) { - tft.setTextColor(i == playerColor ? pal[i] : C(200,200,200), C(15,15,18)); - tft.setTextDatum(TC_DATUM); - tft.drawString(names[i], 46 + i * 76, 102); + GFXP->setTextColor(i == playerColor ? pal[i] : C(200,200,200), C(15,15,18)); + GFXP->setTextDatum(TC_DATUM); + GFXP->drawString(names[i], 46 + i * 76, 102); if (i == playerColor) { - tft.setTextColor(TFT_WHITE, C(15,15,18)); - tft.drawString("^", 46 + i * 76, 118); + GFXP->setTextColor(TFT_WHITE, C(15,15,18)); + GFXP->drawString("^", 46 + i * 76, 118); } } - tft.setTextDatum(TL_DATUM); + GFXP->setTextDatum(TL_DATUM); // showcase the selected car, parked on the road below the arch drawSprite(playerSprite(PV_REAR), 110, scrW / 2, scrH - 4, scrH); } @@ -771,7 +815,7 @@ static void drawSpriteTest() { for (int by = 0; by < scrH; by += 8) for (int bx = 0; bx < scrW; bx += 8) { uint16_t c = (((bx / 8) + (by / 8)) & 1) ? C(70,70,74) : C(135,135,138); - tft.fillRect(bx, by, 8, 8, c); + GFXP->fillRect(bx, by, 8, 8, c); } // build list of sprite ids for this page @@ -794,48 +838,64 @@ static void drawSpriteTest() { for (int i = 0; i < nids; i++) { int col = i % cols, row = i / cols; int cx0 = col * CELL_W + 8, cy0 = row * CELL_H + 24; - tft.drawRect(cx0, cy0, CELL_W, CELL_H, C(20,20,24)); + GFXP->drawRect(cx0, cy0, CELL_W, CELL_H, C(20,20,24)); const GameSprite &sp = SPRITES[ids[i]]; int destW = sp.w <= CELL_W - 8 ? sp.w : CELL_W - 8; if (destW * sp.h / sp.w > CELL_H - 16) destW = (CELL_H - 16) * sp.w / sp.h; drawSprite(ids[i], destW, cx0 + CELL_W / 2, cy0 + CELL_H - 8, scrH); - tft.fillCircle(cx0 + CELL_W / 2, cy0 + CELL_H - 8, 1, TFT_MAGENTA); + GFXP->fillCircle(cx0 + CELL_W / 2, cy0 + CELL_H - 8, 1, TFT_MAGENTA); char buf[24]; snprintf(buf, sizeof(buf), "%d %dx%d", ids[i], sp.w, sp.h); - tft.setTextDatum(TL_DATUM); - tft.setTextFont(1); - tft.setTextColor(TFT_YELLOW, C(20,20,24)); - tft.drawString(buf, cx0 + 2, cy0 + 2); + GFXP->setTextDatum(TL_DATUM); + GFXP->setTextFont(1); + GFXP->setTextColor(TFT_YELLOW, C(20,20,24)); + GFXP->drawString(buf, cx0 + 2, cy0 + 2); } - tft.setTextFont(2); - tft.setTextColor(TFT_WHITE, C(20,20,24)); - tft.setTextDatum(TC_DATUM); + GFXP->setTextFont(2); + GFXP->setTextColor(TFT_WHITE, C(20,20,24)); + GFXP->setTextDatum(TC_DATUM); char title[48]; const char* names[] = { "Car 1", "Car 2", "Car 3", "Car 4", "Traffic cars", "Props" }; snprintf(title, sizeof(title), "Page %d/6: %s (A/D flip, T back)", spritePage + 1, names[spritePage]); - tft.drawString(title, scrW / 2, 0); - tft.setTextDatum(TL_DATUM); + GFXP->drawString(title, scrW / 2, 0); + GFXP->setTextDatum(TL_DATUM); } static void drawEndScreen(bool win) { - tft.fillScreen(C(10,10,14)); - tft.setTextDatum(MC_DATUM); - tft.setFreeFont(&FreeSansBold24pt7b); - tft.setTextColor(win ? TFT_GREEN : TFT_RED, C(10,10,14)); - tft.drawString(win ? "FINISH!" : "GAME OVER", scrW / 2, 80); - tft.setFreeFont(nullptr); - tft.setTextFont(2); - tft.setTextColor(TFT_WHITE, C(10,10,14)); + GFXP->fillRect(0, 0, scrW, scrH, C(10,10,14)); + GFXP->setTextDatum(MC_DATUM); + GFXP->setFreeFont(&FreeSansBold24pt7b); + GFXP->setTextColor(win ? TFT_GREEN : TFT_RED, C(10,10,14)); + GFXP->drawString(win ? "FINISH!" : "GAME OVER", scrW / 2, 80); + GFXP->setFreeFont(nullptr); + GFXP->setTextFont(2); + GFXP->setTextColor(TFT_WHITE, C(10,10,14)); char buf[48]; snprintf(buf, sizeof(buf), "Score %d", score); - tft.drawString(buf, scrW / 2, 140); - tft.setTextColor(TFT_GOLD, C(10,10,14)); - tft.drawString("Press to restart", scrW / 2, 190); - tft.setTextDatum(TL_DATUM); + GFXP->drawString(buf, scrW / 2, 140); + GFXP->setTextColor(TFT_GOLD, C(10,10,14)); + GFXP->drawString("Press to restart", scrW / 2, 190); + GFXP->setTextDatum(TL_DATUM); } -static void render() { +// --------------------------------------------------------------------------- +// Frame present +// --------------------------------------------------------------------------- +#ifndef EMULATOR +// Push the finished frame to the panel as one continuous SPI stream. The +// 320x240x16 buffer lives in PSRAM; the push is what used to be thousands of +// scattered draw calls racing the panel's scan-out (the flicker). +static void present() { + if (!fb.created()) return; tft.startWrite(); + fb.pushSprite(0, 0); + tft.endWrite(); +} +#else +static void present() {} // shim draws straight into the SDL framebuffer +#endif + +static void render() { switch (gameState) { case ST_TITLE: drawTitle(); break; case ST_SPRITES: drawSpriteTest(); break; @@ -848,7 +908,7 @@ static void render() { drawHUD(); break; } - tft.endWrite(); + present(); } // --------------------------------------------------------------------------- @@ -920,6 +980,11 @@ void setup() { hardwareInit(); scrW = tft.width(); scrH = tft.height(); +#ifndef EMULATOR + fb.setColorDepth(16); + if (!fb.createSprite(scrW, scrH)) + Serial.println("MiataDrive: framebuffer alloc failed (falling back to blank screen)"); +#endif buildPalette(); buildTrack(); if (!sound.begin()) Serial.println("Sound engine init failed"); diff --git a/examples/MiataDrive/README.md b/examples/MiataDrive/README.md index d7c3ce3..64b04cf 100644 --- a/examples/MiataDrive/README.md +++ b/examples/MiataDrive/README.md @@ -57,6 +57,11 @@ graphics. corners with their turning sprite, mirrored on right-hand bends. - **Scoring / laps**: 3 laps, 75 s + 45 s per lap, −300 for a crash. - **Sound**: engine + event blips via `sound_engine.*` (I2S on device). +- **Rendering**: on hardware the whole frame is composed in a PSRAM-backed + `TFT_eSprite` framebuffer and pushed to the panel in one continuous SPI + stream at 80 MHz — no half-drawn frames (flicker) and a stable ~30 fps. + If your panel shows artifacts at 80 MHz, drop `SPI_FREQUENCY` to 40000000 + in `platformio.ini`. All graphics — including the title screen, which renders the start line under the arch with your selected car parked on the road — come from the diff --git a/lib/TFT_eSPI/User_Setups/Setup210_LilyGo_T_Deck.h b/lib/TFT_eSPI/User_Setups/Setup210_LilyGo_T_Deck.h index 8705497..df9f570 100644 --- a/lib/TFT_eSPI/User_Setups/Setup210_LilyGo_T_Deck.h +++ b/lib/TFT_eSPI/User_Setups/Setup210_LilyGo_T_Deck.h @@ -44,7 +44,9 @@ // #define SPI_FREQUENCY 27000000 +#ifndef SPI_FREQUENCY // can be overridden from the PlatformIO build flags #define SPI_FREQUENCY 40000000 +#endif #define SPI_READ_FREQUENCY 20000000 diff --git a/platformio.ini b/platformio.ini index c4e1fb8..5ebb3e8 100644 --- a/platformio.ini +++ b/platformio.ini @@ -67,6 +67,10 @@ build_flags = -DDISABLE_ALL_LIBRARY_WARNINGS + ; MiataDrive: full-frame push needs headroom — 80 MHz halves the frame + ; transfer vs the stock 40 MHz. If your panel shows artifacts, drop to 40000000. + -DSPI_FREQUENCY=80000000 + -DRADIOLIB_EXCLUDE_CC1101 -DRADIOLIB_EXCLUDE_NRF24 -DRADIOLIB_EXCLUDE_RF69