Boot: input grace period; persist high score + volume via NVS

- Ignore all input for ~1.2 s after power-up/reset (INPUT_GRACE_MS):
  the keyboard controller can emit stale bytes and pins can float,
  which used to start a game right after a restart.
- High score + volume survive power cycles: Preferences (NVS, 'mc'
  namespace) loaded in setup(), saved on game over and on every
  volume key (U/V/M). arduino-esp32 commits NVS in end(), so
  savePrefs() does putUInt x2 + end() + begin() reopen.
- SoundEngine::setVolume() restores a saved level (0 = stay muted,
  lastVol_ keeps unmute working).
- Emulator: file-backed Preferences shim (mc-prefs.cfg, gitignored)
  so persistence is testable on the desktop.
This commit is contained in:
Dejvino
2026-11-11 07:05:59 +01:00
parent a5921032d7
commit 798f8600a0
5 changed files with 106 additions and 2 deletions
+38 -2
View File
@@ -21,6 +21,7 @@
#include <SPI.h>
#include <TFT_eSPI.h>
#include <Wire.h>
#include <Preferences.h>
#include "utilities.h"
#include "TouchDrvGT911.hpp"
#include "sound_engine.h"
@@ -53,6 +54,10 @@ static const float ROCKET_SPEED = 6.0f; // px / frame
static const float ROCKET_RADIUS = 24.0f;
static const uint32_t BANNER_MS = 1500;
static const uint32_t BONUS_MS = 3400;
// Ignore all input during the first moments after power-up/reset: the
// keyboard controller may still emit stale bytes and pins may float, which
// used to start a game right after a restart.
static const uint32_t INPUT_GRACE_MS = 1200;
static const float ROCKET_SPEED_SIDE = 3.4f; // side batteries fire slow rockets;
// only the center battery is fast
static const int MAX_CRAFTS = 2; // bombers + satellites
@@ -183,6 +188,19 @@ static void sfxNoise(uint16_t d, uint16_t a) { sfx(SND_NOISE, 0, 0,
static void sfxDeny() { sfxTone(220, 90, 8000); }
// ---------------------------------------------------------------------------
// Persistent settings (NVS): high score + volume survive power cycles
// ---------------------------------------------------------------------------
static Preferences prefs;
static void savePrefs()
{
prefs.putUInt("hi", (uint32_t)highScore);
prefs.putUInt("vol", (uint32_t)sound.volume());
prefs.end(); // commit to NVS
prefs.begin("mc", false); // reopen for the next save
}
// Attack alarm, played once as a wave-start intro (like the original):
// four falling tones, then gameplay begins and the alarm is done.
static void sfxSirenIntro()
@@ -1178,6 +1196,7 @@ static void enterGameOver()
{
sfxGameOver();
if (score > highScore) highScore = score;
savePrefs(); // the run is final: commit high score (+ current volume)
tft.fillScreen(TFT_BLACK);
stars(40);
@@ -1364,20 +1383,23 @@ static void pollKeyboard()
char c = tolower(k);
// volume keys work in every state
// volume keys work in every state (and persist immediately)
if (c == 'u') {
sound.volumeUp();
volOSDUntil = millis() + 1200;
savePrefs();
return;
}
if (c == 'v') {
sound.volumeDown();
volOSDUntil = millis() + 1200;
savePrefs();
return;
}
if (c == 'm') {
sound.toggleMute();
volOSDUntil = millis() + 1200;
savePrefs();
return;
}
@@ -1553,15 +1575,29 @@ void setup()
}
Serial.printf("Screen %dx%d, ground at %d\n", scrW, scrH, groundY);
// restore persisted high score and volume
prefs.begin("mc", false);
highScore = (int)prefs.getUInt("hi", 0);
sound.setVolume((uint8_t)prefs.getUInt("vol", 5));
drawTitle();
}
void loop()
{
// swallow all input right after power-up/reset: the keyboard controller
// may still emit stale bytes and pins may float, which used to start
// a game immediately after a restart
uint32_t now = millis();
if (now < INPUT_GRACE_MS) {
delay(1);
return;
}
pollTrackball(); // hot path: sample the ball every iteration (~1 ms)
static uint32_t nextFrame = 0;
uint32_t now = millis();
if ((int32_t)(now - nextFrame) < 0) {
delay(1);
return;