Add Missile Command game for T-Deck + desktop emulator
Game (examples/MissileCommand): - Classic Missile Command: 6 cities, 3 batteries x 10 missiles, escalating waves, smart bombs, score multiplier, session high score - Trackball aims, press/tap fires, keyboard A/S/D selects battery, screen tap aims+fires - Rendering: incremental TFT_eSPI drawing (persistent enemy trails that vanish on death, no AA-rocket trails, explosion circles that repair terrain, ghost-free crosshair) - Sound (sound_engine.cpp): non-blocking I2S task on core 0; wave-start alarm of four falling tones (like the original), sweeps/noise effects, U/V/M keyboard volume control with OSD, master volume scaling Emulator (emulator/): - SDL2 host that compiles the unmodified sketch against shim headers (Arduino, TFT_eSPI->framebuffer, GT911 touch, I2C keyboard, I2S->SDL audio); arrow keys emulate the trackball, --autoplay/--frames/--dump flags for headless smoke tests
This commit is contained in:
@@ -0,0 +1,215 @@
|
||||
/**
|
||||
* SDL2 host for the MissileCommand sketch — run the game on your PC.
|
||||
*
|
||||
* Input mapping:
|
||||
* Arrow keys trackball roll (hold to keep rolling)
|
||||
* Enter / right-click trackball press (fire)
|
||||
* Left-click screen tap (aim + fire)
|
||||
* A S D fire from left / center / right battery
|
||||
* Space fire from nearest battery
|
||||
* U / V volume up / down
|
||||
* M mute toggle
|
||||
*/
|
||||
#include <SDL.h>
|
||||
#include <SDL_ttf.h>
|
||||
#include <cstdio>
|
||||
#include <cctype>
|
||||
#include <cstring>
|
||||
#include "emu_gfx.h"
|
||||
#include "emu_input.h"
|
||||
|
||||
// pull in the whole game as a single translation unit
|
||||
#include "MissileCommand.ino"
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Global instances declared extern in the shims
|
||||
// ---------------------------------------------------------------------------
|
||||
namespace emu {
|
||||
uint8_t pinLevels[64];
|
||||
bool touchDown = false;
|
||||
int touchX = 0, touchY = 0;
|
||||
std::deque<uint8_t> keyboard;
|
||||
bool quitRequested = false;
|
||||
} // namespace emu
|
||||
|
||||
namespace emuGfx {
|
||||
uint32_t fb[W * H];
|
||||
SDL_Texture *tex = nullptr;
|
||||
bool dirty = true;
|
||||
} // namespace emuGfx
|
||||
|
||||
SerialSim Serial;
|
||||
TwoWire Wire;
|
||||
SPIClass SPI;
|
||||
|
||||
const FreeFontStub FreeSansBold24pt7b {0};
|
||||
const FreeFontStub FreeSansBold18pt7b {0};
|
||||
const FreeFontStub FreeSansOblique12pt7b {0};
|
||||
const FreeFontStub FreeSans12pt7b {0};
|
||||
|
||||
static void saveBmp(const char *path)
|
||||
{
|
||||
SDL_Surface *s = SDL_CreateRGBSurfaceWithFormatFrom(
|
||||
emuGfx::fb, emuGfx::W, emuGfx::H, 32, emuGfx::W * 4, SDL_PIXELFORMAT_ABGR8888);
|
||||
if (s) {
|
||||
SDL_SaveBMP(s, path);
|
||||
SDL_FreeSurface(s);
|
||||
std::printf("[emu] frame dumped to %s\n", path);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
long maxFrames = -1;
|
||||
const char *dumpPath = nullptr;
|
||||
bool autoplay = false;
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (!strcmp(argv[i], "--frames") && i + 1 < argc) maxFrames = atol(argv[++i]);
|
||||
else if (!strcmp(argv[i], "--dump") && i + 1 < argc) dumpPath = argv[++i];
|
||||
else if (!strcmp(argv[i], "--autoplay")) autoplay = true;
|
||||
}
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0) {
|
||||
std::printf("SDL_Init failed: %s\n", SDL_GetError());
|
||||
return 1;
|
||||
}
|
||||
TTF_Init();
|
||||
|
||||
SDL_Window *win = SDL_CreateWindow("Missile Command — T-Deck emulator",
|
||||
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
|
||||
640, 480, SDL_WINDOW_RESIZABLE);
|
||||
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (!ren) ren = SDL_CreateRenderer(win, -1, 0); // software fallback (headless)
|
||||
SDL_RenderSetLogicalSize(ren, emuGfx::W, emuGfx::H);
|
||||
emuGfx::tex = SDL_CreateTexture(ren, SDL_PIXELFORMAT_ABGR8888,
|
||||
SDL_TEXTUREACCESS_STREAMING, emuGfx::W, emuGfx::H);
|
||||
|
||||
for (auto &p : emu::pinLevels) p = 1; // everything pulled up
|
||||
|
||||
setup();
|
||||
|
||||
const uint8_t dirPin[4] = { BOARD_TBOX_G02, BOARD_TBOX_G01, BOARD_TBOX_G04, BOARD_TBOX_G03 };
|
||||
bool held[4] = { false, false, false, false };
|
||||
uint32_t frame = 0;
|
||||
|
||||
bool running = true;
|
||||
while (running && !emu::quitRequested) {
|
||||
SDL_Event e;
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_QUIT:
|
||||
running = false;
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN: {
|
||||
if (e.key.repeat) break;
|
||||
SDL_Keycode k = e.key.keysym.sym;
|
||||
switch (k) {
|
||||
case SDLK_RIGHT: held[0] = true; emu::pinLevels[dirPin[0]] = 0; break;
|
||||
case SDLK_UP: held[1] = true; emu::pinLevels[dirPin[1]] = 0; break;
|
||||
case SDLK_LEFT: held[2] = true; emu::pinLevels[dirPin[2]] = 0; break;
|
||||
case SDLK_DOWN: held[3] = true; emu::pinLevels[dirPin[3]] = 0; break;
|
||||
case SDLK_RETURN:
|
||||
case SDLK_KP_ENTER:
|
||||
emu::pinLevels[BOARD_BOOT_PIN] = 0; // trackball press
|
||||
break;
|
||||
case SDLK_SPACE:
|
||||
emu::keyboard.push_back(' ');
|
||||
break;
|
||||
case SDLK_ESCAPE:
|
||||
running = false;
|
||||
break;
|
||||
default: {
|
||||
const char *name = SDL_GetKeyName(k);
|
||||
if (name && strlen(name) == 1) {
|
||||
emu::keyboard.push_back((uint8_t)tolower((unsigned char)name[0]));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_KEYUP: {
|
||||
SDL_Keycode k = e.key.keysym.sym;
|
||||
switch (k) {
|
||||
case SDLK_RIGHT: held[0] = false; emu::pinLevels[dirPin[0]] = 1; break;
|
||||
case SDLK_UP: held[1] = false; emu::pinLevels[dirPin[1]] = 1; break;
|
||||
case SDLK_LEFT: held[2] = false; emu::pinLevels[dirPin[2]] = 1; break;
|
||||
case SDLK_DOWN: held[3] = false; emu::pinLevels[dirPin[3]] = 1; break;
|
||||
case SDLK_RETURN:
|
||||
case SDLK_KP_ENTER:
|
||||
emu::pinLevels[BOARD_BOOT_PIN] = 1;
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
case SDL_MOUSEBUTTONUP: {
|
||||
int ww, wh;
|
||||
SDL_GetWindowSize(win, &ww, &wh);
|
||||
int wx = (int)((int64_t)e.button.x * emuGfx::W / (ww > 0 ? ww : 1));
|
||||
int wy = (int)((int64_t)e.button.y * emuGfx::H / (wh > 0 ? wh : 1));
|
||||
if (wx < 0) wx = 0;
|
||||
if (wx > emuGfx::W - 1) wx = emuGfx::W - 1;
|
||||
if (wy < 0) wy = 0;
|
||||
if (wy > emuGfx::H - 1) wy = emuGfx::H - 1;
|
||||
|
||||
if (e.button.button == SDL_BUTTON_LEFT) {
|
||||
bool down = (e.type == SDL_MOUSEBUTTONDOWN);
|
||||
emu::touchDown = down;
|
||||
emu::touchX = wx;
|
||||
emu::touchY = wy;
|
||||
} else if (e.button.button == SDL_BUTTON_RIGHT) {
|
||||
emu::pinLevels[BOARD_BOOT_PIN] =
|
||||
(e.type == SDL_MOUSEBUTTONDOWN) ? 0 : 1;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Simulate a rolling trackball: toggle the held quadrant pin so the
|
||||
// game keeps seeing edges while the arrow key is down.
|
||||
if (++frame % 3 == 0) {
|
||||
for (int d = 0; d < 4; d++) {
|
||||
if (held[d]) emu::pinLevels[dirPin[d]] ^= 1;
|
||||
}
|
||||
}
|
||||
|
||||
// Headless test mode: start the game, wander the crosshair, keep firing.
|
||||
if (autoplay) {
|
||||
static bool started = false;
|
||||
if (!started && frame >= 5) {
|
||||
emu::keyboard.push_back('a');
|
||||
started = true;
|
||||
}
|
||||
static int dir = 0;
|
||||
if (frame % 45 == 0) dir = (dir + 1) & 3;
|
||||
if (frame % 4 == 0) emu::pinLevels[dirPin[dir]] ^= 1;
|
||||
if (frame % 75 == 0) emu::keyboard.push_back('s');
|
||||
}
|
||||
|
||||
loop();
|
||||
|
||||
if (emuGfx::dirty) {
|
||||
SDL_UpdateTexture(emuGfx::tex, nullptr, emuGfx::fb,
|
||||
emuGfx::W * sizeof(uint32_t));
|
||||
emuGfx::dirty = false;
|
||||
}
|
||||
SDL_RenderClear(ren);
|
||||
SDL_RenderCopy(ren, emuGfx::tex, nullptr, nullptr);
|
||||
SDL_RenderPresent(ren);
|
||||
|
||||
if (maxFrames > 0 && --maxFrames == 0) {
|
||||
running = false;
|
||||
if (dumpPath) saveBmp(dumpPath);
|
||||
}
|
||||
}
|
||||
|
||||
TTF_Quit();
|
||||
SDL_Quit();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user