save to separate areas

This commit is contained in:
Corwin 2024-04-20 16:18:42 +01:00
parent 418f792ef2
commit dc44d20627
No known key found for this signature in database
3 changed files with 12 additions and 8 deletions

View file

@ -3,6 +3,7 @@ use agb::save::{Error, SaveManager};
use agb::Gba;
static HIGH_SCORE: AtomicU32 = AtomicU32::new(0);
static SAVE_OFFSET: usize = 1;
pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
gba.save.init_sram();
@ -18,7 +19,7 @@ pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
save_high_score(&mut gba.save, 0)?;
} else {
let mut buffer = [0; 4];
access.read(1, &mut buffer)?;
access.read(SAVE_OFFSET, &mut buffer)?;
let high_score = u32::from_le_bytes(buffer);
let score = if high_score > 100 { 0 } else { high_score };
@ -35,8 +36,8 @@ pub fn load_high_score() -> u32 {
pub fn save_high_score(save: &mut SaveManager, score: u32) -> Result<(), Error> {
save.access()?
.prepare_write(1..5)?
.write(1, &score.to_le_bytes())?;
.prepare_write(SAVE_OFFSET..SAVE_OFFSET + 4)?
.write(SAVE_OFFSET, &score.to_le_bytes())?;
HIGH_SCORE.store(score, Ordering::SeqCst);
Ok(())
}

View file

@ -121,8 +121,10 @@ pub fn entry(mut gba: agb::Gba) -> ! {
oam: unmanaged,
};
let mut current_level = 0;
let mut maximum_level = save::load_max_level() as usize;
let saved_level = save::load_max_level() as usize;
let mut current_level = saved_level;
let mut maximum_level = saved_level;
loop {
if current_level >= level::Level::num_levels() {
current_level = 0;

View file

@ -5,6 +5,7 @@ use agb::{
};
static MAXIMUM_LEVEL: AtomicU32 = AtomicU32::new(0);
static SAVE_OFFSET: usize = 0xFF;
pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
gba.save.init_sram();
@ -20,7 +21,7 @@ pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
save_max_level(&mut gba.save, 0)?;
} else {
let mut buffer = [0; 4];
access.read(1, &mut buffer)?;
access.read(SAVE_OFFSET, &mut buffer)?;
let max_level = u32::from_le_bytes(buffer);
if max_level > 100 {
@ -39,8 +40,8 @@ pub fn load_max_level() -> u32 {
pub fn save_max_level(save: &mut SaveManager, level: u32) -> Result<(), Error> {
save.access()?
.prepare_write(1..5)?
.write(1, &level.to_le_bytes())?;
.prepare_write(SAVE_OFFSET..SAVE_OFFSET + 4)?
.write(SAVE_OFFSET, &level.to_le_bytes())?;
MAXIMUM_LEVEL.store(level, Ordering::SeqCst);
Ok(())
}