diff --git a/.gitignore b/.gitignore index ecd3fbe..49783fb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ config emulator/missilecommand-emu emulator/seq*.bmp +emulator/mc-prefs.cfg diff --git a/emulator/src/shim/Preferences.h b/emulator/src/shim/Preferences.h new file mode 100644 index 0000000..995d05e --- /dev/null +++ b/emulator/src/shim/Preferences.h @@ -0,0 +1,57 @@ +/** + * Preferences shim: backs the ESP32 NVS Preferences API with a plain text + * file next to the emulator binary ("mc-prefs.cfg"), so the high score and + * volume survive emulator restarts just like on the device. + */ +#pragma once +#include +#include +#include +#include + +class Preferences { +public: + bool begin(const char *ns, bool readOnly = false) + { + (void)ns; + (void)readOnly; + map_.clear(); + if (FILE *f = fopen(path_, "r")) { + char key[64]; + unsigned val; + while (fscanf(f, "%63s %u", key, &val) == 2) { + map_[key] = val; + } + fclose(f); + } + return true; + } + + void end() {} // file-backed: every putUInt already flushes + + uint32_t getUInt(const char *key, uint32_t defaultValue = 0) + { + auto it = map_.find(key); + return it == map_.end() ? defaultValue : it->second; + } + + void putUInt(const char *key, uint32_t value) + { + map_[key] = value; + flush(); + } + +private: + void flush() + { + if (FILE *f = fopen(path_, "w")) { + for (const auto &kv : map_) { + fprintf(f, "%s %u\n", kv.first.c_str(), kv.second); + } + fclose(f); + } + } + + std::unordered_map map_; + const char *path_ = "mc-prefs.cfg"; +}; diff --git a/examples/MissileCommand/MissileCommand.ino b/examples/MissileCommand/MissileCommand.ino index 3edc36b..23e122a 100644 --- a/examples/MissileCommand/MissileCommand.ino +++ b/examples/MissileCommand/MissileCommand.ino @@ -21,6 +21,7 @@ #include #include #include +#include #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; diff --git a/examples/MissileCommand/sound_engine.cpp b/examples/MissileCommand/sound_engine.cpp index 86b896f..daf1ac9 100644 --- a/examples/MissileCommand/sound_engine.cpp +++ b/examples/MissileCommand/sound_engine.cpp @@ -103,6 +103,15 @@ void SoundEngine::toggleMute() } } +// Restore a saved volume level (from NVS at boot); 0 keeps the device muted +// while lastVol_ stays at its default so unmute still works. +void SoundEngine::setVolume(uint8_t v) +{ + if (v > VOL_MAX) v = VOL_MAX; + vol_ = v; + if (vol_ > 0) lastVol_ = vol_; +} + // scaled peak amplitude for the current volume level static inline uint32_t scaledAmp(uint16_t amp, uint8_t vol) { diff --git a/examples/MissileCommand/sound_engine.h b/examples/MissileCommand/sound_engine.h index 6afa9ae..96854d4 100644 --- a/examples/MissileCommand/sound_engine.h +++ b/examples/MissileCommand/sound_engine.h @@ -39,6 +39,7 @@ public: void volumeUp(); void volumeDown(); void toggleMute(); + void setVolume(uint8_t v); // restore a saved level (0 = muted) uint8_t volume() const { return vol_; } private: