agb/examples/hyperspace-roll/src/save.rs

43 lines
977 B
Rust
Raw Normal View History

use agb::Gba;
use agb::save::Error;
use agb::sync::Static;
2022-07-25 06:33:51 +10:00
static HIGHSCORE: Static<u32> = Static::new(0);
2022-07-25 06:33:51 +10:00
pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
gba.save.init_sram();
2022-07-25 06:33:51 +10:00
let mut access = gba.save.access()?;
2022-07-25 06:33:51 +10:00
let mut buffer = [0; 1];
access.read(0, &mut buffer)?;
2022-07-25 06:33:51 +10:00
if buffer[0] != 0 {
access.prepare_write(0..1)?.write(0, &[0])?;
core::mem::drop(access);
save_high_score(gba, 0)?;
} else {
let mut buffer = [0; 4];
access.read(1, &mut buffer)?;
let high_score = u32::from_le_bytes(buffer);
2022-07-25 06:33:51 +10:00
if high_score > 100 {
HIGHSCORE.write(0)
2022-07-25 06:33:51 +10:00
} else {
HIGHSCORE.write(high_score)
2022-07-25 06:33:51 +10:00
}
}
Ok(())
2022-07-25 06:33:51 +10:00
}
pub fn load_high_score() -> u32 {
HIGHSCORE.read()
2022-07-25 06:33:51 +10:00
}
pub fn save_high_score(gba: &mut Gba, score: u32) -> Result<(), Error> {
gba.save.access()?.prepare_write(1..5)?.write(1, &score.to_le_bytes())?;
HIGHSCORE.write(score);
Ok(())
2022-07-25 06:33:51 +10:00
}