Skip to content

Commit

Permalink
assorted cleanup of input-related code and EmulatorTrait
Browse files Browse the repository at this point in the history
* replace the `define_controller_inputs` proc macro with a declarative macro
* move the MappableInputs<Button> trait from jgenesis-native-driver to jgenesis-common, and implement it in the backend cores
* add a Button associated type to EmulatorTrait, and add necessary trait bounds for InputMapper to EmulatorTrait::Button and EmulatorTrait::Inputs
* change NativeEmulator to only have a single generic type parameter Emulator, with a struct-level trait bound Emulator: EmulatorTrait
* remove now-unnecessary generic type parameters and trait bounds in a few places
  • Loading branch information
jsgroth committed Dec 27, 2024
1 parent 4cc5a0b commit 8dc9daa
Show file tree
Hide file tree
Showing 27 changed files with 387 additions and 734 deletions.
3 changes: 2 additions & 1 deletion backend/gb-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::bus::Bus;
use crate::cartridge::{Cartridge, SoftwareType};
use crate::dma::DmaUnit;
use crate::graphics::RgbaFrameBuffer;
use crate::inputs::{GameBoyInputs, InputState};
use crate::inputs::{GameBoyButton, GameBoyInputs, InputState};
use crate::interrupts::InterruptRegisters;
use crate::memory::Memory;
use crate::ppu::Ppu;
Expand Down Expand Up @@ -186,6 +186,7 @@ impl GameBoyEmulator {
}

