From f7fa101ce2156ee4c6e30f544cfd2a50f1941c20 Mon Sep 17 00:00:00 2001 From: Andy Killorin <37423245+Speedy6451@users.noreply.github.com> Date: Sat, 16 Sep 2023 15:57:45 -0500 Subject: [PATCH] added multi-input support --- pirates/src/lib.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pirates/src/lib.rs b/pirates/src/lib.rs index 8e81a7c..dab6f89 100644 --- a/pirates/src/lib.rs +++ b/pirates/src/lib.rs @@ -12,6 +12,8 @@ extern crate wee_alloc; #[cfg_attr(feature = "wasm", global_allocator)] static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; +extern crate alloc; +use alloc::vec::Vec; use noise::PerlinBuf; use core::sync::atomic::{AtomicU32, Ordering}; use libm; @@ -50,7 +52,7 @@ pub static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT]; #[no_mangle] pub static mut KEYCODE: [u8; 2] = [0; 2]; -static LAST_KEY: Mutex> = Mutex::new(None); +static INPUTS: Mutex> = Mutex::new(Vec::new()); static FRAME: AtomicU32 = AtomicU32::new(0); @@ -77,7 +79,7 @@ static BOAT: Mutex = Mutex::new(Boat { x: 0.0, y: 0.0, theta: 0.0, vel: 0. #[no_mangle] pub unsafe extern fn keyboard_input() { - *LAST_KEY.lock() = Some([KEYCODE[0] as u32, KEYCODE[1] as u32]); + INPUTS.lock().push([KEYCODE[0] as u32, KEYCODE[1] as u32]); } #[no_mangle] @@ -103,7 +105,7 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { //camera[0] += 1.0; - if let Some(key) = LAST_KEY.lock().take() { + while let Some(key) = INPUTS.lock().pop() { match key[0] { 38 => camera[1] -= 10.0*camera[2], // up 40 => camera[1] += 10.0*camera[2], // down