//! Run a ROM headless (skip-boot) and write the picture after N frames as a PNG, pressing the //! gate's input script on the way (scripts/inputs/poke.txt), to look at what a game shows. //! //! cargo run --release --example screenshot -- [MACHINE:]ROM --frames N --out FILE.png use std::collections::HashMap; use std::io::Write; use std::path::Path; use gameboy_rust::boot::read_image; use gameboy_rust::{load_cartridge, machine, Clock, GameBoy}; const SLICE_T: u64 = 70224; const BUTTONS: [&str; 8] = ["a", "b", "select", "start", "right", "left", "up", "down"]; const SHADES: [[u8; 3]; 4] = [ [0xE0, 0xF8, 0xD0], [0x88, 0xC0, 0x70], [0x34, 0x68, 0x56], [0x08, 0x18, 0x20], ]; fn chunk(out: &mut Vec, kind: &[u8; 4], data: &[u8]) { out.extend((data.len() as u32).to_be_bytes()); let mut crc = crc32fast::Hasher::new(); crc.update(kind); crc.update(data); out.extend(kind); out.extend(data); out.extend(crc.finalize().to_be_bytes()); } fn main() { let args: Vec = std::env::args().skip(1).collect(); let mut frames = 300u64; let mut out = "screenshot.png".to_string(); let mut game = None; let mut i = 0; while i < args.len() { match args[i].as_str() { "--frames" => { i += 1; frames = args[i].parse().expect("frames"); } "--out" => { i += 1; out = args[i].clone(); } other => game = Some(other.to_string()), } i += 1; } let game = game.expect("usage: screenshot [MACHINE:]ROM --frames N --out FILE.png"); let (key, rom) = match game.split_once(':') { Some((k, r)) if machine(k).is_some() => (k.to_string(), r.to_string()), _ => ("dmg".to_string(), game.clone()), }; let m = machine(&key).unwrap(); let data = read_image(Path::new(&rom)).expect("ROM"); let mut gb = GameBoy::new(load_cartridge(&data, Clock::Board).unwrap(), m, None).unwrap(); let script = std::fs::read_to_string(concat!( env!("CARGO_MANIFEST_DIR"), "/scripts/inputs/poke.txt" )) .unwrap(); let mut inputs: HashMap> = HashMap::new(); for line in script.lines() { let words: Vec<&str> = line .split('#') .next() .unwrap_or("") .split_whitespace() .collect(); if let Some(first) = words.first() { inputs.insert( first.parse().unwrap(), words[1..] .iter() .filter(|w| **w != "-") .map(|w| w.to_string()) .collect(), ); } } let mut pressed: Vec = Vec::new(); for index in 0..frames { if let Some(wanted) = inputs.get(&index) { for name in BUTTONS { let (was, is) = ( pressed.iter().any(|p| p == name), wanted.iter().any(|w| w == name), ); if was != is { gb.set_pressed(name, is); } } pressed = wanted.clone(); } while gb.hw.t < (index + 1) * SLICE_T { gb.step(); } } let mut raw = Vec::new(); for y in 0..144 { raw.push(0u8); for x in 0..160 { let v = gb.hw.ppu.frame[y * 160 + x]; if m.cgb_hardware { let five = |c: u16| ((c & 31) as u8) << 3 | ((c & 31) as u8) >> 2; raw.extend([five(v), five(v >> 5), five(v >> 10)]); } else { raw.extend(SHADES[(v & 3) as usize]); } } } let mut z = flate2::write::ZlibEncoder::new(Vec::new(), flate2::Compression::default()); z.write_all(&raw).unwrap(); let mut png = b"\x89PNG\r\n\x1a\n".to_vec(); let mut ihdr = Vec::new(); ihdr.extend(160u32.to_be_bytes()); ihdr.extend(144u32.to_be_bytes()); ihdr.extend([8, 2, 0, 0, 0]); chunk(&mut png, b"IHDR", &ihdr); chunk(&mut png, b"IDAT", &z.finish().unwrap()); chunk(&mut png, b"IEND", &[]); std::fs::write(&out, png).unwrap(); eprintln!("{out}: {key} after {frames} frames"); }