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;
|
use agb::Gba;
|
||||||
|
|
||||||
static HIGH_SCORE: AtomicU32 = AtomicU32::new(0);
|
static HIGH_SCORE: AtomicU32 = AtomicU32::new(0);
|
||||||
|
static SAVE_OFFSET: usize = 1;
|
||||||
|
|
||||||
pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
|
pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
|
||||||
gba.save.init_sram();
|
gba.save.init_sram();
|
||||||
|
@ -18,7 +19,7 @@ pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
|
||||||
save_high_score(&mut gba.save, 0)?;
|
save_high_score(&mut gba.save, 0)?;
|
||||||
} else {
|
} else {
|
||||||
let mut buffer = [0; 4];
|
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 high_score = u32::from_le_bytes(buffer);
|
||||||
|
|
||||||
let score = if high_score > 100 { 0 } else { high_score };
|
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> {
|
pub fn save_high_score(save: &mut SaveManager, score: u32) -> Result<(), Error> {
|
||||||
save.access()?
|
save.access()?
|
||||||
.prepare_write(1..5)?
|
.prepare_write(SAVE_OFFSET..SAVE_OFFSET + 4)?
|
||||||
.write(1, &score.to_le_bytes())?;
|
.write(SAVE_OFFSET, &score.to_le_bytes())?;
|
||||||
HIGH_SCORE.store(score, Ordering::SeqCst);
|
HIGH_SCORE.store(score, Ordering::SeqCst);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,8 +121,10 @@ pub fn entry(mut gba: agb::Gba) -> ! {
|
||||||
oam: unmanaged,
|
oam: unmanaged,
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut current_level = 0;
|
let saved_level = save::load_max_level() as usize;
|
||||||
let mut maximum_level = save::load_max_level() as usize;
|
|
||||||
|
let mut current_level = saved_level;
|
||||||
|
let mut maximum_level = saved_level;
|
||||||
loop {
|
loop {
|
||||||
if current_level >= level::Level::num_levels() {
|
if current_level >= level::Level::num_levels() {
|
||||||
current_level = 0;
|
current_level = 0;
|
||||||
|
|
|
@ -5,6 +5,7 @@ use agb::{
|
||||||
};
|
};
|
||||||
|
|
||||||
static MAXIMUM_LEVEL: AtomicU32 = AtomicU32::new(0);
|
static MAXIMUM_LEVEL: AtomicU32 = AtomicU32::new(0);
|
||||||
|
static SAVE_OFFSET: usize = 0xFF;
|
||||||
|
|
||||||
pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
|
pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
|
||||||
gba.save.init_sram();
|
gba.save.init_sram();
|
||||||
|
@ -20,7 +21,7 @@ pub fn init_save(gba: &mut Gba) -> Result<(), Error> {
|
||||||
save_max_level(&mut gba.save, 0)?;
|
save_max_level(&mut gba.save, 0)?;
|
||||||
} else {
|
} else {
|
||||||
let mut buffer = [0; 4];
|
let mut buffer = [0; 4];
|
||||||
access.read(1, &mut buffer)?;
|
access.read(SAVE_OFFSET, &mut buffer)?;
|
||||||
let max_level = u32::from_le_bytes(buffer);
|
let max_level = u32::from_le_bytes(buffer);
|
||||||
|
|
||||||
if max_level > 100 {
|
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> {
|
pub fn save_max_level(save: &mut SaveManager, level: u32) -> Result<(), Error> {
|
||||||
save.access()?
|
save.access()?
|
||||||
.prepare_write(1..5)?
|
.prepare_write(SAVE_OFFSET..SAVE_OFFSET + 4)?
|
||||||
.write(1, &level.to_le_bytes())?;
|
.write(SAVE_OFFSET, &level.to_le_bytes())?;
|
||||||
MAXIMUM_LEVEL.store(level, Ordering::SeqCst);
|
MAXIMUM_LEVEL.store(level, Ordering::SeqCst);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue