2021-01-19 02:47:09 +00:00
|
|
|
use anyhow::{anyhow, Result};
|
2021-03-23 02:41:22 +00:00
|
|
|
use clap::{crate_authors, crate_description, crate_name, crate_version, App, Arg};
|
2021-07-28 07:01:04 +00:00
|
|
|
use gb::{AudioSPSC, Cycle, GB_HEIGHT, GB_WIDTH};
|
2021-06-07 22:23:48 +00:00
|
|
|
use gilrs::Gilrs;
|
2021-07-19 02:33:52 +00:00
|
|
|
use pixels::{PixelsBuilder, SurfaceTexture};
|
2021-07-28 07:01:04 +00:00
|
|
|
use rodio::{OutputStream, Sink};
|
2021-08-03 19:09:16 +00:00
|
|
|
use std::time::{Duration, Instant};
|
2021-03-21 00:56:26 +00:00
|
|
|
use winit::dpi::LogicalSize;
|
|
|
|
use winit::event::{Event, VirtualKeyCode};
|
|
|
|
use winit::event_loop::{ControlFlow, EventLoop};
|
|
|
|
use winit::window::{Window, WindowBuilder};
|
2021-01-19 02:47:09 +00:00
|
|
|
use winit_input_helper::WinitInputHelper;
|
2020-12-23 09:25:16 +00:00
|
|
|
|
2021-08-03 19:09:16 +00:00
|
|
|
const WINDOW_SCALE: f64 = 2.0;
|
2021-03-21 00:56:26 +00:00
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
fn main() -> Result<()> {
|
2021-03-23 02:41:22 +00:00
|
|
|
let app = App::new(crate_name!())
|
|
|
|
.version(crate_version!())
|
|
|
|
.author(crate_authors!())
|
|
|
|
.about(crate_description!());
|
|
|
|
|
|
|
|
let m = app
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("rom")
|
|
|
|
.value_name("ROM_FILE")
|
|
|
|
.takes_value(true)
|
|
|
|
.required(true)
|
|
|
|
.index(1)
|
2021-04-14 06:21:45 +00:00
|
|
|
.help("Path to the Game ROM"),
|
2021-03-23 02:41:22 +00:00
|
|
|
)
|
|
|
|
.arg(
|
|
|
|
Arg::with_name("boot")
|
|
|
|
.short("b")
|
|
|
|
.long("boot")
|
|
|
|
.value_name("FILE")
|
|
|
|
.takes_value(true)
|
|
|
|
.help("Path to Boot ROM"),
|
|
|
|
)
|
|
|
|
.get_matches();
|
|
|
|
|
2021-07-08 23:50:58 +00:00
|
|
|
let rom_path = m
|
|
|
|
.value_of("rom")
|
|
|
|
.expect("Required value 'rom' was provided");
|
2021-03-23 02:41:22 +00:00
|
|
|
|
2021-06-28 01:28:29 +00:00
|
|
|
let mut game_boy =
|
2021-08-03 19:09:16 +00:00
|
|
|
gb::emu::init(m.value_of("boot"), rom_path).expect("Initialize DMG-01 Emulator");
|
|
|
|
let rom_title = gb::emu::rom_title(&game_boy);
|
2021-04-14 06:21:45 +00:00
|
|
|
|
2021-08-03 19:09:16 +00:00
|
|
|
let mut gamepad = Gilrs::new().expect("Initialize Controller Support");
|
2021-05-04 04:11:39 +00:00
|
|
|
|
2021-03-23 02:41:22 +00:00
|
|
|
// Initialize GUI
|
2021-01-19 02:47:09 +00:00
|
|
|
let event_loop = EventLoop::new();
|
|
|
|
let mut input = WinitInputHelper::new();
|
2021-08-03 19:09:16 +00:00
|
|
|
let window = create_window(&event_loop, rom_title)?;
|
2021-06-02 06:50:16 +00:00
|
|
|
|
2021-07-28 03:38:21 +00:00
|
|
|
let mut pixels = {
|
2021-06-02 06:50:16 +00:00
|
|
|
let size = window.inner_size();
|
|
|
|
let surface_texture = SurfaceTexture::new(size.width, size.height, &window);
|
2021-07-19 02:33:52 +00:00
|
|
|
|
2021-07-28 03:38:21 +00:00
|
|
|
PixelsBuilder::new(GB_WIDTH as u32, GB_HEIGHT as u32, surface_texture)
|
2021-07-19 02:33:52 +00:00
|
|
|
.enable_vsync(false)
|
2021-07-28 03:38:21 +00:00
|
|
|
.build()?
|
2021-06-02 06:50:16 +00:00
|
|
|
};
|
2021-01-28 04:07:31 +00:00
|
|
|
|
2021-08-03 19:09:16 +00:00
|
|
|
// Initialize Audio
|
2021-08-16 04:31:27 +00:00
|
|
|
// let spsc: AudioSPSC<f32> = Default::default();
|
|
|
|
// let (prod, cons) = spsc.init();
|
|
|
|
// let (_stream, stream_handle) = OutputStream::try_default().expect("Initialized Audio");
|
|
|
|
// let sink = Sink::try_new(&stream_handle)?;
|
|
|
|
// sink.append(cons);
|
|
|
|
// game_boy.apu_mut().set_producer(prod);
|
2021-07-09 06:25:52 +00:00
|
|
|
|
2021-08-16 04:31:27 +00:00
|
|
|
// std::thread::spawn(move || {
|
|
|
|
// sink.sleep_until_end();
|
|
|
|
// });
|
2021-07-09 06:25:52 +00:00
|
|
|
|
2021-08-03 19:09:16 +00:00
|
|
|
let mut start = Instant::now();
|
2021-08-14 22:59:59 +00:00
|
|
|
let frame_time = Duration::from_secs_f64(1.0 / 59.73); // 59.73 Hz on Host
|
2021-06-28 01:28:29 +00:00
|
|
|
let mut cycle_count: Cycle = Default::default();
|
2021-06-02 06:50:16 +00:00
|
|
|
|
2021-01-19 02:47:09 +00:00
|
|
|
event_loop.run(move |event, _, control_flow| {
|
|
|
|
if let Event::RedrawRequested(_) = event {
|
2021-07-28 03:38:21 +00:00
|
|
|
if pixels
|
|
|
|
.render()
|
2021-01-19 02:47:09 +00:00
|
|
|
.map_err(|e| anyhow!("pixels.render() failed: {}", e))
|
|
|
|
.is_err()
|
|
|
|
{
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if input.update(&event) {
|
|
|
|
if input.key_pressed(VirtualKeyCode::Escape) || input.quit() {
|
|
|
|
*control_flow = ControlFlow::Exit;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-06-02 06:50:16 +00:00
|
|
|
if let Some(size) = input.window_resized() {
|
|
|
|
pixels.resize_surface(size.width, size.height);
|
2021-05-04 04:11:39 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 19:09:16 +00:00
|
|
|
let mut diff = Instant::now() - start;
|
|
|
|
while diff.subsec_nanos() < frame_time.subsec_nanos() {
|
|
|
|
if cycle_count < gb::emu::CYCLES_IN_FRAME {
|
|
|
|
cycle_count += gb::emu::run_frame(&mut game_boy, &mut gamepad, &input);
|
|
|
|
}
|
2021-03-21 00:56:26 +00:00
|
|
|
|
2021-08-03 19:09:16 +00:00
|
|
|
diff = Instant::now() - start;
|
|
|
|
}
|
|
|
|
start = Instant::now();
|
2021-03-21 00:56:26 +00:00
|
|
|
|
2021-06-28 01:28:29 +00:00
|
|
|
if cycle_count >= gb::emu::CYCLES_IN_FRAME {
|
2021-07-28 21:23:31 +00:00
|
|
|
cycle_count %= gb::emu::CYCLES_IN_FRAME;
|
2021-03-21 00:56:26 +00:00
|
|
|
|
2021-06-28 01:28:29 +00:00
|
|
|
gb::emu::draw(game_boy.ppu(), pixels.get_frame());
|
2021-06-02 06:50:16 +00:00
|
|
|
window.request_redraw();
|
2021-03-21 00:56:26 +00:00
|
|
|
}
|
2021-01-19 02:47:09 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-17 02:07:00 +00:00
|
|
|
#[cfg(not(windows))]
|
2021-04-14 06:21:45 +00:00
|
|
|
fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> {
|
2021-08-14 06:02:18 +00:00
|
|
|
let size = LogicalSize::new(
|
|
|
|
(GB_WIDTH as f64) * WINDOW_SCALE,
|
|
|
|
(GB_HEIGHT as f64) * WINDOW_SCALE,
|
|
|
|
);
|
2021-01-19 02:47:09 +00:00
|
|
|
Ok(WindowBuilder::new()
|
2021-04-14 06:21:45 +00:00
|
|
|
.with_title(title)
|
2021-01-19 02:47:09 +00:00
|
|
|
.with_inner_size(size)
|
|
|
|
.with_min_inner_size(size)
|
2021-06-28 01:28:29 +00:00
|
|
|
.with_resizable(true)
|
|
|
|
.with_decorations(true)
|
|
|
|
.with_transparent(false)
|
2021-07-17 02:07:00 +00:00
|
|
|
.build(event_loop)?)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
fn create_window(event_loop: &EventLoop<()>, title: &str) -> Result<Window> {
|
|
|
|
use winit::platform::windows::WindowBuilderExtWindows;
|
|
|
|
|
2021-08-03 19:09:16 +00:00
|
|
|
let size = LogicalSize::new(
|
|
|
|
(GB_WIDTH as f64) * WINDOW_SCALE,
|
|
|
|
(GB_HEIGHT as f64) * WINDOW_SCALE,
|
|
|
|
);
|
2021-07-17 02:07:00 +00:00
|
|
|
Ok(WindowBuilder::new()
|
|
|
|
.with_title(title)
|
|
|
|
.with_inner_size(size)
|
|
|
|
.with_min_inner_size(size)
|
|
|
|
.with_resizable(true)
|
|
|
|
.with_decorations(true)
|
|
|
|
.with_transparent(false)
|
|
|
|
.with_drag_and_drop(false)
|
2021-06-07 02:30:08 +00:00
|
|
|
.build(event_loop)?)
|
2021-01-19 02:47:09 +00:00
|
|
|
}
|