//! `galaga::objects` places every sprite where the video pipeline draws it. //! For sampled frames of the game-2400 reference run, each slot is hidden //! in turn (X bit 9 set, as the game itself parks sprites) and the frame //! re-rendered; every pixel that changes must lie inside that slot's //! decoded box. Needs rom/galaga; skips without it. use std::path::Path; use emu_galaga::galaga::objects::objects; use emu_galaga::galaga::roms::load_directory; use emu_galaga::galaga::trace::parse_inputs; use emu_galaga::galaga::video::{Video, SCREEN_HEIGHT, SCREEN_WIDTH}; use emu_galaga::galaga::Machine; #[test] fn object_boxes_hold_their_sprites() { 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 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); // Stars off in the probe renders: they are not sprites and they scroll. let mut video = Video::new(&roms); let mut base = vec![0u8; SCREEN_WIDTH * SCREEN_HEIGHT * 4]; let mut probe = base.clone(); 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 < 1100 || frame % 10 != 0 { continue; } let latch = machine.videolatch_at_vblank_end & !0x20; let mut shared = machine.shared.borrow_mut(); video.render(&shared, latch); video.screen_rgba(&mut base); for object in objects(&shared) { let o = 0x380 + 2 * usize::from(object.slot); let saved = shared.ram3[o + 1]; shared.ram3[o + 1] |= 2; video.render(&shared, latch); video.screen_rgba(&mut probe); shared.ram3[o + 1] = saved; let mut changed = 0; for (i, (a, b)) in base.chunks(4).zip(probe.chunks(4)).enumerate() { if a == b { continue; } changed += 1; let (x, y) = ((i % SCREEN_WIDTH) as i32, (i / SCREEN_WIDTH) as i32); assert!( x >= object.x && x < object.x + object.w && y >= object.y && y < object.y + object.h, "frame {frame}: pixel ({x}, {y}) changed outside {object:?}" ); } if changed > 0 { checked += 1; } } } // Not vacuous: the run has formations, dives, shots and explosions. assert!(checked > 1000, "only {checked} visible sprites checked"); eprintln!("{checked} visible sprites inside their boxes"); }