@@ -137,6 +137,49 @@ Promise.all([wasmReady, documentReady]).then(async () => {
137137 el . style . touchAction = "manipulation" ;
138138 } ) ;
139139
140+ // ========================
141+ // Gamepad support
142+ // ========================
143+
144+ const GAMEPAD_DEADZONE = 0.25 ;
145+ const GAMEPAD_THRESHOLD = 0.5 ;
146+
147+ function readGamepadInput ( ) {
148+ const gamepads = navigator . getGamepads ( ) ;
149+ if ( ! gamepads || ! gamepads [ 0 ] ) return 0 ;
150+
151+ const gamepad = gamepads [ 0 ] ;
152+ let buttons = 0 ;
153+
154+ // D-pad (buttons 12-15 on standard gamepad)
155+ if ( gamepad . buttons [ 12 ] ?. pressed ) buttons |= BUTTON_UP ;
156+ if ( gamepad . buttons [ 13 ] ?. pressed ) buttons |= BUTTON_DOWN ;
157+ if ( gamepad . buttons [ 14 ] ?. pressed ) buttons |= BUTTON_LEFT ;
158+ if ( gamepad . buttons [ 15 ] ?. pressed ) buttons |= BUTTON_RIGHT ;
159+
160+ // Face buttons
161+ if ( gamepad . buttons [ 0 ] ?. pressed ) buttons |= BUTTON_A ; // Cross/A
162+ if ( gamepad . buttons [ 2 ] ?. pressed ) buttons |= BUTTON_B ; // Square/X
163+ if ( gamepad . buttons [ 8 ] ?. pressed ) buttons |= BUTTON_SELECT ; // Select
164+ if ( gamepad . buttons [ 9 ] ?. pressed ) buttons |= BUTTON_START ; // Start
165+
166+ // Left analog stick
167+ let leftX = gamepad . axes [ 0 ] || 0 ;
168+ let leftY = gamepad . axes [ 1 ] || 0 ;
169+
170+ // Apply deadzone
171+ if ( Math . abs ( leftX ) < GAMEPAD_DEADZONE ) leftX = 0 ;
172+ if ( Math . abs ( leftY ) < GAMEPAD_DEADZONE ) leftY = 0 ;
173+
174+ // Convert to digital input
175+ if ( leftX < - GAMEPAD_THRESHOLD ) buttons |= BUTTON_LEFT ;
176+ if ( leftX > GAMEPAD_THRESHOLD ) buttons |= BUTTON_RIGHT ;
177+ if ( leftY < - GAMEPAD_THRESHOLD ) buttons |= BUTTON_UP ;
178+ if ( leftY > GAMEPAD_THRESHOLD ) buttons |= BUTTON_DOWN ;
179+
180+ return buttons ;
181+ }
182+
140183 // ========================
141184 // ROM loading
142185 // ========================
@@ -189,7 +232,8 @@ Promise.all([wasmReady, documentReady]).then(async () => {
189232
190233 function executeFrame ( ) {
191234 while ( true ) {
192- let frameReady = go . RunFrame ( buttonsPressed ) ;
235+ let buttons = buttonsPressed | readGamepadInput ( ) ;
236+ let frameReady = go . RunFrame ( buttons ) ;
193237
194238 if ( frameReady ) {
195239 let framePtr = go . GetFrameBufferPtr ( ) ;
0 commit comments