impl EmulatorTrait for GameBoyEmulator {
type Button = GameBoyButton;
type Inputs = GameBoyInputs;
type Config = GameBoyEmulatorConfig;
type Err<
Expand Down
32 changes: 19 additions & 13 deletions backend/gb-core/src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@
use crate::interrupts::InterruptRegisters;
use crate::sm83::InterruptType;
use bincode::{Decode, Encode};
use jgenesis_common::define_controller_inputs;
use jgenesis_common::frontend::MappableInputs;
use jgenesis_common::input::Player;
use jgenesis_common::num::GetBit;
use jgenesis_proc_macros::define_controller_inputs;

define_controller_inputs! {
enum GameBoyButton {
Up,
Left,
Right,
Down,
A,
B,
Start,
Select,
}
buttons: GameBoyButton {
Up -> up,
Left -> left,
Right -> right,
Down -> down,
A -> a,
B -> b,
Start -> start,
Select -> select,
},
joypad: GameBoyInputs,
}

struct GameBoyInputs {
buttons!
impl MappableInputs<GameBoyButton> for GameBoyInputs {
#[inline]
fn set_field(&mut self, button: GameBoyButton, _player: Player, pressed: bool) {
self.set_button(button, pressed);
}
}

Expand Down
3 changes: 2 additions & 1 deletion backend/genesis-core/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Genesis public interface and main loop
use crate::audio::{GenesisAudioResampler, LowPassFilter};
use crate::input::{GenesisInputs, InputState};
use crate::input::{GenesisButton, GenesisInputs, InputState};
use crate::memory::{Cartridge, MainBus, MainBusSignals, MainBusWrites, Memory};
use crate::timing::{CycleCounters, GenesisCycleCounters};
use crate::vdp::{TimingModeExt, Vdp, VdpConfig, VdpTickEffect};
Expand Down Expand Up @@ -335,6 +335,7 @@ pub fn render_frame<R: Renderer>(
}

impl EmulatorTrait for GenesisEmulator {
type Button = GenesisButton;
type Inputs = GenesisInputs;
type Config = GenesisEmulatorConfig;

Expand Down
47 changes: 23 additions & 24 deletions backend/genesis-core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,32 @@
use crate::GenesisEmulatorConfig;
use bincode::{Decode, Encode};
use jgenesis_common::define_controller_inputs;
use jgenesis_common::num::GetBit;
use jgenesis_proc_macros::{EnumAll, EnumDisplay, define_controller_inputs};
use jgenesis_proc_macros::{EnumAll, EnumDisplay};

define_controller_inputs! {
enum GenesisButton {
Up,
Left,
Right,
Down,
A,
B,
C,
X,
Y,
Z,
Start,
Mode,
}

struct GenesisJoypadState {
buttons!
}

struct GenesisInputs {
p1: Player::One,
p2: Player::Two,
}
buttons: GenesisButton {
Up -> up,
Left -> left,
Right -> right,
Down -> down,
A -> a,
B -> b,
C -> c,
X -> x,
Y -> y,
Z -> z,
Start -> start,
Mode -> mode,
},
joypad: GenesisJoypadState,
inputs: GenesisInputs {
players: {
p1: Player::One,
p2: Player::Two,
},
},
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Encode, Decode, EnumDisplay, EnumAll)]
Expand Down
3 changes: 2 additions & 1 deletion backend/nes-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::bus::cartridge::CartridgeFileError;
use crate::bus::{Bus, cartridge};
use crate::cpu::CpuState;
use crate::graphics::TimingModeGraphicsExt;
use crate::input::NesInputs;
use crate::input::{NesButton, NesInputs};
use crate::ppu::PpuState;
use crate::{apu, audio, cpu, graphics, ppu};
use bincode::{Decode, Encode};
Expand Down Expand Up @@ -277,6 +277,7 @@ fn new_rgba_frame_buffer() -> Vec<Color> {
}

impl EmulatorTrait for NesEmulator {
type Button = NesButton;
type Inputs = NesInputs;
type Config = NesEmulatorConfig;
type Err<
Expand Down
63 changes: 42 additions & 21 deletions backend/nes-core/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
use bincode::{Decode, Encode};
use jgenesis_common::define_controller_inputs;
use jgenesis_common::frontend::{DisplayArea, FrameSize, MappableInputs};
use jgenesis_common::input::Player;
use jgenesis_proc_macros::define_controller_inputs;

define_controller_inputs! {
enum NesButton {
Up,
Left,
Right,
Down,
A,
B,
Start,
Select,
#[on_console]
ZapperFire,
#[on_console]
ZapperForceOffscreen,
}

struct NesJoypadState {
buttons!
}
buttons: NesButton {
Up -> up,
Left -> left,
Right -> right,
Down -> down,
A -> a,
B -> b,
Start -> start,
Select -> select,
},
non_gamepad_buttons: [ZapperFire, ZapperForceOffscreen],
joypad: NesJoypadState,
}

impl NesButton {
Expand Down Expand Up @@ -100,9 +95,9 @@ pub struct NesInputs {
pub p2: NesInputDevice,
}

impl NesInputs {
impl MappableInputs<NesButton> for NesInputs {
#[inline]
pub fn set_button(&mut self, button: NesButton, player: Player, pressed: bool) {
fn set_field(&mut self, button: NesButton, player: Player, pressed: bool) {
match (button, player) {
(NesButton::ZapperFire | NesButton::ZapperForceOffscreen, _) => {
if let NesInputDevice::Zapper(zapper_state) = &mut self.p2 {
Expand All @@ -121,6 +116,32 @@ impl NesInputs {
}
}
}

#[inline]
fn handle_mouse_motion(
&mut self,
x: i32,
y: i32,
frame_size: FrameSize,
display_area: DisplayArea,
) {
if let NesInputDevice::Zapper(zapper_state) = &mut self.p2 {
zapper_state.position = jgenesis_common::input::viewport_position_to_frame_position(
x,
y,
frame_size,
display_area,
);
log::debug!("Set Zapper position to {:?}", zapper_state.position);
}
}

#[inline]
fn handle_mouse_leave(&mut self) {
if let NesInputDevice::Zapper(zapper_state) = &mut self.p2 {
zapper_state.position = None;
}
}
}

#[derive(Debug, Clone, Copy, Encode, Decode)]
Expand Down
3 changes: 2 additions & 1 deletion backend/s32x-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use crate::audio::Sega32XResampler;
use crate::core::Sega32X;
use bincode::{Decode, Encode};
use genesis_core::input::InputState;
use genesis_core::input::{GenesisButton, InputState};
use genesis_core::memory::{MainBus, MainBusSignals, MainBusWrites, Memory};
use genesis_core::timing::GenesisCycleCounters;
use genesis_core::vdp::{Vdp, VdpTickEffect};
Expand Down Expand Up @@ -177,6 +177,7 @@ impl Sega32XEmulator {
}

impl EmulatorTrait for Sega32XEmulator {
type Button = GenesisButton;
type Inputs = GenesisInputs;
type Config = Sega32XEmulatorConfig;
type Err<
Expand Down
3 changes: 2 additions & 1 deletion backend/segacd-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::rf5c164::Rf5c164;
use bincode::{Decode, Encode};
use cdrom::CdRomError;
use cdrom::reader::{CdRom, CdRomFileFormat};
use genesis_core::input::InputState;
use genesis_core::input::{GenesisButton, InputState};
use genesis_core::memory::{MainBus, MainBusSignals, MainBusWrites, Memory};
use genesis_core::timing::CycleCounters;
use genesis_core::vdp::{Vdp, VdpTickEffect};
Expand Down Expand Up @@ -336,6 +336,7 @@ impl SegaCdEmulator {
}

impl EmulatorTrait for SegaCdEmulator {
type Button = GenesisButton;
type Inputs = GenesisInputs;
type Config = SegaCdEmulatorConfig;

Expand Down
3 changes: 2 additions & 1 deletion backend/smsgg-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::input::InputState;
use crate::memory::Memory;
use crate::psg::{Sn76489, Sn76489TickEffect, Sn76489Version};
use crate::vdp::{Vdp, VdpBuffer, VdpTickEffect};
use crate::{SmsGgInputs, VdpVersion, vdp};
use crate::{SmsGgButton, SmsGgInputs, VdpVersion, vdp};
use bincode::{Decode, Encode};
use jgenesis_common::frontend::{
AudioOutput, Color, EmulatorTrait, FrameSize, PartialClone, PixelAspectRatio, Renderer,
Expand Down Expand Up @@ -327,6 +327,7 @@ fn determine_aspect_ratio(
}

impl EmulatorTrait for SmsGgEmulator {
type Button = SmsGgButton;
type Inputs = SmsGgInputs;
type Config = SmsGgEmulatorConfig;

Expand Down
39 changes: 18 additions & 21 deletions backend/smsgg-core/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@
use crate::api::SmsRegion;
use bincode::{Decode, Encode};
use jgenesis_common::define_controller_inputs;
use jgenesis_common::num::GetBit;
use jgenesis_proc_macros::define_controller_inputs;

define_controller_inputs! {
enum SmsGgButton {
Up,
Left,
Right,
Down,
Button1,
Button2,
#[on_console]
Pause,
}

struct SmsGgJoypadState {
buttons!
}

struct SmsGgInputs {
p1: Player::One,
p2: Player::Two,
pause: Button::Pause,
}
buttons: SmsGgButton {
Up -> up,
Left -> left,
Right -> right,
Down -> down,
Button1 -> button1,
Button2 -> button2,
},
non_gamepad_buttons: [Pause],
joypad: SmsGgJoypadState,
inputs: SmsGgInputs {
players: {
p1: Player::One,
p2: Player::Two,
},
buttons: [Pause -> pause],
},
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Encode, Decode)]
Expand Down
3 changes: 2 additions & 1 deletion backend/snes-core/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::apu::{Apu, ApuTickEffect};
use crate::audio::AudioResampler;
use crate::bus::Bus;
use crate::input::SnesInputs;
use crate::input::{SnesButton, SnesInputs};
use crate::memory::dma::{DmaStatus, DmaUnit};
use crate::memory::{CpuInternalRegisters, Memory};
use crate::ppu::{Ppu, PpuTickEffect};
Expand Down Expand Up @@ -276,6 +276,7 @@ impl SnesEmulator {
}

impl EmulatorTrait for SnesEmulator {
type Button = SnesButton;
type Inputs = SnesInputs;
type Config = SnesEmulatorConfig;

Expand Down
Loading

0 comments on commit 8dc9daa

Please sign in to comment.