Skip to content

Commit

Permalink
set window title from the rom
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathanballs committed Sep 9, 2024
1 parent 401ffc2 commit e10329e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 45 deletions.
93 changes: 50 additions & 43 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -17,59 +19,64 @@ use minifb::Key;
use mmu::ppu::PPU;
use renderer::window_loop;

fn read_file_to_bytes(filename: &str) -> Result<Vec<u8>, 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::<PPU>();
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<u8>, tx: Sender<PPU>, 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<Vec<u8>, std::io::Error> {
let mut file = File::open(filename)?;
let mut buffer = Vec::new();
file.read_to_end(&mut buffer)?;
Ok(buffer)
}
4 changes: 2 additions & 2 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ fn latest_ppu(rx: &Receiver<PPU>) -> Option<PPU> {
latest_frame
}

pub fn window_loop(rx: Receiver<PPU>, tx: Sender<(bool, Key)>) {
pub fn window_loop(rx: Receiver<PPU>, tx: Sender<(bool, Key)>, game_title: &String) {
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];

let mut window = Window::new(
"Cowboy Emulator",
format!("Cowboy Emulator - {}", game_title).as_str(),
WIDTH,
HEIGHT,
WindowOptions {
Expand Down

0 comments on commit e10329e

Please sign in to comment.