Skip to content

Commit

Permalink
Implement controller support for the SDL2 frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
xTibor authored and michelhe committed May 11, 2020
1 parent 21cb7f8 commit f46f42d
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "external/gba-suite"]
path = external/gba-suite
url = https://github.com/jsmolka/gba-suite.git
[submodule "external/SDL_GameControllerDB"]
path = external/SDL_GameControllerDB
url = https://github.com/gabomdq/SDL_GameControllerDB
1 change: 1 addition & 0 deletions external/SDL_GameControllerDB
Submodule SDL_GameControllerDB added at 14cb29
30 changes: 30 additions & 0 deletions platform/rustboyadvance-sdl2/src/input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use sdl2::keyboard::Scancode;
use sdl2::controller::Button;

use rustboyadvance_core::keypad as gba_keypad;
use rustboyadvance_core::InputInterface;
Expand All @@ -22,11 +23,24 @@ impl Sdl2Input {
self.keyinput.set_bit(key as usize, false);
}
}

pub fn on_keyboard_key_up(&mut self, scancode: Scancode) {
if let Some(key) = scancode_to_keypad(scancode) {
self.keyinput.set_bit(key as usize, true);
}
}

pub fn on_controller_button_down(&mut self, button: Button) {
if let Some(key) = controller_button_to_keypad(button) {
self.keyinput.set_bit(key as usize, false);
}
}

pub fn on_controller_button_up(&mut self, button: Button) {
if let Some(key) = controller_button_to_keypad(button) {
self.keyinput.set_bit(key as usize, true);
}
}
}

fn scancode_to_keypad(scancode: Scancode) -> Option<gba_keypad::Keys> {
Expand All @@ -45,6 +59,22 @@ fn scancode_to_keypad(scancode: Scancode) -> Option<gba_keypad::Keys> {
}
}

fn controller_button_to_keypad(button: Button) -> Option<gba_keypad::Keys> {
match button {
Button::DPadUp => Some(gba_keypad::Keys::Up),
Button::DPadDown => Some(gba_keypad::Keys::Down),
Button::DPadLeft => Some(gba_keypad::Keys::Left),
Button::DPadRight => Some(gba_keypad::Keys::Right),
Button::A => Some(gba_keypad::Keys::ButtonB), // A and B are swapped compared to the SDL layout
Button::B => Some(gba_keypad::Keys::ButtonA),
Button::Start => Some(gba_keypad::Keys::Start),
Button::Back => Some(gba_keypad::Keys::Select),
Button::LeftShoulder => Some(gba_keypad::Keys::ButtonL),
Button::RightShoulder => Some(gba_keypad::Keys::ButtonR),
_ => None,
}
}

pub fn create_input() -> Sdl2Input {
Sdl2Input {
keyinput: gba_keypad::KEYINPUT_ALL_RELEASED,
Expand Down
28 changes: 27 additions & 1 deletion platform/rustboyadvance-sdl2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::cell::RefCell;
use std::rc::Rc;

use std::fs;
use std::io::Read;
use std::io::{Cursor, Read};
use std::path::{Path, PathBuf};
use std::process;
use std::time;
Expand Down Expand Up @@ -192,6 +192,26 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {

canvas.set_logical_size(CANVAS_WIDTH, CANVAS_HEIGHT)?;

let controller_subsystem = sdl_context.game_controller()?;
let controller_mappings = include_str!("../../../external/SDL_GameControllerDB/gamecontrollerdb.txt");
controller_subsystem.load_mappings_from_read(&mut Cursor::new(controller_mappings))?;

let available_controllers = (0..controller_subsystem.num_joysticks()?)
.filter(|&id| controller_subsystem.is_game_controller(id))
.collect::<Vec<u32>>();

let _active_controller = match available_controllers.first() {
Some(&id) => {
let controller = controller_subsystem.open(id)?;
info!("Found game controller: {}", controller.name());
Some(controller)
},
_ => {
info!("No game controllers were found");
None
},
};

let mut rom_path = match matches.value_of("game_rom") {
Some(path) => path.to_string(),
_ => {
Expand Down Expand Up @@ -298,6 +318,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
Scancode::Space => frame_limiter = true,
k => input.borrow_mut().on_keyboard_key_up(k),
},
Event::ControllerButtonDown { button, .. } => {
input.borrow_mut().on_controller_button_down(button);
},
Event::ControllerButtonUp { button, .. } => {
input.borrow_mut().on_controller_button_up(button);
},
Event::Quit { .. } => break 'running,
Event::DropFile { filename, .. } => {
// load the new rom
Expand Down

0 comments on commit f46f42d

Please sign in to comment.