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:
@@ -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 <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
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<std::string, uint32_t> map_;
|
||||
const char *path_ = "mc-prefs.cfg";
|
||||
};
|
||||
Reference in New Issue
Block a user