/** * TFT_eSPI shim: renders the same drawing calls into a 320x240 ABGR8888 * framebuffer that the SDL3 host pushes to a window. Text uses SDL3_ttf with * DejaVu fonts as stand-ins for the TFT_eSPI font set. */ #pragma once #include #include #if __has_include() // upstream install layout #include #else // Debian/Ubuntu package layout #include #endif #include "emu_gfx.h" #include // ---- TFT_eSPI RGB565 color constants (the ones the game uses) ---- #define TFT_BLACK 0x0000 #define TFT_NAVY 0x000F #define TFT_DARKGREEN 0x0320 #define TFT_DARKGREY 0x7BEF #define TFT_BLUE 0x001F #define TFT_GREEN 0x07E0 #define TFT_CYAN 0x07FF #define TFT_RED 0xF800 #define TFT_MAGENTA 0xF81F #define TFT_YELLOW 0xFFE0 #define TFT_ORANGE 0xFDA0 #define TFT_GREENYELLOW 0xAFE5 #define TFT_GOLD 0xFEA0 #define TFT_WHITE 0xFFFF #define TFT_SKYBLUE 0x867D enum TextDatum { TL_DATUM = 0, TC_DATUM = 1, TR_DATUM = 2, ML_DATUM = 3, MC_DATUM = 4, MR_DATUM = 5, BL_DATUM = 6, BC_DATUM = 7, BR_DATUM = 8 }; // The .ino only needs the address of these font objects. struct FreeFontStub { int _; }; extern const FreeFontStub FreeSansBold24pt7b; extern const FreeFontStub FreeSansBold18pt7b; extern const FreeFontStub FreeSansOblique12pt7b; extern const FreeFontStub FreeSans12pt7b; static inline uint32_t rgb565to888(uint16_t c) { uint32_t r = ((c >> 11) & 0x1F) * 255 / 31; uint32_t g = ((c >> 5) & 0x3F) * 255 / 63; uint32_t b = (c & 0x1F) * 255 / 31; return 0xFF000000u | (b << 16) | (g << 8) | r; } class TFT_eSPI { public: TFT_eSPI() {} void begin() {} void startWrite() {} void endWrite() {} void setRotation(uint8_t) {} int width() { return emuGfx::W; } int height() { return emuGfx::H; } void fillScreen(uint32_t c) { fillRect(0, 0, emuGfx::W, emuGfx::H, c); } void drawPixel(int32_t x, int32_t y, uint32_t c) { if (x < 0 || y < 0 || x >= emuGfx::W || y >= emuGfx::H) return; emuGfx::fb[y * emuGfx::W + x] = rgb565to888((uint16_t)c); emuGfx::dirty = true; } void fillRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t c) { uint32_t px = rgb565to888((uint16_t)c); for (int32_t j = y; j < y + h; j++) { if (j < 0 || j >= emuGfx::H) continue; for (int32_t i = x; i < x + w; i++) { if (i < 0 || i >= emuGfx::W) continue; emuGfx::fb[j * emuGfx::W + i] = px; } } emuGfx::dirty = true; } void drawRect(int32_t x, int32_t y, int32_t w, int32_t h, uint32_t c) { drawFastHLine(x, y, w, c); drawFastHLine(x, y + h - 1, w, c); drawFastVLine(x, y, h, c); drawFastVLine(x + w - 1, y, h, c); } void drawFastHLine(int32_t x, int32_t y, int32_t w, uint32_t c) { fillRect(x, y, w, 1, c); } void drawFastVLine(int32_t x, int32_t y, int32_t h, uint32_t c) { fillRect(x, y, 1, h, c); } void drawLine(int32_t x0, int32_t y0, int32_t x1, int32_t y1, uint32_t c) { int dx = std::abs(x1 - x0), sx = x0 < x1 ? 1 : -1; int dy = -std::abs(y1 - y0), sy = y0 < y1 ? 1 : -1; int err = dx + dy; for (;;) { drawPixel(x0, y0, c); if (x0 == x1 && y0 == y1) break; int e2 = 2 * err; if (e2 >= dy) { err += dy; x0 += sx; } if (e2 <= dx) { err += dx; y0 += sy; } } } // Blit a 16bpp (RGB565) image — the pattern-stamp primitive that the // device-side TFT_eSPI / TFT_eSprite pushImage() provides. Clipped. void pushImage(int32_t x, int32_t y, int32_t w, int32_t h, const uint16_t *data) { for (int32_t j = 0; j < h; j++) { int32_t ty = y + j; if (ty < 0 || ty >= emuGfx::H) continue; for (int32_t i = 0; i < w; i++) { int32_t tx = x + i; if (tx < 0 || tx >= emuGfx::W) continue; emuGfx::fb[ty * emuGfx::W + tx] = rgb565to888(data[j * w + i]); } } emuGfx::dirty = true; } void fillCircle(int32_t x0, int32_t y0, int32_t r, uint32_t c) { for (int dy = -r; dy <= r; dy++) { int dx = (int)sqrtf((float)(r * r - dy * dy)); drawFastHLine(x0 - dx, y0 + dy, dx * 2 + 1, c); } } void drawCircle(int32_t x0, int32_t y0, int32_t r, uint32_t c) { for (int x = -r; x <= r; x++) { int y = (int)(sqrtf((float)(r * r - x * x)) + 0.5f); drawPixel(x0 + x, y0 + y, c); drawPixel(x0 + x, y0 - y, c); drawPixel(x0 + y, y0 + x, c); drawPixel(x0 - y, y0 + x, c); drawPixel(x0 + y, y0 - x, c); drawPixel(x0 - y, y0 - x, c); } } void fillTriangle(int32_t x0, int32_t y0, int32_t x1, int32_t y1, int32_t x2, int32_t y2, uint32_t c) { int32_t minx = std::min(std::min(x0, x1), x2); int32_t maxx = std::max(std::max(x0, x1), x2); int32_t miny = std::min(std::min(y0, y1), y2); int32_t maxy = std::max(std::max(y0, y1), y2); float d = (float)((y1 - y2) * (x0 - x2) + (x2 - x1) * (y0 - y2)); if (d == 0) return; for (int32_t y = miny; y <= maxy; y++) { for (int32_t x = minx; x <= maxx; x++) { float l0 = ((y1 - y2) * (x - x2) + (x2 - x1) * (y - y2)) / d; float l1 = ((y2 - y0) * (x - x2) + (x0 - x2) * (y - y2)) / d; float l2 = 1.0f - l0 - l1; if (l0 >= -0.001f && l1 >= -0.001f && l2 >= -0.001f) { drawPixel(x, y, c); } } } } void setTextDatum(uint8_t d) { datum = d; } void setTextFont(uint8_t f) { fontNum = f; freeFontActive = false; } void setFreeFont(const void *f) { fontPtr = f; freeFontActive = true; } void setTextColor(uint16_t fg) { this->fg = fg; bgOpaque = false; } void setTextColor(uint16_t fg, uint16_t bg) { this->fg = fg; this->bg = bg; bgOpaque = true; } void drawString(const String &s, int32_t x, int32_t y) { if (s.s.empty()) return; int size = freeFontActive ? freeFontSize(fontPtr) : (fontNum >= 2 ? 15 : 10); TTF_Font *f = getFont(size, freeFontActive); if (!f) return; SDL_Color col = sdlColor(fg); SDL_Surface *surf = TTF_RenderText_Blended(f, s.s.c_str(), s.s.size(), col); if (!surf) return; SDL_Surface *conv = SDL_ConvertSurface(surf, SDL_PIXELFORMAT_ABGR8888); SDL_DestroySurface(surf); if (!conv) return; int w = conv->w, h = conv->h; int dx = x, dy = y; switch (datum) { case TL_DATUM: dx = x; dy = y; break; case TC_DATUM: dx = x - w/2; dy = y; break; case TR_DATUM: dx = x - w; dy = y; break; case ML_DATUM: dx = x; dy = y - h/2; break; case MC_DATUM: dx = x - w/2; dy = y - h/2; break; case MR_DATUM: dx = x - w; dy = y - h/2; break; case BL_DATUM: dx = x; dy = y - h; break; case BC_DATUM: dx = x - w/2; dy = y - h; break; case BR_DATUM: dx = x - w; dy = y - h; break; default: break; } if (bgOpaque) fillRect(dx, dy, w, h, bg); uint32_t *px = (uint32_t *)conv->pixels; int pitch = conv->pitch / 4; for (int j = 0; j < h; j++) { int ty = dy + j; if (ty < 0 || ty >= emuGfx::H) continue; for (int i = 0; i < w; i++) { int tx = dx + i; if (tx < 0 || tx >= emuGfx::W) continue; uint32_t p = px[j * pitch + i]; if ((p >> 24) > 0) emuGfx::fb[ty * emuGfx::W + tx] = p; } } SDL_DestroySurface(conv); emuGfx::dirty = true; } private: static SDL_Color sdlColor(uint16_t c565) { uint32_t p = rgb565to888(c565); SDL_Color c; c.r = (uint8_t)(p & 0xFF); c.g = (uint8_t)((p >> 8) & 0xFF); c.b = (uint8_t)((p >> 16) & 0xFF); c.a = 255; return c; } static int freeFontSize(const void *f) { if (f == (const void *)&FreeSansBold18pt7b) return 24; if (f == (const void *)&FreeSansOblique12pt7b) return 16; if (f == (const void *)&FreeSans12pt7b) return 16; return 32; // 24pt and anything unknown } static TTF_Font *getFont(int size, bool bold) { static std::map, TTF_Font *> cache; auto key = std::make_pair(size, bold); auto it = cache.find(key); if (it != cache.end()) return it->second; const char *paths[] = { "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", "/usr/share/fonts/dejavu/DejaVuSans.ttf", "/usr/share/fonts/dejavu/DejaVuSans-Bold.ttf", }; const char *path = bold ? paths[1] : paths[0]; TTF_Font *f = TTF_OpenFont(path, (float)size); if (!f) { f = TTF_OpenFont(paths[2], size); if (!f) f = TTF_OpenFont(paths[3], size); } cache[key] = f; return f; } uint8_t datum = MC_DATUM; uint8_t fontNum = 1; const void *fontPtr = nullptr; bool freeFontActive = false; uint16_t fg = TFT_WHITE; uint16_t bg = TFT_BLACK; bool bgOpaque = false; };