Gamepad/Game Controller Input
It would be amazing to have a WASM (and native) game work with a game controller! 🎮
I saw one macroquad-compatible library in progress that claims to support wasm, but did not tried it myself: https://github.com/Bombfuse/gamepad
There is also: https://gitlab.com/gilrs-project/gilrs which as far as I can see could be compatible with macroquad.
@not-fl3 @ln42 I have been able to do a small test of Gilrs and cannot get it to work

Example code:
#[macroquad::main("Gamepad Test")]
async fn main()
{
use gilrs::{Gilrs, Button, Event};
let mut gilrs = Gilrs::new().unwrap();
// Iterate over all connected gamepads
for (_id, gamepad) in gilrs.gamepads() {
println!("{} is {:?}", gamepad.name(), gamepad.power_info());
}
let mut active_gamepad = None;
loop
{
// Examine new events
while let Some(Event { id, event, time }) = gilrs.next_event() {
println!("{:?} New event from {}: {:?}", time, id, event);
active_gamepad = Some(id);
}
// You can also use cached gamepad state
if let Some(gamepad) = active_gamepad.map(|id| gilrs.gamepad(id)) {
if gamepad.is_pressed(Button::South) {
println!("Button South is pressed (XBox - A, PS - X)");
}
}
use macroquad::prelude::*;
clear_background(GRAY);
draw_circle(64.0, 64.0, radius, WHITE);
if is_key_pressed(KeyCode::Escape) { break; }
next_frame().await
}
}1
# Cargo.toml
[target.'cfg(target_os = "wasm32")'.dependencies.gilrs]
version = "0.8.1"
features = ["wasm-bindgen"]
index.html:
<html lang="en">
<head>
<meta charset="utf-8">
<title>TITLE</title>
<style>
html,
body,
canvas {
margin: 0px;
padding: 0px;
width: 100%;
height: 100%;
overflow: hidden;
position: absolute;
background: black;
z-index: 0;
}
</style>
</head>
<body>
<canvas id="glcanvas" tabindex='1'></canvas>
<!-- Minified and statically hosted version of https://github.com/not-fl3/miniquad/blob/master/native/sapp-wasm/js/gl.js -->
<script src="https://not-fl3.github.io/miniquad-samples/gl.js"></script>
<script>load("/target/wasm32-unknown-unknown/debug/gamepad_test.wasm");</script> <!-- Your compiled wasm file -->
</body>
</html>
Anyone have any pointers on what this could be? Gilrs said to use the wasm-bindgen feature, but that resulted in the error screenshot above.
macroquad do not use wasm-bindgen
I just created the gamepads crate for this - see information on the README how to use this as a macroquad web plugin.
@fornwall thanks this is great! 👍