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",
]
[[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]]
name = "num-integer"
version = "0.1.45"
@ -613,6 +624,8 @@ version = "0.1.0"
dependencies = [
"libm",
"nalgebra",
"num-derive",
"num-traits",
"rayon",
"spin",
"wee_alloc",

View file

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

View file

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

View file

@ -15,6 +15,8 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
extern crate alloc;
use alloc::vec::Vec;
use noise::PerlinBuf;
use num_traits::FromPrimitive;
use num_derive::FromPrimitive;
use core::sync::atomic::{AtomicU32, Ordering};
use libm;
extern crate nalgebra as na;
@ -23,8 +25,6 @@ use spin::Mutex;
#[cfg(feature = "rayon")]
use rayon::prelude::*;
use crate::noise::lerp;
mod sampler;
mod noise;
@ -108,20 +108,20 @@ fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) {
//camera[0] += 1.0;
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
37 => camera[0] -= 10.0*camera[2], // left
39 => camera[0] += 10.0*camera[2], // right
61 => camera[2] *= 0.9, // +
173 => camera[2] *= 1.1, // -
65 => boat.theta -= 10.0, // A
68 => boat.theta += 10.0, // D
0 => camera[2] *= 1.0 - (key[1] as f32 - 127.0) * 0.0004, // analog zoom
1 => boat.theta += (key[1] as f32 - 127.0) * 0.031, // analog rudder
3 => camera[1] -= (key[1] as f32 - 127.0) * 0.004, // pan[y]
4 => camera[0] += (key[1] as f32 - 127.0) * 0.004, // pan[x]
_ => {}
use Input::*;
match FromPrimitive::from_u32(key[0]).unwrap() {
PanUp => camera[1] -= 10.0*camera[2], // up
PanDown => camera[1] += 10.0*camera[2], // down
PanLeft => camera[0] -= 10.0*camera[2], // left
PanRight => camera[0] += 10.0*camera[2], // right
ZoomIn => camera[2] *= 0.9, // +
ZoomOut => camera[2] *= 1.1, // -
RudderLeft => boat.theta -= 10.0, // A
RudderRight => boat.theta += 10.0, // D
AxisZoom => camera[2] *= 1.0 - (key[1] as f32 - 127.0) * 0.0004, // analog zoom
AxisRudder => boat.theta += (key[1] as f32 - 127.0) * 0.031, // analog rudder
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);
#[cfg(feature = "rayon")]
let mut buffer_iter = buffer.par_iter_mut();
let buffer_iter = buffer.par_iter_mut();
#[cfg(not(feature = "rayon"))]
let mut buffer_iter = buffer.iter_mut();
let buffer_iter = buffer.iter_mut();
buffer_iter.enumerate().for_each(|pix| {
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,
}