@@ -32,21 +32,54 @@ impl Video {
3232}
3333
3434/// debug helper for showing the current visual state.
35- /// hotkeys: 'q' closes window, 's' steps by a frame
35+ /// hotkeys: 'q' closes window, 's' steps by a frame, 'p' toggles autoplay
3636#[ allow( dead_code) ]
3737pub fn preview ( emu : & mut NesState ) {
38+ preview_base ( emu, false ) ;
39+ }
40+
41+ /// debug viewer with keyboard input
42+ #[ allow( dead_code) ]
43+ pub fn preview_input ( emu : & mut NesState ) {
44+ preview_base ( emu, true ) ;
45+ }
46+
47+ fn preview_base ( emu : & mut NesState , has_input : bool ) {
3848 let mut view = Video :: new ( ) ;
49+ let mut running = false ;
3950 view. window . set_key_repeat_rate ( 0.1 ) ;
51+ view. window . set_target_fps ( 60 ) ;
52+
4053 loop {
54+ if has_input {
55+ use crate :: input:: * ;
56+ let mut buttons = 0 ;
57+
58+ if view. window . is_key_down ( Key :: Up ) { buttons |= UP ; }
59+ if view. window . is_key_down ( Key :: Down ) { buttons |= DOWN ; }
60+ if view. window . is_key_down ( Key :: Left ) { buttons |= LEFT ; }
61+ if view. window . is_key_down ( Key :: Right ) { buttons |= RIGHT ; }
62+ if view. window . is_key_down ( Key :: V ) { buttons |= START ; }
63+ if view. window . is_key_down ( Key :: C ) { buttons |= SELECT ; }
64+ if view. window . is_key_down ( Key :: X ) { buttons |= A ; }
65+ if view. window . is_key_down ( Key :: Z ) { buttons |= B ; }
66+
67+ crate :: util:: set_controller_raw ( emu, buttons) ;
68+ }
69+
4170 if !view. window . is_open ( ) {
4271 break ;
4372 }
4473 if view. window . is_key_pressed ( Key :: Q , KeyRepeat :: No ) {
4574 break ;
4675 }
47- if view. window . is_key_pressed ( Key :: S , KeyRepeat :: Yes ) {
76+ if view. window . is_key_pressed ( Key :: P , KeyRepeat :: No ) {
77+ running = !running;
78+ }
79+ if running || view. window . is_key_pressed ( Key :: S , KeyRepeat :: Yes ) {
4880 emu. run_until_vblank ( ) ;
4981 }
82+
5083 view. render ( emu) ;
5184 }
5285}
0 commit comments