Skip to content
Snippets Groups Projects
Verified Commit 40a3f4c8 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

feat: initial support for key pressing

parent 10c0fa0c
No related branches found
No related tags found
No related merge requests found
Pipeline #915 passed
use boytacean::{
gb::GameBoy,
pad::PadKey,
ppu::{DISPLAY_HEIGHT, DISPLAY_WIDTH},
};
use sdl2::{
event::Event, image::LoadSurface, pixels::PixelFormatEnum, surface::Surface, video::Window,
AudioSubsystem, EventPump, TimerSubsystem, VideoSubsystem,
event::Event, image::LoadSurface, keyboard::Keycode, pixels::PixelFormatEnum, surface::Surface,
video::Window, AudioSubsystem, EventPump, TimerSubsystem, VideoSubsystem,
};
/// The base title to be used in the window.
......@@ -112,6 +113,17 @@ fn main() {
while let Some(event) = graphics.event_pump.poll_event() {
match event {
Event::Quit { .. } => break 'main,
Event::KeyDown {
keycode: Some(keycode),
..
} => game_boy.key_press(key_to_pad(keycode)),
Event::KeyUp {
keycode: Some(keycode),
..
} => game_boy.key_lift(key_to_pad(keycode)),
_ => (),
}
}
......@@ -146,3 +158,17 @@ fn main() {
graphics.timer_subsystem.delay(17);
}
}
fn key_to_pad(keycode: Keycode) -> PadKey {
match keycode {
Keycode::Up => PadKey::Up,
Keycode::Down => PadKey::Down,
Keycode::Left => PadKey::Left,
Keycode::Right => PadKey::Right,
Keycode::Space => PadKey::Start,
Keycode::Return => PadKey::Select,
Keycode::A => PadKey::A,
Keycode::S => PadKey::B,
_ => PadKey::A,
}
}
......@@ -3,6 +3,7 @@ use core::panic;
use crate::{
inst::{EXTENDED, INSTRUCTIONS},
mmu::Mmu,
pad::Pad,
ppu::Ppu,
};
......@@ -177,6 +178,11 @@ impl Cpu {
self.mmu().ppu()
}
#[inline(always)]
pub fn pad(&mut self) -> &mut Pad {
self.mmu().pad()
}
#[inline(always)]
pub fn halted(&self) -> bool {
self.halted
......
......@@ -2,7 +2,7 @@ use crate::{
cpu::Cpu,
data::{BootRom, DMG_BOOT, DMG_BOOTIX, MGB_BOOTIX, SGB_BOOT},
mmu::Mmu,
pad::Pad,
pad::{Pad, PadKey},
ppu::{Ppu, Tile, FRAME_BUFFER_SIZE},
util::read_file,
};
......@@ -35,6 +35,14 @@ impl GameBoy {
self.cpu.reset();
}
pub fn key_press(&mut self, key: PadKey) {
self.pad().key_press(key);
}
pub fn key_lift(&mut self, key: PadKey) {
self.pad().key_lift(key);
}
pub fn pc(&self) -> u16 {
self.cpu.pc()
}
......@@ -132,6 +140,10 @@ impl GameBoy {
self.cpu.ppu()
}
pub fn pad(&mut self) -> &mut Pad {
self.cpu.pad()
}
pub fn frame_buffer(&mut self) -> &Box<[u8; FRAME_BUFFER_SIZE]> {
&(self.ppu().frame_buffer)
}
......
......@@ -45,6 +45,10 @@ impl Mmu {
&mut self.ppu
}
pub fn pad(&mut self) -> &mut Pad {
&mut self.pad
}
pub fn boot_active(&self) -> bool {
self.boot_active
}
......
#[cfg(feature = "wasm")]
use wasm_bindgen::prelude::*;
pub struct Pad {
down: bool,
up: bool,
......@@ -16,6 +19,7 @@ pub enum PadSelection {
Direction,
}
#[cfg_attr(feature = "wasm", wasm_bindgen)]
pub enum PadKey {
Up,
Down,
......@@ -87,4 +91,30 @@ impl Pad {
addr => panic!("Writing to unknown Pad location 0x{:04x}", addr),
}
}
pub fn key_press(&mut self, key: PadKey) {
match key {
PadKey::Up => self.up = true,
PadKey::Down => self.down = true,
PadKey::Left => self.left = true,
PadKey::Right => self.right = true,
PadKey::Start => self.start = true,
PadKey::Select => self.select = true,
PadKey::A => self.a = true,
PadKey::B => self.b = true,
}
}
pub fn key_lift(&mut self, key: PadKey) {
match key {
PadKey::Up => self.up = false,
PadKey::Down => self.down = false,
PadKey::Left => self.left = false,
PadKey::Right => self.right = false,
PadKey::Start => self.start = false,
PadKey::Select => self.select = false,
PadKey::A => self.a = false,
PadKey::B => self.b = false,
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment