Former-commit-id: bfa963ac13e76b7e3cf2eb2e23651eb3189d3cbb
Former-commit-id: 6602c3b0a22327d5ceb85063c9934b3490e1da01
This commit is contained in:
Michel Heily 2022-09-12 00:41:35 +03:00
parent 407818d32a
commit 0f64f07133
5 changed files with 35 additions and 32 deletions

View file

@ -76,10 +76,6 @@ impl IoDevices {
pub fn set_sysbus_ptr(&mut self, ptr: SysBusPtr) {
self.sysbus_ptr = ptr;
}
pub(crate) fn set_key_state(&mut self, state: u16) {
self.keyinput = state;
}
}
impl InterruptConnect for IoDevices {

View file

@ -1,4 +1,4 @@
#[derive(Debug, Primitive, PartialEq, Eq)]
#[derive(Debug, Primitive, PartialEq, Eq, Clone, Copy)]
#[repr(u8)]
pub enum Keys {
ButtonA = 0,

View file

@ -18,7 +18,6 @@ use crate::audio::{self, connector::AudioJNIConnector, thread::AudioThreadComman
struct Hardware {
sample_rate: i32,
audio_producer: Option<Producer<i16>>,
key_state: u16,
}
impl AudioInterface for Hardware {
@ -192,9 +191,8 @@ impl EmulatorContext {
let hw = Rc::new(RefCell::new(Hardware {
sample_rate: audio::util::get_sample_rate(env, audio_player),
audio_producer: None,
key_state: 0xffff,
}));
let mut gba = GameBoyAdvance::new(bios, gamepak, hw.clone(), hw.clone());
let mut gba = GameBoyAdvance::new(bios, gamepak, hw.clone());
if skip_bios != 0 {
info!("skipping bios");
gba.skip_bios();
@ -242,9 +240,8 @@ impl EmulatorContext {
let hw = Rc::new(RefCell::new(Hardware {
sample_rate: audio::util::get_sample_rate(env, audio_player),
audio_producer: None,
key_state: 0xffff,
}));
let gba = GameBoyAdvance::from_saved_state(&savestate, bios, rom, hw.clone(), hw.clone())
let gba = GameBoyAdvance::from_saved_state(&savestate, bios, rom, hw.clone())
.map_err(|e| {
format!(
"failed to create GameBoyAdvance from saved savestate, error {:?}",

View file

@ -11,10 +11,11 @@ use libretro_backend::{
use bit::BitIndex;
use unsafe_unwrap::UnsafeUnwrap;
use rustboyadvance_core::keypad::Keys as GbaButton;
use rustboyadvance_core::keypad::Keys as _GbaButton;
use rustboyadvance_core::prelude::*;
use rustboyadvance_utils::audio::AudioRingBuffer;
use std::ops::Deref;
use std::path::Path;
use std::cell::RefCell;
@ -42,21 +43,33 @@ struct RustBoyAdvanceCore {
audio: Option<Rc<RefCell<AudioDevice>>>,
}
fn set_button_state(key_state: &mut u16, button: JoypadButton, is_pressed: bool) {
let mapped_button = match button {
JoypadButton::A => GbaButton::ButtonA,
JoypadButton::B => GbaButton::ButtonB,
JoypadButton::Start => GbaButton::Start,
JoypadButton::Select => GbaButton::Select,
JoypadButton::Left => GbaButton::Left,
JoypadButton::Up => GbaButton::Up,
JoypadButton::Right => GbaButton::Right,
JoypadButton::Down => GbaButton::Down,
JoypadButton::L1 => GbaButton::ButtonL,
JoypadButton::R1 => GbaButton::ButtonR,
_ => unreachable!(),
};
key_state.set_bit(mapped_button as usize, !is_pressed);
#[repr(transparent)]
struct GbaButton(_GbaButton);
impl Deref for GbaButton {
type Target = _GbaButton;
fn deref(&self) -> &Self::Target {
return &self.0
}
}
impl From<JoypadButton> for GbaButton {
fn from(button: JoypadButton) -> Self {
let mapped = match button {
JoypadButton::A => _GbaButton::ButtonA,
JoypadButton::B => _GbaButton::ButtonB,
JoypadButton::Start => _GbaButton::Start,
JoypadButton::Select => _GbaButton::Select,
JoypadButton::Left => _GbaButton::Left,
JoypadButton::Up => _GbaButton::Up,
JoypadButton::Right => _GbaButton::Right,
JoypadButton::Down => _GbaButton::Down,
JoypadButton::L1 => _GbaButton::ButtonL,
JoypadButton::R1 => _GbaButton::ButtonR,
_ => panic!("unimplemented button {:?}", button),
};
GbaButton(mapped)
}
}
impl libretro_backend::Core for RustBoyAdvanceCore {
@ -125,17 +138,17 @@ impl libretro_backend::Core for RustBoyAdvanceCore {
let gba = unsafe { self.gba.as_mut().unsafe_unwrap() };
let audio = unsafe { self.audio.as_mut().unsafe_unwrap() };
let key_state = gba.borrow_mut().get_key_state_mut();
let key_state = gba.get_key_state_mut();
macro_rules! update_controllers {
( $( $button:ident ),+ ) => (
$(
set_button_state(key_state, JoypadButton::$button, handle.is_joypad_button_pressed( joypad_port, JoypadButton::$button ) );
key_state.set_bit(*GbaButton::from(JoypadButton::$button) as usize, !handle.is_joypad_button_pressed( joypad_port, JoypadButton::$button ));
)+
)
}
drop(key_state);
update_controllers!(A, B, Start, Select, Left, Up, Right, Down, L1, R1);
drop(key_state);
gba.frame();

View file

@ -37,7 +37,6 @@ mod input;
mod video;
use audio::{create_audio_player, create_dummy_player};
use input;
use video::{create_video_interface, SCREEN_HEIGHT, SCREEN_WIDTH};
use rustboyadvance_core::cartridge::BackupType;
@ -189,7 +188,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
} else {
Rc::new(RefCell::new(create_audio_player(&sdl_context)))
};
let input = Rc::new(RefCell::new(create_input()));
let mut savestate_path = get_savestate_path(&Path::new(&rom_path));
@ -212,7 +210,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
gamepak,
video.clone(),
audio.clone(),
input.clone(),
);
if skip_bios {