Skip to content

Commit

Permalink
feat: add keypad support
Browse files Browse the repository at this point in the history
  • Loading branch information
nikoof committed Oct 5, 2023
1 parent ca8172f commit 9b5097d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod state;
pub mod window;

fn main() -> Result<()> {
let window = Window::new(
let mut window = Window::new(
"chip8",
640,
320,
Expand All @@ -21,9 +21,11 @@ fn main() -> Result<()> {
},
)
.unwrap_or_else(|err| panic!("Failed to create window: {}", err));
window.limit_update_rate(Some(std::time::Duration::from_secs(1 / 60)));

let mut state = State::new(window);

let program = read_program_from_file(Path::new("./tests/bin/3-corax+.ch8"))?;
let program = read_program_from_file(Path::new("./tests/bin/6-keypad.ch8"))?;
state.load_program(&program);

while state.display_open() {
Expand Down
7 changes: 3 additions & 4 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,17 +156,17 @@ where
}
}
SkipIfKey { key_register } => {
if self.keys[self.variables[key_register] as usize] {
if self.window.is_key_down(self.variables[key_register]) {
self.pc += 2;
}
}
SkipIfNotKey { key_register } => {
if !self.keys[self.variables[key_register] as usize] {
if !self.window.is_key_down(self.variables[key_register]) {
self.pc += 2;
}
}
GetKey { dest } => {
if let Some(key) = self.keys.iter().position(|&e| e) {
if let Some(key) = self.window.get_key() {
self.variables[dest] = key as u8;
} else {
self.pc -= 2;
Expand Down Expand Up @@ -233,7 +233,6 @@ where
let pixel = (sprite_row >> (7 - x_offset)) & 1;
let buffer_index = (y + y_offset) * DISPLAY_WIDTH + (x + x_offset);
self.display[buffer_index] ^= pixel;
// NOTE: Update display here to get live drawing effect
self.variables[0xF] = pixel;
}
}
Expand Down
40 changes: 33 additions & 7 deletions src/window/minifb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,39 @@ fn keyval_to_key(key: u8) -> Option<Key> {
}
}

fn key_to_keyval(key: &Key) -> Option<u8> {
use Key::*;
match key {
Key1 => Some(0x1),
Key2 => Some(0x2),
Key3 => Some(0x3),
Key4 => Some(0xC),

Q => Some(0x4),
W => Some(0x5),
E => Some(0x6),
R => Some(0xD),

A => Some(0x7),
S => Some(0x8),
D => Some(0x9),
F => Some(0xE),

Z => Some(0xA),
X => Some(0x0),
C => Some(0xB),
V => Some(0xF),

_ => None,
}
}

impl Input for Window {
fn get_keys(&self, keys: &mut [bool]) {
for key in 0u8..=0xF {
keys[key as usize] = self.is_key_pressed(
keyval_to_key(key).expect("All cases should be covered"),
KeyRepeat::No,
)
}
fn is_key_down(&self, keyval: u8) -> bool {
self.is_key_down(keyval_to_key(keyval).expect("Should not be called with bad keyval"))
}

fn get_key(&self) -> Option<u8> {
self.get_keys().iter().filter_map(key_to_keyval).nth(0)
}
}
3 changes: 2 additions & 1 deletion src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ pub trait Display {
}

pub trait Input {
fn get_keys(&self, keys: &mut [bool]);
fn is_key_down(&self, key: u8) -> bool;
fn get_key(&self) -> Option<u8>;
}

0 comments on commit 9b5097d

Please sign in to comment.