macroquad icon indicating copy to clipboard operation
macroquad copied to clipboard

Gamepad/Game Controller Input

Open Pebaz opened this issue 5 years ago • 6 comments

It would be amazing to have a WASM (and native) game work with a game controller! 🎮

Pebaz avatar Dec 20 '20 21:12 Pebaz

I saw one macroquad-compatible library in progress that claims to support wasm, but did not tried it myself: https://github.com/Bombfuse/gamepad

not-fl3 avatar Dec 20 '20 22:12 not-fl3

There is also: https://gitlab.com/gilrs-project/gilrs which as far as I can see could be compatible with macroquad.

ln42 avatar Mar 30 '21 15:03 ln42

@not-fl3 @ln42 I have been able to do a small test of Gilrs and cannot get it to work

image

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.

Pebaz avatar Mar 30 '21 20:03 Pebaz

macroquad do not use wasm-bindgen

FuriouZz avatar May 04 '21 20:05 FuriouZz

I just created the gamepads crate for this - see information on the README how to use this as a macroquad web plugin.

fornwall avatar Sep 19 '23 12:09 fornwall

@fornwall thanks this is great! 👍

Pebaz avatar Sep 19 '23 15:09 Pebaz