feat/savestates: Implement savestates for plat/sdl2

Former-commit-id: e61193e0c9a937dd3b4b7e7e748d9a9b1e170e44
This commit is contained in:
Michel Heily 2020-01-16 20:07:14 +02:00
parent f4460b2740
commit dc1cd84225
2 changed files with 39 additions and 2 deletions

View file

@ -28,6 +28,7 @@ spin_sleep="0.3.7"
bit-set = "0.5.1"
ringbuf = "0.2.1"
debug_stub_derive = "0.3.0"
bytesize = "1.0.0"
rustyline = {version = "5.0.0", optional = true}
nom = {version = "5.0.0", optional = true}

View file

@ -6,12 +6,13 @@ use sdl2::pixels::Color;
use sdl2::rect::Rect;
use sdl2::EventPump;
extern crate bytesize;
extern crate spin_sleep;
use std::cell::RefCell;
use std::rc::Rc;
use std::path::Path;
use std::path::{Path, PathBuf};
use std::time;
use std::process;
@ -31,6 +32,10 @@ extern crate rustboyadvance_ng;
use rustboyadvance_ng::prelude::*;
use rustboyadvance_ng::util::FpsCounter;
fn get_savestate_path(rom_filename: &Path) -> PathBuf {
rom_filename.with_extension("savestate")
}
/// Waits for the user to drag a rom file to window
fn wait_for_rom(event_pump: &mut EventPump) -> String {
'running: loop {
@ -46,7 +51,7 @@ fn wait_for_rom(event_pump: &mut EventPump) -> String {
}
}
fn main() {
fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut frame_limiter = true;
let yaml = load_yaml!("cli.yml");
let matches = clap::App::from_yaml(yaml).get_matches();
@ -110,6 +115,8 @@ fn main() {
}
};
let mut savestate_path = get_savestate_path(&Path::new(&rom_path));
let mut rom_name = Path::new(&rom_path).file_name().unwrap().to_str().unwrap();
let cart = Cartridge::from_path(Path::new(&rom_path)).unwrap();
@ -173,6 +180,32 @@ fn main() {
println!("ending debugger...");
break;
}
Event::KeyUp {
keycode: Some(Keycode::F5),
..
} => {
println!("Saving state ...");
let save = gba.save_state()?;
write_bin_file(&savestate_path, &save)?;
println!(
"Saved to {:?} ({})",
savestate_path,
bytesize::ByteSize::b(save.len() as u64)
);
}
Event::KeyUp {
keycode: Some(Keycode::F9),
..
} => {
if savestate_path.is_file() {
let save = read_bin_file(&savestate_path)?;
println!("Restoring state from {:?}...", savestate_path);
gba.restore_state(&save)?;
println!("Restored!");
} else {
println!("Savestate not created, please create one by pressing F5");
}
}
Event::KeyDown {
keycode: Some(keycode),
..
@ -189,6 +222,7 @@ fn main() {
Event::DropFile { filename, .. } => {
// load the new rom
rom_path = filename;
savestate_path = get_savestate_path(&Path::new(&rom_path));
rom_name = Path::new(&rom_path).file_name().unwrap().to_str().unwrap();
let cart = Cartridge::from_path(Path::new(&rom_path)).unwrap();
let bios_bin = read_bin_file(bios_path).unwrap();
@ -226,4 +260,6 @@ fn main() {
};
}
}
Ok(())
}