From e10329eb482ea411864a9a24834b9e83f9ded624 Mon Sep 17 00:00:00 2001 From: Jonathan Balls Date: Mon, 9 Sep 2024 16:57:33 +0100 Subject: [PATCH] set window title from the rom --- src/main.rs | 93 ++++++++++++++++++++++++++----------------------- src/renderer.rs | 4 +-- 2 files changed, 52 insertions(+), 45 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a93db4..4731570 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,10 +5,12 @@ mod registers; mod renderer; use colored::*; +use mmu::rom::GBCHeader; use std::fs::File; use std::io::Read; use std::process::exit; use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::mpsc::{Receiver, Sender}; use std::sync::{mpsc, Arc}; use std::thread; @@ -17,59 +19,64 @@ use minifb::Key; use mmu::ppu::PPU; use renderer::window_loop; -fn read_file_to_bytes(filename: &str) -> Result, std::io::Error> { - let mut file = File::open(filename)?; - let mut buffer = Vec::new(); - file.read_to_end(&mut buffer)?; - Ok(buffer) -} - fn main() { let (tx, rx) = mpsc::channel::(); let (tx_key, rx_key) = mpsc::channel::<(bool, Key)>(); + let rom = read_file_to_bytes("roms/tetris.gb").unwrap(); + let game_title = GBCHeader::new(&rom).unwrap().title(); - let emulator_loop = thread::spawn(move || { - let rom_data = read_file_to_bytes("roms/tetris.gb").unwrap(); - let mut gameboy = GameBoy::new(rom_data); + let _ = thread::spawn(move || emulator_loop(rom, tx, rx_key)); + window_loop(rx, tx_key, &game_title); +} - let paused = Arc::new(AtomicBool::new(false)); - let p = paused.clone(); +fn emulator_loop(rom: Vec, tx: Sender, rx: Receiver<(bool, Key)>) { + let mut gameboy = GameBoy::new(rom); - ctrlc::set_handler(move || { - if p.load(Ordering::SeqCst) { - // If already paused, stop the emulator - println!("{}", "\nSo long space cowboy".red()); - exit(-1); - } else { - // If running, pause the emulator - p.store(true, Ordering::SeqCst); - println!("Received Ctrl+C! Pausing at the end of this step..."); - } - }) - .expect("Error setting Ctrl-C handler"); + let paused = Arc::new(AtomicBool::new(false)); + let p = paused.clone(); - loop { - if paused.load(Ordering::SeqCst) { - gameboy.debugger_enabled = true; - } - paused.store(false, Ordering::SeqCst); + ctrlc::set_handler(move || { + if p.load(Ordering::SeqCst) { + // If already paused, stop the emulator + println!("{}", "\nSo long space cowboy".red()); + exit(-1); + } else { + // If running, pause the emulator + p.store(true, Ordering::SeqCst); + println!("Received Ctrl+C! Pausing at the end of this step..."); + } + }) + .expect("Error setting Ctrl-C handler"); - gameboy.step(); - if gameboy.mmu.ppu.get_and_reset_frame_available() { - let _ = tx.send(gameboy.mmu.ppu.clone()); - } + loop { + // Enter debug mode if Ctrl-C received + if paused.load(Ordering::SeqCst) { + gameboy.debugger_enabled = true; + } + paused.store(false, Ordering::SeqCst); - loop { - match rx_key.try_recv() { - Ok((true, key)) => gameboy.mmu.joypad.handle_key_down(key), - Ok((false, key)) => gameboy.mmu.joypad.handle_key_up(key), - _ => break, - } - } + // Step forward + gameboy.step(); + + // Render window + if gameboy.mmu.ppu.get_and_reset_frame_available() { + let _ = tx.send(gameboy.mmu.ppu.clone()); } - }); - window_loop(rx, tx_key); + // Handle joypad input + loop { + match rx.try_recv() { + Ok((true, key)) => gameboy.mmu.joypad.handle_key_down(key), + Ok((false, key)) => gameboy.mmu.joypad.handle_key_up(key), + _ => break, + } + } + } +} - let _ = emulator_loop.join(); +fn read_file_to_bytes(filename: &str) -> Result, std::io::Error> { + let mut file = File::open(filename)?; + let mut buffer = Vec::new(); + file.read_to_end(&mut buffer)?; + Ok(buffer) } diff --git a/src/renderer.rs b/src/renderer.rs index 77eb5f9..bac7354 100644 --- a/src/renderer.rs +++ b/src/renderer.rs @@ -52,11 +52,11 @@ fn latest_ppu(rx: &Receiver) -> Option { latest_frame } -pub fn window_loop(rx: Receiver, tx: Sender<(bool, Key)>) { +pub fn window_loop(rx: Receiver, tx: Sender<(bool, Key)>, game_title: &String) { let mut buffer: Vec = vec![0; WIDTH * HEIGHT]; let mut window = Window::new( - "Cowboy Emulator", + format!("Cowboy Emulator - {}", game_title).as_str(), WIDTH, HEIGHT, WindowOptions {