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};
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<Key>,
gamepads: ConnectedGamepadsIterator,
) -> bool {
pub fn update_pressed_keys(&mut self, keys: Vec<Key>, gamepads: &mut Gilrs) -> bool {
self.joypad.update_pressed_keys(keys, gamepads)
}

View file

@ -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));
}

View file

@ -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<Key>,
gamepads: ConnectedGamepadsIterator,
) -> bool {
pub fn update_pressed_keys(&mut self, keys: Vec<Key>, 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);