mirror of
https://github.com/italicsjenga/agb.git
synced 2024-12-23 08:11:33 +11:00
save to separate areas
This commit is contained in:
parent
418f792ef2
commit
dc44d20627
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue