From 1e1e82a6e7d12a2fafff2770acd9c245ede81526 Mon Sep 17 00:00:00 2001 From: Alex Janka Date: Wed, 22 Feb 2023 13:28:19 +1100 Subject: [PATCH] joypad: trigger even if gamepad button held for less than 1 frame --- src/processor/memory.rs | 8 ++------ src/processor/memory/mmio/gpu.rs | 3 +-- src/processor/memory/mmio/joypad.rs | 27 ++++++++++++++++++++------- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/processor/memory.rs b/src/processor/memory.rs index 43cb09d..0bf73d3 100644 --- a/src/processor/memory.rs +++ b/src/processor/memory.rs @@ -1,7 +1,7 @@ use self::mmio::{Apu, Joypad, Serial}; pub use self::rom::Rom; use crate::{processor::SplitRegister, util::set_bit, verbose_println, Cpu}; -use gilrs::ConnectedGamepadsIterator; +use gilrs::Gilrs; use minifb::Key; pub mod mmio; @@ -163,11 +163,7 @@ impl Memory { self.io[addr_l] = masked_update(self.io[addr_l], data, mask); } - pub fn update_pressed_keys( - &mut self, - keys: Vec, - gamepads: ConnectedGamepadsIterator, - ) -> bool { + pub fn update_pressed_keys(&mut self, keys: Vec, gamepads: &mut Gilrs) -> bool { self.joypad.update_pressed_keys(keys, gamepads) } diff --git a/src/processor/memory/mmio/gpu.rs b/src/processor/memory/mmio/gpu.rs index 6ef5922..cf7ba1d 100644 --- a/src/processor/memory/mmio/gpu.rs +++ b/src/processor/memory/mmio/gpu.rs @@ -161,10 +161,9 @@ impl Cpu { } fn enter_vblank(&mut self) { - while self.gamepad_handler.next_event().is_some() {} if self .memory - .update_pressed_keys(self.window.get_keys(), self.gamepad_handler.gamepads()) + .update_pressed_keys(self.window.get_keys(), &mut self.gamepad_handler) { self.memory.set(0xFF0F, set_bit(self.memory.get(0xFF0F), 4)); } diff --git a/src/processor/memory/mmio/joypad.rs b/src/processor/memory/mmio/joypad.rs index b39538c..1dd2b04 100644 --- a/src/processor/memory/mmio/joypad.rs +++ b/src/processor/memory/mmio/joypad.rs @@ -1,5 +1,5 @@ use crate::util::{clear_bit, get_bit}; -use gilrs::{Button, ConnectedGamepadsIterator}; +use gilrs::{Button, Gilrs}; use minifb::Key; #[derive(Debug, Clone, Copy, PartialEq)] @@ -57,14 +57,27 @@ impl Joypad { self.sel_direction = !get_bit(data, 4); } - pub fn update_pressed_keys( - &mut self, - keys: Vec, - gamepads: ConnectedGamepadsIterator, - ) -> bool { + pub fn update_pressed_keys(&mut self, keys: Vec, gamepad_handler: &mut Gilrs) -> bool { let old = *self; self.clear_buttons(); - for (_, pad) in gamepads { + + while let Some(event) = gamepad_handler.next_event() { + if let gilrs::EventType::ButtonPressed(button, _) = event.event { + match button { + Button::DPadDown => self.down = true, + Button::DPadUp => self.up = true, + Button::DPadLeft => self.left = true, + Button::DPadRight => self.right = true, + Button::Start => self.start = true, + Button::Select => self.select = true, + Button::East => self.a = true, + Button::South => self.b = true, + _ => {} + } + } + } + + for (_, pad) in gamepad_handler.gamepads() { self.down |= pad.is_pressed(Button::DPadDown); self.up |= pad.is_pressed(Button::DPadUp); self.left |= pad.is_pressed(Button::DPadLeft);