2019-12-04 23:15:49 +00:00
|
|
|
extern crate sdl2;
|
2019-12-20 16:11:14 +00:00
|
|
|
use sdl2::event::Event;
|
|
|
|
use sdl2::keyboard::Keycode;
|
2019-12-09 21:37:46 +00:00
|
|
|
|
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
|
|
|
|
|
|
|
use std::path::Path;
|
|
|
|
use std::time;
|
|
|
|
|
|
|
|
#[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-09 21:37:46 +00:00
|
|
|
use audio::{create_audio_player, Sdl2AudioPlayer};
|
2019-12-20 16:11:14 +00:00
|
|
|
use input::{create_input, Sdl2Input};
|
2019-12-09 21:37:46 +00:00
|
|
|
use video::{create_video_interface, Sdl2Video};
|
2019-12-04 23:15:49 +00:00
|
|
|
|
|
|
|
extern crate rustboyadvance_ng;
|
|
|
|
use rustboyadvance_ng::prelude::*;
|
|
|
|
use rustboyadvance_ng::util::FpsCounter;
|
|
|
|
|
|
|
|
fn main() {
|
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;
|
|
|
|
let debug = matches.occurrences_of("debug") != 0;
|
|
|
|
|
|
|
|
let bios_path = Path::new(matches.value_of("bios").unwrap_or_default());
|
|
|
|
let rom_path = Path::new(matches.value_of("game_rom").unwrap());
|
|
|
|
let rom_name = rom_path.file_name().unwrap().to_str().unwrap();
|
|
|
|
|
|
|
|
let bios_bin = read_bin_file(bios_path).unwrap();
|
|
|
|
let cart = Cartridge::from_path(rom_path).unwrap();
|
|
|
|
let mut cpu = arm7tdmi::Core::new();
|
|
|
|
if skip_bios {
|
|
|
|
cpu.skip_bios();
|
|
|
|
}
|
|
|
|
let cpu = cpu;
|
|
|
|
|
|
|
|
let sdl_context = sdl2::init().unwrap();
|
2019-12-09 21:37:46 +00:00
|
|
|
let video = Rc::new(RefCell::new(create_video_interface(&sdl_context)));
|
|
|
|
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
|
|
|
|
|
|
|
let mut fps_counter = FpsCounter::default();
|
2019-12-22 23:27:25 +00:00
|
|
|
let mut gba = GameBoyAdvance::new(
|
2019-12-20 16:11:14 +00:00
|
|
|
cpu,
|
|
|
|
bios_bin,
|
|
|
|
cart,
|
|
|
|
video.clone(),
|
|
|
|
audio.clone(),
|
|
|
|
input.clone(),
|
|
|
|
);
|
|
|
|
|
|
|
|
let mut event_pump = sdl_context.event_pump().unwrap();
|
2019-12-04 23:15:49 +00:00
|
|
|
|
|
|
|
if debug {
|
|
|
|
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...");
|
|
|
|
} else {
|
|
|
|
let frame_time = time::Duration::new(0, 1_000_000_000u32 / 60);
|
|
|
|
loop {
|
|
|
|
let start_time = time::Instant::now();
|
|
|
|
|
2019-12-20 16:11:14 +00:00
|
|
|
for event in event_pump.poll_iter() {
|
|
|
|
match event {
|
|
|
|
Event::KeyDown {
|
|
|
|
keycode: Some(Keycode::Space),
|
|
|
|
..
|
|
|
|
} => {
|
|
|
|
frame_limiter = false;
|
|
|
|
}
|
|
|
|
Event::KeyUp {
|
|
|
|
keycode: Some(Keycode::Space),
|
|
|
|
..
|
|
|
|
} => {
|
2019-12-22 23:30:36 +00:00
|
|
|
audio.borrow_mut().device.clear(); // clear audio queue
|
2019-12-20 16:11:14 +00:00
|
|
|
frame_limiter = true;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
Event::Quit { .. } => panic!("quit!"),
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-09 21:37:46 +00:00
|
|
|
gba.frame();
|
|
|
|
|
2019-12-04 23:15:49 +00:00
|
|
|
if let Some(fps) = fps_counter.tick() {
|
|
|
|
let title = format!("{} ({} fps)", rom_name, fps);
|
2019-12-09 21:37:46 +00:00
|
|
|
video.borrow_mut().set_window_title(&title);
|
2019-12-04 23:15:49 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 16:11:14 +00:00
|
|
|
if frame_limiter {
|
2019-12-04 23:15:49 +00:00
|
|
|
let time_passed = start_time.elapsed();
|
|
|
|
let delay = frame_time.checked_sub(time_passed);
|
|
|
|
match delay {
|
|
|
|
None => {}
|
|
|
|
Some(delay) => {
|
2019-12-20 17:02:33 +00:00
|
|
|
spin_sleep::sleep(delay);
|
2019-12-04 23:15:49 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|