From 7101d14f3bcc0af29b8d51b1f5d58689fee1a571 Mon Sep 17 00:00:00 2001 From: Astatin Date: Sat, 14 Sep 2024 23:49:32 +0900 Subject: Add keyboard support & start supporting serial communication (but tetris doesn't work in 2p) --- src/gamepad.rs | 77 +++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 66 insertions(+), 11 deletions(-) (limited to 'src/gamepad.rs') diff --git a/src/gamepad.rs b/src/gamepad.rs index 8f05333..4ea82f7 100644 --- a/src/gamepad.rs +++ b/src/gamepad.rs @@ -1,11 +1,18 @@ -use crate::state::GBState; -use gilrs::{Button, GamepadId, Gilrs, Event}; +use crate::display::Display; +use gilrs::{Button, GamepadId, Gilrs}; +use minifb::Key; pub struct Gamepad { gilrs: Gilrs, gamepad_id: Option, } +pub trait Input { + fn update_events(&mut self); + fn get_action_gamepad_reg(&self) -> u8; + fn get_direction_gamepad_reg(&self) -> u8; +} + impl Gamepad { pub fn new() -> Self { let mut gilrs = Gilrs::new().unwrap(); @@ -18,24 +25,24 @@ impl Gamepad { None }; - Self { gilrs, gamepad_id } } - pub fn update_events(&mut self) { - while let Some(_) = self.gilrs.next_event() {} - } - - pub fn check_special_actions(&self, state: &mut GBState) { + pub fn check_special_actions(&self, is_debug: &mut bool) { if let Some(gamepad_id) = self.gamepad_id { if let Some(gamepad) = self.gilrs.connected_gamepad(gamepad_id) { - state.is_debug = gamepad.is_pressed(Button::West); + *is_debug = gamepad.is_pressed(Button::West); } } } +} - pub fn get_action_gamepad_reg(&self) -> u8 { +impl Input for Gamepad { + fn update_events(&mut self) { + while let Some(_) = self.gilrs.next_event() {} + } + fn get_action_gamepad_reg(&self) -> u8 { let mut res = 0xf; if let Some(gamepad_id) = self.gamepad_id { @@ -61,7 +68,7 @@ impl Gamepad { res } - pub fn get_direction_gamepad_reg(&self) -> u8 { + fn get_direction_gamepad_reg(&self) -> u8 { let mut res = 0xf; if let Some(gamepad_id) = self.gamepad_id { @@ -87,3 +94,51 @@ impl Gamepad { res } } + +impl Input for Display { + fn update_events(&mut self) {} + + fn get_action_gamepad_reg(&self) -> u8 { + let mut res = 0xf; + + if self.window.is_key_down(Key::A) { + res &= 0b1110; + } + + if self.window.is_key_down(Key::B) { + res &= 0b1101; + } + + if self.window.is_key_down(Key::Backspace) { + res &= 0b1011; + } + + if self.window.is_key_down(Key::Enter) { + res &= 0b0111; + } + + res + } + + fn get_direction_gamepad_reg(&self) -> u8 { + let mut res = 0xf; + + if self.window.is_key_down(Key::Right) { + res &= 0b1110; + } + + if self.window.is_key_down(Key::Left) { + res &= 0b1101; + } + + if self.window.is_key_down(Key::Up) { + res &= 0b1011; + } + + if self.window.is_key_down(Key::Down) { + res &= 0b0111; + } + + res + } +} -- cgit v1.2.3-70-g09d2