Skip to content

Commit

Permalink
fix: keypress is now done via events
Browse files Browse the repository at this point in the history
  • Loading branch information
amydevs committed Aug 27, 2024
1 parent af2f188 commit 206b78f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
4 changes: 3 additions & 1 deletion src/bin/chip-8-emu-bin/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ fn main() {
}
Event::WindowEvent { window_id: _, event: window_ev } => match window_ev {
WindowEvent::KeyboardInput {input, device_id: _, is_synthetic: _ } => {
parse_input(input, &mut eventloopchip8.write().unwrap());
if let Some((key, pressed)) = parse_input(input) {
eventloopchip8.write().unwrap().keystate[key] = pressed as u8;
}
let pressed = (input.state == ElementState::Pressed) as u8;
if let Some(virtual_keycode) = input.virtual_keycode {
match virtual_keycode {
Expand Down
40 changes: 20 additions & 20 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,63 +19,63 @@ pub static KEYMAP: [usize; 16] = [
0xF, // F
];

pub fn parse_input(input: KeyboardInput, chip8inst: &mut crate::chip8::Chip8) {
let pressed = (input.state == ElementState::Pressed) as u8;
pub fn parse_input(input: KeyboardInput) -> Option<(usize, bool)> {
let pressed = input.state == ElementState::Pressed;
if let Some(virtual_keycode) = input.virtual_keycode {
match virtual_keycode {
VirtualKeyCode::Key1=> {
chip8inst.keystate[KEYMAP[0]] = pressed;
return Some((KEYMAP[0], pressed));
},
VirtualKeyCode::Key2=> {
chip8inst.keystate[KEYMAP[1]] = pressed;
return Some((KEYMAP[1], pressed));
},
VirtualKeyCode::Key3=> {
chip8inst.keystate[KEYMAP[2]] = pressed;
return Some((KEYMAP[2], pressed));
},
VirtualKeyCode::Key4=> {
chip8inst.keystate[KEYMAP[3]] = pressed;
return Some((KEYMAP[3], pressed));
},

VirtualKeyCode::Q=> {
chip8inst.keystate[KEYMAP[4]] = pressed;
return Some((KEYMAP[4], pressed));
},
VirtualKeyCode::W=> {
chip8inst.keystate[KEYMAP[5]] = pressed;
return Some((KEYMAP[5], pressed));
},
VirtualKeyCode::E=> {
chip8inst.keystate[KEYMAP[6]] = pressed;
return Some((KEYMAP[6], pressed));
},
VirtualKeyCode::R=> {
chip8inst.keystate[KEYMAP[7]] = pressed;
return Some((KEYMAP[7], pressed));
},

VirtualKeyCode::A=> {
chip8inst.keystate[KEYMAP[8]] = pressed;
return Some((KEYMAP[8], pressed));
},
VirtualKeyCode::S=> {
chip8inst.keystate[KEYMAP[9]] = pressed;
return Some((KEYMAP[9], pressed));
},
VirtualKeyCode::D=> {
chip8inst.keystate[KEYMAP[10]] = pressed;
return Some((KEYMAP[10], pressed));
},
VirtualKeyCode::F=> {
chip8inst.keystate[KEYMAP[11]] = pressed;
return Some((KEYMAP[11], pressed));
},

VirtualKeyCode::Z=> {
chip8inst.keystate[KEYMAP[12]] = pressed;
return Some((KEYMAP[12], pressed));
},
VirtualKeyCode::X=> {
chip8inst.keystate[KEYMAP[13]] = pressed;
return Some((KEYMAP[13], pressed));
},
VirtualKeyCode::C=> {
chip8inst.keystate[KEYMAP[14]] = pressed;
return Some((KEYMAP[14], pressed));
},
VirtualKeyCode::V=> {
chip8inst.keystate[KEYMAP[15]] = pressed;
return Some((KEYMAP[15], pressed));
},
_ => {}
_ => { }
}
}

None
}
50 changes: 21 additions & 29 deletions src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ impl WasmEventLoop {
}
Event::WindowEvent { window_id: _, event: ref window_ev } => match window_ev {
WindowEvent::KeyboardInput {input, device_id: _, is_synthetic: _ } => {
parse_input(*input, &mut main_loop.chip8.write().unwrap());
if let Some((key, pressed)) = parse_input(*input) {
main_loop.tx.send(WasmMainLoopMessage::SetKey(key, pressed)).unwrap();
}
// let pressed = (input.state == ElementState::Pressed) as u8;
// if let Some(virtual_keycode) = input.virtual_keycode {
// match virtual_keycode {
Expand Down Expand Up @@ -165,22 +167,12 @@ impl WasmEventLoop {
).unwrap();
}

pub fn press(&self, key: u8) {
if !KEYMAP.contains(&(key as usize)) {
return;
}
if let Some(main_loop_wrapper) = self.main_loop_wrapper.lock().unwrap().as_ref() {
main_loop_wrapper.main_loop.chip8.write().unwrap().keystate[key as usize] = 1;
}
}

pub fn unpress(&self, key: u8) {
if !KEYMAP.contains(&(key as usize)) {
return;
}
if let Some(main_loop_wrapper) = self.main_loop_wrapper.lock().unwrap().as_ref() {
main_loop_wrapper.main_loop.chip8.write().unwrap().keystate[key as usize] = 0;
}
pub fn set_key(&self, key: u8, pressed: bool) {
self.tx.send(
WasmEventLoopMessage::WasmMainLoopMessage(
WasmMainLoopMessage::SetKey(key as usize, pressed)
)
).unwrap();
}
}

Expand Down Expand Up @@ -211,6 +203,7 @@ impl From<Options> for WasmMainLoopOptions {
enum WasmMainLoopMessage {
Stop,
SetOptions(WasmMainLoopOptions),
SetKey(usize, bool),
}

#[wasm_bindgen]
Expand Down Expand Up @@ -276,6 +269,13 @@ impl WasmMainLoop {
WasmMainLoopMessage::SetOptions(mesg) => {
main_loop_options = mesg;
},
WasmMainLoopMessage::SetKey(key, pressed) => {
if !KEYMAP.contains(&(key as usize)) {
continue;
}
main_loop_chip8.write().unwrap().keystate[key] = pressed as u8;

}
}
}

Expand Down Expand Up @@ -326,17 +326,9 @@ impl WasmMainLoop {
self.tx.send(WasmMainLoopMessage::SetOptions(WasmMainLoopOptions::from(options))).unwrap();
}

pub fn press(&self, key: u8) {
if !KEYMAP.contains(&(key as usize)) {
return;
}
self.chip8.write().unwrap().keystate[key as usize] = 1;
}

pub fn unpress(&self, key: u8) {
if !KEYMAP.contains(&(key as usize)) {
return;
}
self.chip8.write().unwrap().keystate[key as usize] = 0;
pub fn set_key(&self, key: u8, pressed: bool) {
self.tx.send(
WasmMainLoopMessage::SetKey(key as usize, pressed)
).unwrap();
}
}

0 comments on commit 206b78f

Please sign in to comment.