1
Fork 0

changed to using an enum for input types

caveat: num_traits needs std
This commit is contained in:
Andy Killorin 2023-09-16 16:47:50 -05:00
parent 42843165fc
commit 2fcf30c819
Signed by: ank
GPG key ID: B6241CA3B552BCA4
4 changed files with 80 additions and 39 deletions

13
Cargo.lock generated
View file

@ -530,6 +530,17 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "num-derive"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9e6a0fd4f737c707bd9086cc16c925f294943eb62eb71499e9fd4cf71f8b9f4e"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]] [[package]]
name = "num-integer" name = "num-integer"
version = "0.1.45" version = "0.1.45"
@ -613,6 +624,8 @@ version = "0.1.0"
dependencies = [ dependencies = [
"libm", "libm",
"nalgebra", "nalgebra",
"num-derive",
"num-traits",
"rayon", "rayon",
"spin", "spin",
"wee_alloc", "wee_alloc",

View file

@ -1,8 +1,9 @@
use minifb::{Key, ScaleMode, Window, WindowOptions, Scale}; use minifb::{Key, ScaleMode, Window, WindowOptions, Scale};
extern crate pirates; extern crate pirates;
use pirates::{WIDTH, HEIGHT}; use pirates::{WIDTH, HEIGHT, Input};
#[cfg(feature = "gilrs")] #[cfg(feature = "gilrs")]
use gilrs::{Axis, Gilrs, Button}; use gilrs::{Axis, Gilrs, Button};
use pirates::Input::*;
fn main() { fn main() {
#[cfg(feature = "gilrs")] #[cfg(feature = "gilrs")]
@ -26,17 +27,17 @@ fn main() {
let mut buffer: Vec<u32> = Vec::with_capacity(WIDTH * HEIGHT); let mut buffer: Vec<u32> = Vec::with_capacity(WIDTH * HEIGHT);
fn keyboard_input(key: u8) { fn keyboard_input(key: Input) {
unsafe { unsafe {
pirates::KEYCODE[0] = key; pirates::KEYCODE[0] = key as u8;
pirates::keyboard_input(); pirates::keyboard_input();
} }
} }
fn analog_input(chan: u8, val: f32) { fn analog_input(chan: Input, val: f32) {
let val = (val * 127.0) as i8; let val = (val * 127.0) as i8;
unsafe { unsafe {
pirates::KEYCODE[0] = chan; pirates::KEYCODE[0] = chan as u8;
pirates::KEYCODE[1] = (val + 127) as u8; pirates::KEYCODE[1] = (val + 127) as u8;
pirates::keyboard_input(); pirates::keyboard_input();
} }
@ -66,43 +67,43 @@ fn main() {
#[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)) {
gamepad.axis_data(Axis::LeftStickX).map(|axis| { gamepad.axis_data(Axis::LeftStickX).map(|axis| {
analog_input(1, axis.value()); analog_input(AxisRudder, axis.value());
}); });
if gamepad.is_pressed(Button::RightTrigger) { if gamepad.is_pressed(Button::RightTrigger) {
gamepad.axis_data(Axis::RightStickY).map(|axis| { gamepad.axis_data(Axis::RightStickY).map(|axis| {
analog_input(3, dbg!(axis.value())); analog_input(AxisPanY, axis.value());
}); });
gamepad.axis_data(Axis::RightStickX).map(|axis| { gamepad.axis_data(Axis::RightStickX).map(|axis| {
analog_input(4, dbg!(axis.value())); analog_input(AxisPanX, axis.value());
}); });
} else { } else {
gamepad.axis_data(Axis::RightStickY).map(|axis| { gamepad.axis_data(Axis::RightStickY).map(|axis| {
analog_input(0, dbg!(axis.value())); analog_input(AxisZoom, axis.value());
}); });
} }
if gamepad.is_pressed(Button::DPadLeft) { if gamepad.is_pressed(Button::DPadLeft) {
keyboard_input(65) keyboard_input(PanLeft)
} }
if gamepad.is_pressed(Button::DPadRight) { if gamepad.is_pressed(Button::DPadRight) {
keyboard_input(68) keyboard_input(PanRight)
} }
if gamepad.is_pressed(Button::DPadUp) { if gamepad.is_pressed(Button::DPadUp) {
keyboard_input(61) keyboard_input(ZoomIn)
} }
if gamepad.is_pressed(Button::DPadDown) { if gamepad.is_pressed(Button::DPadDown) {
keyboard_input(173) keyboard_input(ZoomOut)
} }
} }
window.get_keys().iter().for_each(|key| match key { window.get_keys().iter().for_each(|key| match key {
Key::A => keyboard_input(65), Key::A => keyboard_input(RudderLeft),
Key::D => keyboard_input(68), Key::D => keyboard_input(RudderRight),
Key::Equal => keyboard_input(61), Key::Equal => keyboard_input(ZoomIn),
Key::Minus => keyboard_input(173), Key::Minus => keyboard_input(ZoomOut),
Key::Up => keyboard_input(38), Key::Up => keyboard_input(PanUp),
Key::Down => keyboard_input(40), Key::Down => keyboard_input(PanDown),
Key::Left => keyboard_input(37), Key::Left => keyboard_input(PanLeft),
Key::Right => keyboard_input(39), Key::Right => keyboard_input(PanRight),
_ => (), _ => (),
}); });

View file

@ -16,6 +16,8 @@ crate-type = ["lib", "cdylib"]
[dependencies] [dependencies]
libm = "0.2.7" libm = "0.2.7"
num-derive = "0.4.0"
num-traits = "0.2.16"
spin = "0.9.8" spin = "0.9.8"
[dependencies.rayon] [dependencies.rayon]

View file

@ -15,6 +15,8 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
extern crate alloc; extern crate alloc;
use alloc::vec::Vec; use alloc::vec::Vec;
use noise::PerlinBuf; use noise::PerlinBuf;
use num_traits::FromPrimitive;
use num_derive::FromPrimitive;
use core::sync::atomic::{AtomicU32, Ordering}; use core::sync::atomic::{AtomicU32, Ordering};
use libm; use libm;
extern crate nalgebra as na; extern crate nalgebra as na;
@ -23,8 +25,6 @@ use spin::Mutex;
#[cfg(feature = "rayon")] #[cfg(feature = "rayon")]
use rayon::prelude::*; use rayon::prelude::*;
use crate::noise::lerp;
mod sampler; mod sampler;
mod noise; mod noise;
@ -108,20 +108,20 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
//camera[0] += 1.0; //camera[0] += 1.0;
while let Some(key) = INPUTS.lock().pop() { while let Some(key) = INPUTS.lock().pop() {
match key[0] { use Input::*;
38 => camera[1] -= 10.0*camera[2], // up match FromPrimitive::from_u32(key[0]).unwrap() {
40 => camera[1] += 10.0*camera[2], // down PanUp => camera[1] -= 10.0*camera[2], // up
37 => camera[0] -= 10.0*camera[2], // left PanDown => camera[1] += 10.0*camera[2], // down
39 => camera[0] += 10.0*camera[2], // right PanLeft => camera[0] -= 10.0*camera[2], // left
61 => camera[2] *= 0.9, // + PanRight => camera[0] += 10.0*camera[2], // right
173 => camera[2] *= 1.1, // - ZoomIn => camera[2] *= 0.9, // +
65 => boat.theta -= 10.0, // A ZoomOut => camera[2] *= 1.1, // -
68 => boat.theta += 10.0, // D RudderLeft => boat.theta -= 10.0, // A
0 => camera[2] *= 1.0 - (key[1] as f32 - 127.0) * 0.0004, // analog zoom RudderRight => boat.theta += 10.0, // D
1 => boat.theta += (key[1] as f32 - 127.0) * 0.031, // analog rudder AxisZoom => camera[2] *= 1.0 - (key[1] as f32 - 127.0) * 0.0004, // analog zoom
3 => camera[1] -= (key[1] as f32 - 127.0) * 0.004, // pan[y] AxisRudder => boat.theta += (key[1] as f32 - 127.0) * 0.031, // analog rudder
4 => camera[0] += (key[1] as f32 - 127.0) * 0.004, // pan[x] AxisPanY => camera[1] -= (key[1] as f32 - 127.0) * 0.004, // pan[y]
_ => {} AxisPanX => camera[0] += (key[1] as f32 - 127.0) * 0.004, // pan[x]
} }
} }
@ -146,9 +146,9 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
const HALF: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0); const HALF: Vector2<f32> = Vector2::new(WIDTH as f32 / 2.0, HEIGHT as f32 / 2.0);
#[cfg(feature = "rayon")] #[cfg(feature = "rayon")]
let mut buffer_iter = buffer.par_iter_mut(); let buffer_iter = buffer.par_iter_mut();
#[cfg(not(feature = "rayon"))] #[cfg(not(feature = "rayon"))]
let mut buffer_iter = buffer.iter_mut(); let buffer_iter = buffer.iter_mut();
buffer_iter.enumerate().for_each(|pix| { buffer_iter.enumerate().for_each(|pix| {
let y = pix.0 / WIDTH; let y = pix.0 / WIDTH;
@ -293,3 +293,28 @@ impl Boat {
} }
} }
#[derive(FromPrimitive)]
#[repr(u8)]
pub enum Input {
/// Up Arrow
PanUp = 38,
/// Down Arrow
PanDown = 40,
/// Left Arrow
PanLeft = 37,
/// Right Arrow
PanRight = 39,
/// +
ZoomIn = 61,
/// -
ZoomOut = 173,
/// A
RudderLeft = 65,
/// D
RudderRight = 68,
AxisZoom = 0,
AxisRudder = 1,
AxisPanY = 3,
AxisPanX = 4,
}