From 31eb12cb6aec1bbadc69c70489031ed50fc342e1 Mon Sep 17 00:00:00 2001 From: Michel Heily Date: Fri, 20 Dec 2019 18:11:14 +0200 Subject: [PATCH] Add "press SPACE to turbo" to sdl2 binary, like in old-school emulators :P Former-commit-id: 95dfddfac193ac846f179ec607a8f7c3fc0e21a4 --- src/plat/sdl2/cli.yml | 3 -- src/plat/sdl2/{keyboard.rs => input.rs} | 42 +++++++------------ src/plat/sdl2/main.rs | 56 +++++++++++++++++++++---- 3 files changed, 63 insertions(+), 38 deletions(-) rename src/plat/sdl2/{keyboard.rs => input.rs} (50%) diff --git a/src/plat/sdl2/cli.yml b/src/plat/sdl2/cli.yml index b195de1..df8b351 100644 --- a/src/plat/sdl2/cli.yml +++ b/src/plat/sdl2/cli.yml @@ -16,9 +16,6 @@ args: - skip_bios: long: skip-bios help: Skip running bios and start from the ROM instead - - no_framerate_limit: - long: no-framerate-limit - help: Run without frame limiter - debug: long: debug help: Start with the debugger attached diff --git a/src/plat/sdl2/keyboard.rs b/src/plat/sdl2/input.rs similarity index 50% rename from src/plat/sdl2/keyboard.rs rename to src/plat/sdl2/input.rs index 34b00c2..7eabc2c 100644 --- a/src/plat/sdl2/keyboard.rs +++ b/src/plat/sdl2/input.rs @@ -1,5 +1,4 @@ use sdl2::keyboard::Keycode; -use sdl2::{event::Event, EventPump}; use rustboyadvance_ng::core::keypad as gba_keypad; use rustboyadvance_ng::InputInterface; @@ -8,38 +7,28 @@ extern crate bit; use bit::BitIndex; pub struct Sdl2Input { - event_pump: EventPump, keyinput: u16, } impl InputInterface for Sdl2Input { fn poll(&mut self) -> u16 { - for event in self.event_pump.poll_iter() { - match event { - Event::KeyDown { - keycode: Some(keycode), - .. - } => { - if let Some(key) = keycode_to_keypad(keycode) { - self.keyinput.set_bit(key as usize, false); - } - } - Event::KeyUp { - keycode: Some(keycode), - .. - } => { - if let Some(key) = keycode_to_keypad(keycode) { - self.keyinput.set_bit(key as usize, true); - } - } - Event::Quit { .. } => panic!("quit!"), - _ => {} - } - } self.keyinput } } +impl Sdl2Input { + pub fn on_keyboard_key_down(&mut self, keycode: Keycode) { + if let Some(key) = keycode_to_keypad(keycode) { + self.keyinput.set_bit(key as usize, false); + } + } + pub fn on_keyboard_key_up(&mut self, keycode: Keycode) { + if let Some(key) = keycode_to_keypad(keycode) { + self.keyinput.set_bit(key as usize, true); + } + } +} + fn keycode_to_keypad(keycode: Keycode) -> Option { match keycode { Keycode::Up => Some(gba_keypad::Keys::Up), @@ -49,16 +38,15 @@ fn keycode_to_keypad(keycode: Keycode) -> Option { Keycode::Z => Some(gba_keypad::Keys::ButtonB), Keycode::X => Some(gba_keypad::Keys::ButtonA), Keycode::Return => Some(gba_keypad::Keys::Start), - Keycode::Space => Some(gba_keypad::Keys::Select), + Keycode::Backspace => Some(gba_keypad::Keys::Select), Keycode::A => Some(gba_keypad::Keys::ButtonL), Keycode::S => Some(gba_keypad::Keys::ButtonR), _ => None, } } -pub fn create_keyboard(sdl: &sdl2::Sdl) -> Sdl2Input { +pub fn create_input() -> Sdl2Input { Sdl2Input { - event_pump: sdl.event_pump().unwrap(), keyinput: gba_keypad::KEYINPUT_ALL_RELEASED, } } diff --git a/src/plat/sdl2/main.rs b/src/plat/sdl2/main.rs index bfa9776..fc77714 100644 --- a/src/plat/sdl2/main.rs +++ b/src/plat/sdl2/main.rs @@ -1,4 +1,6 @@ extern crate sdl2; +use sdl2::event::Event; +use sdl2::keyboard::Keycode; use std::cell::RefCell; use std::rc::Rc; @@ -10,24 +12,23 @@ use std::time; extern crate clap; mod audio; -mod keyboard; +mod input; mod video; use audio::{create_audio_player, Sdl2AudioPlayer}; -use keyboard::{create_keyboard, Sdl2Input}; +use input::{create_input, Sdl2Input}; use video::{create_video_interface, Sdl2Video}; -#[macro_use] extern crate rustboyadvance_ng; use rustboyadvance_ng::prelude::*; use rustboyadvance_ng::util::FpsCounter; fn main() { + let mut frame_limiter = true; let yaml = load_yaml!("cli.yml"); let matches = clap::App::from_yaml(yaml).get_matches(); let skip_bios = matches.occurrences_of("skip_bios") != 0; - let no_framerate_limit = matches.occurrences_of("no_framerate_limit") != 0; let debug = matches.occurrences_of("debug") != 0; let bios_path = Path::new(matches.value_of("bios").unwrap_or_default()); @@ -45,11 +46,19 @@ fn main() { let sdl_context = sdl2::init().unwrap(); let video = Rc::new(RefCell::new(create_video_interface(&sdl_context))); let audio = Rc::new(RefCell::new(create_audio_player(&sdl_context))); - let keyboard = Rc::new(RefCell::new(create_keyboard(&sdl_context))); + let input = Rc::new(RefCell::new(create_input())); let mut fps_counter = FpsCounter::default(); - let mut gba: GameBoyAdvance = - GameBoyAdvance::new(cpu, bios_bin, cart, video.clone(), audio.clone(), keyboard.clone()); + let mut gba: GameBoyAdvance = GameBoyAdvance::new( + cpu, + bios_bin, + cart, + video.clone(), + audio.clone(), + input.clone(), + ); + + let mut event_pump = sdl_context.event_pump().unwrap(); if debug { gba.cpu.set_verbose(true); @@ -62,6 +71,37 @@ fn main() { loop { let start_time = time::Instant::now(); + for event in event_pump.poll_iter() { + match event { + Event::KeyDown { + keycode: Some(Keycode::Space), + .. + } => { + frame_limiter = false; + } + Event::KeyUp { + keycode: Some(Keycode::Space), + .. + } => { + frame_limiter = true; + } + Event::KeyDown { + keycode: Some(keycode), + .. + } => { + input.borrow_mut().on_keyboard_key_down(keycode); + } + Event::KeyUp { + keycode: Some(keycode), + .. + } => { + input.borrow_mut().on_keyboard_key_up(keycode); + } + Event::Quit { .. } => panic!("quit!"), + _ => {} + } + } + gba.frame(); if let Some(fps) = fps_counter.tick() { @@ -69,7 +109,7 @@ fn main() { video.borrow_mut().set_window_title(&title); } - if !no_framerate_limit { + if frame_limiter { let time_passed = start_time.elapsed(); let delay = frame_time.checked_sub(time_passed); match delay {