CHGame 0.3.0
Libraries for the CHGame handheld
Loading...
Searching...
No Matches

save::: a game's options and progress in flash, with a CRC, kept across power cycles and re-uploads. More...

Namespaces

namespace  save
 Saved records: load, store, and the raw read/write underneath.
 

Detailed Description

save::: a game's options and progress in flash, with a CRC, kept across power cycles and re-uploads.

struct SaveData { Options opt; Stats stats; }; // up to 244 bytes
const uint32_t MAGIC = save::magic("CHXX");
SaveData d;
if (save::load(MAGIC, 1, d)) { ... } // at start-up
save::store(MAGIC, 1, d); // after gfx_wait()
bool store(uint32_t magic, uint8_t version, const T &data, uint8_t flag=0)
Save a struct as a new record.
Definition Save.h:102
constexpr uint32_t magic(const char(&s)[5])
Four characters as a magic number ("CHCR" -> 0x52434843).
Definition Save.h:52
bool load(uint32_t magic, uint8_t version, T &data, uint8_t *flag=nullptr)
Load a struct from the newest valid record.
Definition Save.h:89

Change the version when SaveData changes shape: old records are then ignored instead of misread. A flag byte travels in the header (most of the casino games mark "a game in progress" with it).

The page is built in CHGfx's chunk scratch, so store() must run between gfx_wait() and the next flush. read() points straight into flash: a game can take just the part it needs.

Note
Load once at start-up, before the first store: the read remembers the newest record's sequence number, and the next save follows it.