//! The remix's layers put back together are the board's picture: for every //! 5th frame of the game-2400 reference run (and a stretch of the TAS movie, //! for later stages), each sprite cell of `Video::render_layers` placed at its //! `objects` box in slot order, then the tile layer over them, equals //! `Video::render` with the stars off, pixel for pixel. Needs rom/galaga. use std::path::Path; use emu_galaga::galaga::movie::Movie; use emu_galaga::galaga::objects::objects; use emu_galaga::galaga::roms::{load_directory, RomSet}; use emu_galaga::galaga::trace::parse_inputs; use emu_galaga::galaga::video::{Video, SCREEN_HEIGHT, SCREEN_WIDTH}; use emu_galaga::galaga::Machine; fn compose(machine: &Machine, video: &mut Video, atlas: &mut [u8], tiles: &mut [u8]) -> Vec { let shared = machine.shared.borrow(); video.render_layers(&shared, atlas, tiles); let mut out = vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4]; for px in out.chunks_mut(4) { px[3] = 0xff; } for object in objects(&shared) { let n = usize::from(object.slot); let (cx, cy) = (32 * (n % 8), 32 * (n / 8)); for v in 0..object.h { for u in 0..object.w { let (x, y) = (object.x + u, object.y + v); if x < 0 || y < 0 || x >= SCREEN_WIDTH as i32 || y >= SCREEN_HEIGHT as i32 { continue; } let a = ((cy + v as usize) * 256 + cx + u as usize) * 4; if atlas[a + 3] != 0 { let o = (y as usize * SCREEN_WIDTH + x as usize) * 4; out[o..o + 4].copy_from_slice(&atlas[a..a + 4]); } } } } for (o, t) in out.chunks_mut(4).zip(tiles.chunks(4)) { if t[3] != 0 { o.copy_from_slice(t); } } out } fn check(machine: &Machine, roms: &RomSet, reference: &mut Video, label: &str) { let mut video = Video::new(roms); let mut atlas = vec![0u8; 256 * 256 * 4]; let mut tiles = vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4]; let ours = compose(machine, &mut video, &mut atlas, &mut tiles); reference.render( &machine.shared.borrow(), machine.videolatch_at_vblank_end & !0x20, ); let mut theirs = vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4]; reference.screen_rgba(&mut theirs); let differing = ours .chunks(4) .zip(theirs.chunks(4)) .filter(|(a, b)| a != b) .count(); assert_eq!(differing, 0, "{label}: {differing} pixels differ"); } #[test] fn layers_recompose_the_frame() { let root = Path::new(env!("CARGO_MANIFEST_DIR")); let Ok(roms) = load_directory(&root.join("rom/galaga")) else { eprintln!("skipped: no extracted set at rom/galaga"); return; }; let mut reference = Video::new(&roms); let runs: serde_json::Value = serde_json::from_str(&std::fs::read_to_string(root.join("oracle/runs.json")).unwrap()) .unwrap(); let run_def = runs["runs"] .as_array() .unwrap() .iter() .find(|r| r["name"] == "game-2400") .unwrap(); let inputs = parse_inputs(run_def["inputs"].as_str().unwrap()).unwrap(); let mut machine = Machine::new(&roms); let mut next_input = 0; let mut checked = 0; for frame in 0..2400u64 { machine.run_frame(); while next_input < inputs.len() && inputs[next_input].frame <= frame { let input = &inputs[next_input]; machine.set_control(&input.control, input.pressed).unwrap(); next_input += 1; } if frame % 5 == 0 { check( &machine, &roms, &mut reference, &format!("game-2400 frame {frame}"), ); checked += 1; } } // Later stages: the TAS movie's stretch around stages 11-12. let movie = Movie::from_json(&std::fs::read_to_string(root.join("movies/tas-seed1.json")).unwrap()) .unwrap(); let mut machine = Machine::new(&roms); machine.set_dips(movie.dsw_a, movie.dsw_b); for (frame, c) in movie.inputs().into_iter().enumerate().take(30_000) { machine.set_controls(c); machine.run_frame(); if frame >= 24_000 && frame % 10 == 0 { check( &machine, &roms, &mut reference, &format!("tas-seed1 frame {frame}"), ); checked += 1; } } eprintln!("{checked} frames recomposed exactly"); }