1
Fork 0

analog support

This commit is contained in:
Andy Killorin 2023-09-16 15:10:41 -05:00
parent 6f33b6d217
commit 41d8def875
Signed by: ank
GPG key ID: B6241CA3B552BCA4
2 changed files with 19 additions and 4 deletions

View file

@ -2,7 +2,7 @@ use minifb::{Key, ScaleMode, Window, WindowOptions, Scale};
extern crate pirates; extern crate pirates;
use pirates::{WIDTH, HEIGHT}; use pirates::{WIDTH, HEIGHT};
#[cfg(feature = "gilrs")] #[cfg(feature = "gilrs")]
use gilrs::{Gilrs, Button, Event}; use gilrs::{Axis, Gilrs, Button};
fn main() { fn main() {
#[cfg(feature = "gilrs")] #[cfg(feature = "gilrs")]
@ -33,6 +33,14 @@ fn main() {
} }
} }
fn analog_input(chan: u8, val: i8) {
unsafe {
pirates::KEYCODE[0] = chan;
pirates::KEYCODE[1] = (val + 127) as u8;
pirates::keyboard_input();
}
}
#[cfg(feature = "gamepad")] #[cfg(feature = "gamepad")]
let mut gamepad_handle = None; let mut gamepad_handle = None;
@ -50,13 +58,20 @@ fn main() {
} }
#[cfg(feature = "gamepad")] #[cfg(feature = "gamepad")]
if let Some(event) = gilrs.next_event() { while let Some(event) = gilrs.next_event() {
gamepad_handle = Some(event.id); gamepad_handle = Some(event.id);
println!("gampad"); println!("gampad");
} }
#[cfg(feature = "gamepad")] #[cfg(feature = "gamepad")]
if let Some(gamepad) = gamepad_handle.map(|h| gilrs.gamepad(h)) { if let Some(gamepad) = gamepad_handle.map(|h| gilrs.gamepad(h)) {
let val = gamepad.state()
.axis_data(gamepad.axis_code(Axis::LeftStickX).unwrap());
if val.is_some() {
let val = dbg!(val.unwrap().value() * 127.0) as i8;
dbg!(val);
analog_input(1, val);
}
if gamepad.is_pressed(Button::DPadLeft) { if gamepad.is_pressed(Button::DPadLeft) {
keyboard_input(65) keyboard_input(65)
} }

View file

@ -75,8 +75,7 @@ static BOAT: Mutex<Boat> = Mutex::new(Boat { x: 0.0, y: 0.0, theta: 0.0, vel: 0.
#[no_mangle] #[no_mangle]
pub unsafe extern fn keyboard_input() { pub unsafe extern fn keyboard_input() {
let keycode = KEYCODE[0]; *LAST_KEY.lock() = Some([KEYCODE[0] as u32, KEYCODE[1] as u32]);
*LAST_KEY.lock() = Some([keycode as u32, 0]);
} }
#[no_mangle] #[no_mangle]
@ -112,6 +111,7 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
173 => camera[2] *= 1.1, // - 173 => camera[2] *= 1.1, // -
65 => boat.theta -= 10.0, // A 65 => boat.theta -= 10.0, // A
68 => boat.theta += 10.0, // D 68 => boat.theta += 10.0, // D
1 => boat.theta += (key[1] as f32 - 127.0) * 0.031, // analog rudder
_ => {} _ => {}
} }
} }