From 6f33b6d21700b6e82d452482d67a9242634c31bc Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Fri, 15 Sep 2023 12:51:00 -0500 Subject: [PATCH] digital gamepad support --- Cargo.lock | 133 ++++++++++++++++++++++++++++++++++++++++++++- client/Cargo.toml | 5 ++ client/src/main.rs | 30 ++++++++++ 3 files changed, 165 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bf390d6..537ce83 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -60,10 +60,27 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" name = "client" version = "0.1.0" dependencies = [ + "gilrs", "minifb", "pirates", ] +[[package]] +name = "core-foundation" +version = "0.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "194a7a9e6de53fa55116934067c844d9d749312f75c6f6d0980e8c252f8c2146" +dependencies = [ + "core-foundation-sys", + "libc", +] + +[[package]] +name = "core-foundation-sys" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa" + [[package]] name = "cty" version = "0.2.2" @@ -112,6 +129,12 @@ version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + [[package]] name = "futures" version = "0.3.28" @@ -201,6 +224,39 @@ dependencies = [ "slab", ] +[[package]] +name = "gilrs" +version = "0.10.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62fd19844d0eb919aca41d3e4ea0e0b6bf60e1e827558b101c269015b8f5f27a" +dependencies = [ + "fnv", + "gilrs-core", + "log", + "uuid", + "vec_map", +] + +[[package]] +name = "gilrs-core" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ccc99e9b8d63ffcaa334c4babfa31f46e156618a11f63efb6e8e6bcb37b830d" +dependencies = [ + "core-foundation", + "io-kit-sys", + "js-sys", + "libc", + "libudev-sys", + "log", + "nix 0.26.4", + "uuid", + "vec_map", + "wasm-bindgen", + "web-sys", + "windows", +] + [[package]] name = "instant" version = "0.1.12" @@ -213,6 +269,16 @@ dependencies = [ "web-sys", ] +[[package]] +name = "io-kit-sys" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b2d4429acc1deff0fbdece0325b4997bdb02b2c245ab7023fd5deca0f6348de" +dependencies = [ + "core-foundation-sys", + "mach2", +] + [[package]] name = "js-sys" version = "0.3.64" @@ -250,6 +316,16 @@ version = "0.2.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7012b1bbb0719e1097c47611d3898568c546d597c2e74d66f6087edd5233ff4" +[[package]] +name = "libudev-sys" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324" +dependencies = [ + "libc", + "pkg-config", +] + [[package]] name = "linux-raw-sys" version = "0.4.7" @@ -272,6 +348,15 @@ version = "0.4.20" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" +[[package]] +name = "mach2" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6d0d1830bcd151a6fc4aea1369af235b36c1528fe976b8ff678683c9995eade8" +dependencies = [ + "libc", +] + [[package]] name = "memchr" version = "2.6.3" @@ -351,6 +436,17 @@ dependencies = [ "memoffset", ] +[[package]] +name = "nix" +version = "0.26.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b" +dependencies = [ + "bitflags 1.3.2", + "cfg-if 1.0.0", + "libc", +] + [[package]] name = "nom" version = "7.1.3" @@ -629,6 +725,18 @@ version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "uuid" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79daa5ed5740825c40b389c5e50312b9c86df53fccd33f281df655642b43869d" + +[[package]] +name = "vec_map" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" + [[package]] name = "version-compare" version = "0.1.1" @@ -710,7 +818,7 @@ dependencies = [ "bitflags 1.3.2", "downcast-rs", "libc", - "nix", + "nix 0.24.3", "scoped-tls", "wayland-commons", "wayland-scanner", @@ -723,7 +831,7 @@ version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8691f134d584a33a6606d9d717b95c4fa20065605f798a3f350d78dced02a902" dependencies = [ - "nix", + "nix 0.24.3", "once_cell", "smallvec", "wayland-sys", @@ -735,7 +843,7 @@ version = "0.29.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6865c6b66f13d6257bef1cd40cbfe8ef2f150fb8ebbdb1e8e873455931377661" dependencies = [ - "nix", + "nix 0.24.3", "wayland-client", "xcursor", ] @@ -818,6 +926,25 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +[[package]] +name = "windows" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca229916c5ee38c2f2bc1e9d8f04df975b4bd93f9955dc69fabb5d91270045c9" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.51.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64" +dependencies = [ + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.48.0" diff --git a/client/Cargo.toml b/client/Cargo.toml index a4d2e49..8469945 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -4,7 +4,12 @@ version = "0.1.0" edition = "2021" description = "A game about wind" +[features] +default = ["gamepad"] +gamepad = ["gilrs"] + [dependencies] +gilrs = { version = "0.10.2", optional = true } minifb = "0.25.0" pirates = { path = "../pirates", default_features = false } diff --git a/client/src/main.rs b/client/src/main.rs index ad2fa8e..53272ea 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -1,8 +1,13 @@ use minifb::{Key, ScaleMode, Window, WindowOptions, Scale}; extern crate pirates; use pirates::{WIDTH, HEIGHT}; +#[cfg(feature = "gilrs")] +use gilrs::{Gilrs, Button, Event}; fn main() { + #[cfg(feature = "gilrs")] + let mut gilrs = Gilrs::new().unwrap(); + let mut window = Window::new( "Simple Sailing Simulator", WIDTH, @@ -28,6 +33,9 @@ fn main() { } } + #[cfg(feature = "gamepad")] + let mut gamepad_handle = None; + while window.is_open() && !window.is_key_down(Key::Escape) { buffer.clear(); unsafe { @@ -41,6 +49,28 @@ fn main() { } } + #[cfg(feature = "gamepad")] + if let Some(event) = gilrs.next_event() { + gamepad_handle = Some(event.id); + println!("gampad"); + } + + #[cfg(feature = "gamepad")] + if let Some(gamepad) = gamepad_handle.map(|h| gilrs.gamepad(h)) { + if gamepad.is_pressed(Button::DPadLeft) { + keyboard_input(65) + } + if gamepad.is_pressed(Button::DPadRight) { + keyboard_input(68) + } + if gamepad.is_pressed(Button::DPadUp) { + keyboard_input(61) + } + if gamepad.is_pressed(Button::DPadDown) { + keyboard_input(173) + } + } + window.get_keys().iter().for_each(|key| match key { Key::A => keyboard_input(65), Key::D => keyboard_input(68),