Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 33 additions & 1 deletion js/gl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1458,7 +1458,39 @@ var importObject = {
}
animation_frame_timeout = window.requestAnimationFrame(animation);
},
init_webgl
init_webgl,

// Gamepad support (W3C Gamepad API)
// https://w3c.github.io/gamepad/#remapping
// Returns highest occupied gamepad slot + 1 (not count of connected gamepads)
sapp_gamepad_count: function() {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
var count = 0;
for (var i = 0; i < gamepads.length && i < 4; i++) {
if (gamepads[i]) count = i + 1;
}
return count;
},
sapp_gamepad_connected: function(id) {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
if (id < 0 || id >= gamepads.length) return 0;
var gp = gamepads[id];
return (gp && gp.connected) ? 1 : 0;
},
sapp_gamepad_button: function(id, btn) {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
if (id < 0 || id >= gamepads.length) return 0;
var gp = gamepads[id];
if (!gp || !gp.connected || btn < 0 || btn >= gp.buttons.length) return 0;
return gp.buttons[btn].pressed ? 1 : 0;
},
sapp_gamepad_axis: function(id, axis) {
var gamepads = navigator.getGamepads ? navigator.getGamepads() : [];
if (id < 0 || id >= gamepads.length) return 0.0;
var gp = gamepads[id];
if (!gp || !gp.connected || axis < 0 || axis >= gp.axes.length) return 0.0;
return gp.axes[axis];
}
}
};

Expand Down
14 changes: 14 additions & 0 deletions src/native/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,20 @@ extern "C" {
pub fn sapp_schedule_update();
pub fn init_webgl(version: i32);
pub fn now() -> f64;

// Gamepad support (W3C Gamepad API)
// https://w3c.github.io/gamepad/#remapping
/// Returns the number of gamepad slots to check (0-4).
/// This is the highest occupied index + 1, not the count of connected gamepads.
pub fn sapp_gamepad_count() -> i32;
/// Returns 1 if gamepad at index is connected, 0 otherwise.
pub fn sapp_gamepad_connected(id: i32) -> i32;
/// Returns 1 if button is pressed, 0 otherwise.
/// Button indices follow W3C standard gamepad mapping.
pub fn sapp_gamepad_button(id: i32, btn: i32) -> i32;
/// Returns axis value (-1.0 to 1.0).
/// Axis indices: 0=LeftX, 1=LeftY, 2=RightX, 3=RightY
pub fn sapp_gamepad_axis(id: i32, axis: i32) -> f32;
}

unsafe fn show_mouse(shown: bool) {
Expand Down
Loading