2019-12-04 23:15:49 +00:00
|
|
|
extern crate sdl2;
|
2019-12-20 16:11:14 +00:00
|
|
|
use sdl2::event::Event;
|
2020-01-11 23:29:36 +00:00
|
|
|
use sdl2::image::{InitFlag, LoadTexture};
|
2019-12-20 16:11:14 +00:00
|
|
|
use sdl2::keyboard::Keycode;
|
2020-01-11 23:29:36 +00:00
|
|
|
use sdl2::pixels::Color;
|
|
|
|
use sdl2::rect::Rect;
|
2020-01-11 15:28:25 +00:00
|
|
|
use sdl2::EventPump;
|
2019-12-09 21:37:46 +00:00
|
|
|
|
2020-01-16 18:07:14 +00:00
|
|
|
extern crate bytesize;
|
2019-12-20 17:02:33 +00:00
|
|
|
extern crate spin_sleep;
|
|
|
|
|
2019-12-09 21:37:46 +00:00
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
2019-12-04 23:15:49 +00:00
|
|
|
|
2020-01-16 18:07:14 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-12-04 23:15:49 +00:00
|
|
|
use std::time;
|
|
|
|
|
2020-01-11 15:28:25 +00:00
|
|
|
use std::process;
|
|
|
|
|
2019-12-04 23:15:49 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate clap;
|
|
|
|
|
|
|
|
mod audio;
|
2019-12-20 16:11:14 +00:00
|
|
|
mod input;
|
2019-12-04 23:15:49 +00:00
|
|
|
mod video;
|
|
|
|
|
2019-12-28 13:59:35 +00:00
|
|
|
use audio::create_audio_player;
|
|
|
|
use input::create_input;
|
2020-01-20 23:11:50 +00:00
|
|
|
use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH};
|
2019-12-04 23:15:49 +00:00
|
|
|
|
|
|
|
extern crate rustboyadvance_ng;
|
|
|
|
use rustboyadvance_ng::prelude::*;
|
|
|
|
use rustboyadvance_ng::util::FpsCounter;
|
|
|
|
|
2020-01-16 18:07:14 +00:00
|
|
|
fn get_savestate_path(rom_filename: &Path) -> PathBuf {
|
|
|
|
rom_filename.with_extension("savestate")
|
|
|
|
}
|
|
|
|
|
2020-01-11 15:28:25 +00:00
|
|
|
/// Waits for the user to drag a rom file to window
|
|
|
|
fn wait_for_rom(event_pump: &mut EventPump) -> String {
|
|
|
|
'running: loop {
|
|
|
|
for event in event_pump.poll_iter() {
|
|
|
|
match event {
|
|
|
|
Event::DropFile { filename, .. } => {
|
|
|
|
return filename;
|
|
|
|
}
|
|
|
|
Event::Quit { .. } => process::exit(0),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 18:07:14 +00:00
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
2019-12-20 16:11:14 +00:00
|
|
|
let mut frame_limiter = true;
|
2019-12-04 23:15:49 +00:00
|
|
|
let yaml = load_yaml!("cli.yml");
|
|
|
|
let matches = clap::App::from_yaml(yaml).get_matches();
|
|
|
|
|
|
|
|
let skip_bios = matches.occurrences_of("skip_bios") != 0;
|
2019-12-29 21:37:23 +00:00
|
|
|
|
2019-12-04 23:15:49 +00:00
|
|
|
let debug = matches.occurrences_of("debug") != 0;
|
|
|
|
|
2020-01-11 23:29:36 +00:00
|
|
|
let sdl_context = sdl2::init().expect("failed to initialize sdl2");
|
2020-01-20 23:11:50 +00:00
|
|
|
let mut event_pump = sdl_context.event_pump()?;
|
2020-01-11 15:28:25 +00:00
|
|
|
|
2020-01-20 23:11:50 +00:00
|
|
|
let video_subsystem = sdl_context.video()?;
|
|
|
|
let _image_context = sdl2::image::init(InitFlag::PNG | InitFlag::JPG)?;
|
2019-12-29 21:38:51 +00:00
|
|
|
let window = video_subsystem
|
2020-01-20 23:11:50 +00:00
|
|
|
.window("RustBoyAdvance", SCREEN_WIDTH * 3, SCREEN_HEIGHT * 3)
|
2019-12-29 21:38:51 +00:00
|
|
|
.opengl()
|
|
|
|
.position_centered()
|
2020-01-20 23:11:50 +00:00
|
|
|
.resizable()
|
|
|
|
.build()?;
|
|
|
|
let mut canvas = window.into_canvas().accelerated().build()?;
|
|
|
|
|
|
|
|
canvas.set_logical_size(SCREEN_WIDTH as u32, SCREEN_HEIGHT as u32)?;
|
2020-01-11 23:29:36 +00:00
|
|
|
|
|
|
|
// Display the icon as a placeholder
|
2020-01-12 22:33:13 +00:00
|
|
|
canvas.set_draw_color(Color::RGB(0x40, 0x22, 0x20)); // default background color for the icon
|
2020-01-11 23:29:36 +00:00
|
|
|
canvas.clear();
|
|
|
|
let texture_creator = canvas.texture_creator();
|
|
|
|
let icon_texture = texture_creator
|
|
|
|
.load_texture("assets/icon.png")
|
|
|
|
.expect("failed to load icon");
|
2020-01-20 23:11:50 +00:00
|
|
|
canvas.copy(&icon_texture, None, None).unwrap();
|
2020-01-11 23:29:36 +00:00
|
|
|
canvas.present();
|
|
|
|
|
|
|
|
// TODO also set window icon
|
|
|
|
|
|
|
|
let video = Rc::new(RefCell::new(create_video_interface(canvas)));
|
2019-12-09 21:37:46 +00:00
|
|
|
let audio = Rc::new(RefCell::new(create_audio_player(&sdl_context)));
|
2019-12-20 16:11:14 +00:00
|
|
|
let input = Rc::new(RefCell::new(create_input()));
|
2019-12-04 23:15:49 +00:00
|
|
|
|
2020-01-11 15:28:25 +00:00
|
|
|
let bios_path = Path::new(matches.value_of("bios").unwrap_or_default());
|
|
|
|
let bios_bin = read_bin_file(bios_path).unwrap();
|
|
|
|
|
|
|
|
let mut rom_path = match matches.value_of("game_rom") {
|
|
|
|
Some(path) => path.to_string(),
|
|
|
|
_ => {
|
|
|
|
println!("[!] Rom file missing, please drag a rom file into the emulator window...");
|
|
|
|
wait_for_rom(&mut event_pump)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-01-16 18:07:14 +00:00
|
|
|
let mut savestate_path = get_savestate_path(&Path::new(&rom_path));
|
|
|
|
|
2020-01-11 15:28:25 +00:00
|
|
|
let mut rom_name = Path::new(&rom_path).file_name().unwrap().to_str().unwrap();
|
|
|
|
let cart = Cartridge::from_path(Path::new(&rom_path)).unwrap();
|
|
|
|
|
2019-12-22 23:27:25 +00:00
|
|
|
let mut gba = GameBoyAdvance::new(
|
2020-01-16 17:47:05 +00:00
|
|
|
arm7tdmi::Core::new(),
|
2019-12-20 16:11:14 +00:00
|
|
|
bios_bin,
|
|
|
|
cart,
|
|
|
|
video.clone(),
|
|
|
|
audio.clone(),
|
|
|
|
input.clone(),
|
|
|
|
);
|
|
|
|
|
2020-01-16 17:47:05 +00:00
|
|
|
if skip_bios {
|
|
|
|
gba.skip_bios();
|
|
|
|
}
|
|
|
|
|
2019-12-04 23:15:49 +00:00
|
|
|
if debug {
|
2020-01-12 23:11:58 +00:00
|
|
|
#[cfg(feature = "debugger")]
|
2019-12-29 21:37:23 +00:00
|
|
|
{
|
|
|
|
gba.cpu.set_verbose(true);
|
|
|
|
let mut debugger = Debugger::new(gba);
|
|
|
|
println!("starting debugger...");
|
|
|
|
debugger.repl(matches.value_of("script_file")).unwrap();
|
|
|
|
println!("ending debugger...");
|
|
|
|
return;
|
|
|
|
}
|
2020-01-12 23:11:58 +00:00
|
|
|
#[cfg(not(feature = "debugger"))]
|
2019-12-29 21:37:23 +00:00
|
|
|
{
|
2020-01-12 23:11:58 +00:00
|
|
|
panic!("Please compile me with 'debugger' feature");
|
2019-12-29 21:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 15:28:25 +00:00
|
|
|
let mut fps_counter = FpsCounter::default();
|
2019-12-29 21:37:23 +00:00
|
|
|
let frame_time = time::Duration::new(0, 1_000_000_000u32 / 60);
|
2020-01-11 15:28:25 +00:00
|
|
|
'running: loop {
|
2019-12-29 21:37:23 +00:00
|
|
|
let start_time = time::Instant::now();
|
|
|
|
|
|
|
|
for event in event_pump.poll_iter() {
|
|
|
|
match event {
|
|
|
|
Event::KeyDown {
|
|
|
|
keycode: Some(Keycode::Space),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
frame_limiter = false;
|
|
|
|
}
|
|
|
|
Event::KeyUp {
|
|
|
|
keycode: Some(Keycode::Space),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
frame_limiter = true;
|
|
|
|
}
|
2020-01-12 23:11:58 +00:00
|
|
|
#[cfg(feature = "debugger")]
|
2019-12-29 21:37:23 +00:00
|
|
|
Event::KeyUp {
|
|
|
|
keycode: Some(Keycode::F1),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
let mut debugger = Debugger::new(gba);
|
|
|
|
println!("starting debugger...");
|
|
|
|
debugger.repl(matches.value_of("script_file")).unwrap();
|
|
|
|
gba = debugger.gba;
|
|
|
|
println!("ending debugger...");
|
|
|
|
break;
|
2019-12-20 16:11:14 +00:00
|
|
|
}
|
2020-01-16 18:07:14 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2019-12-29 21:37:23 +00:00
|
|
|
Event::KeyDown {
|
|
|
|
keycode: Some(keycode),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
input.borrow_mut().on_keyboard_key_down(keycode);
|
|
|
|
}
|
|
|
|
Event::KeyUp {
|
|
|
|
keycode: Some(keycode),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
input.borrow_mut().on_keyboard_key_up(keycode);
|
|
|
|
}
|
2020-01-11 15:28:25 +00:00
|
|
|
Event::Quit { .. } => break 'running,
|
|
|
|
Event::DropFile { filename, .. } => {
|
|
|
|
// load the new rom
|
|
|
|
rom_path = filename;
|
2020-01-16 18:07:14 +00:00
|
|
|
savestate_path = get_savestate_path(&Path::new(&rom_path));
|
2020-01-11 15:28:25 +00:00
|
|
|
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();
|
|
|
|
|
|
|
|
// create a new emulator - TODO, export to a function
|
|
|
|
gba = GameBoyAdvance::new(
|
2020-01-16 17:47:05 +00:00
|
|
|
arm7tdmi::Core::new(),
|
2020-01-11 15:28:25 +00:00
|
|
|
bios_bin,
|
|
|
|
cart,
|
|
|
|
video.clone(),
|
|
|
|
audio.clone(),
|
|
|
|
input.clone(),
|
|
|
|
);
|
2020-01-16 17:47:05 +00:00
|
|
|
gba.skip_bios();
|
2020-01-11 15:28:25 +00:00
|
|
|
}
|
2019-12-29 21:37:23 +00:00
|
|
|
_ => {}
|
2019-12-20 16:11:14 +00:00
|
|
|
}
|
2019-12-29 21:37:23 +00:00
|
|
|
}
|
2019-12-20 16:11:14 +00:00
|
|
|
|
2019-12-29 21:37:23 +00:00
|
|
|
gba.frame();
|
2019-12-09 21:37:46 +00:00
|
|
|
|
2019-12-29 21:37:23 +00:00
|
|
|
if let Some(fps) = fps_counter.tick() {
|
|
|
|
let title = format!("{} ({} fps)", rom_name, fps);
|
|
|
|
video.borrow_mut().set_window_title(&title);
|
|
|
|
}
|
2019-12-04 23:15:49 +00:00
|
|
|
|
2019-12-29 21:37:23 +00:00
|
|
|
if frame_limiter {
|
|
|
|
let time_passed = start_time.elapsed();
|
|
|
|
let delay = frame_time.checked_sub(time_passed);
|
|
|
|
match delay {
|
|
|
|
None => {}
|
|
|
|
Some(delay) => {
|
|
|
|
spin_sleep::sleep(delay);
|
|
|
|
}
|
|
|
|
};
|
2019-12-04 23:15:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 18:07:14 +00:00
|
|
|
|
|
|
|
Ok(())
|
2019-12-04 23:15:49 +00:00
|
|
|
}
|