joypad: trigger even if gamepad button held for less than 1 frame

This commit is contained in:
Alex Janka 2023-02-22 13:28:19 +11:00
parent ac3b197d6d
commit 1e1e82a6e7
3 changed files with 23 additions and 15 deletions

View file

@ -1,7 +1,7 @@
use self::mmio::{Apu, Joypad, Serial}; use self::mmio::{Apu, Joypad, Serial};
pub use self::rom::Rom; pub use self::rom::Rom;
use crate::{processor::SplitRegister, util::set_bit, verbose_println, Cpu}; use crate::{processor::SplitRegister, util::set_bit, verbose_println, Cpu};
use gilrs::ConnectedGamepadsIterator; use gilrs::Gilrs;
use minifb::Key; use minifb::Key;
pub mod mmio; pub mod mmio;
@ -163,11 +163,7 @@ impl Memory {
self.io[addr_l] = masked_update(self.io[addr_l], data, mask); self.io[addr_l] = masked_update(self.io[addr_l], data, mask);
} }
pub fn update_pressed_keys( pub fn update_pressed_keys(&mut self, keys: Vec<Key>, gamepads: &mut Gilrs) -> bool {
&mut self,
keys: Vec<Key>,
gamepads: ConnectedGamepadsIterator,
) -> bool {
self.joypad.update_pressed_keys(keys, gamepads) self.joypad.update_pressed_keys(keys, gamepads)
} }

View file

@ -161,10 +161,9 @@ impl Cpu {
} }
fn enter_vblank(&mut self) { fn enter_vblank(&mut self) {
while self.gamepad_handler.next_event().is_some() {}
if self if self
.memory .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)); self.memory.set(0xFF0F, set_bit(self.memory.get(0xFF0F), 4));
} }

View file

@ -1,5 +1,5 @@
use crate::util::{clear_bit, get_bit}; use crate::util::{clear_bit, get_bit};
use gilrs::{Button, ConnectedGamepadsIterator}; use gilrs::{Button, Gilrs};
use minifb::Key; use minifb::Key;
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq)]
@ -57,14 +57,27 @@ impl Joypad {
self.sel_direction = !get_bit(data, 4); self.sel_direction = !get_bit(data, 4);
} }
pub fn update_pressed_keys( pub fn update_pressed_keys(&mut self, keys: Vec<Key>, gamepad_handler: &mut Gilrs) -> bool {
&mut self,
keys: Vec<Key>,
gamepads: ConnectedGamepadsIterator,
) -> bool {
let old = *self; let old = *self;
self.clear_buttons(); 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.down |= pad.is_pressed(Button::DPadDown);
self.up |= pad.is_pressed(Button::DPadUp); self.up |= pad.is_pressed(Button::DPadUp);
self.left |= pad.is_pressed(Button::DPadLeft); self.left |= pad.is_pressed(Button::DPadLeft);