From ff1a8222a835068cb5b88ec1189ea58fcff2ed2a Mon Sep 17 00:00:00 2001 From: Rekai Musuka Date: Tue, 14 Jul 2020 23:35:00 -0500 Subject: [PATCH] Fix issues with user input. Maybe we should do something about the 36 if statements in handle_input --- src/main.rs | 96 +++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 32 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e7bc5c..16c4e2f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -58,85 +58,117 @@ fn handle_input(chip8: &mut Chip8, input: &mut WinitInputHelper, control_flow: & return; } + // 1 -> 1 if input.key_pressed(VirtualKeyCode::Key1) { chip8.key.set_key(0x1); - } else { + } + if input.key_released(VirtualKeyCode::Key1) { chip8.key.unset_key(0x1); } + // 2 -> 2 if input.key_pressed(VirtualKeyCode::Key2) { chip8.key.set_key(0x2); - } else { - chip8.key.set_key(0x2); } + if input.key_released(VirtualKeyCode::Key2) { + chip8.key.unset_key(0x2); + } + // 3 -> 3 if input.key_pressed(VirtualKeyCode::Key3) { chip8.key.set_key(0x3); - } else { - chip8.key.set_key(0x3); } + if input.key_released(VirtualKeyCode::Key3) { + chip8.key.unset_key(0x3); + } + // 4 -> C if input.key_pressed(VirtualKeyCode::Key4) { chip8.key.set_key(0xC); - } else { - chip8.key.set_key(0xC); } + if input.key_released(VirtualKeyCode::Key4) { + chip8.key.unset_key(0xC); + } + // Q -> 4 if input.key_pressed(VirtualKeyCode::Q) { chip8.key.set_key(0x4); - } else { - chip8.key.set_key(0x4); } + if input.key_released(VirtualKeyCode::Q) { + chip8.key.unset_key(0x4); + } + // W -> 5 if input.key_pressed(VirtualKeyCode::W) { chip8.key.set_key(0x5); - } else { - chip8.key.set_key(0x5); } + if input.key_released(VirtualKeyCode::W) { + chip8.key.unset_key(0x5); + } + // E -> 6 if input.key_pressed(VirtualKeyCode::E) { chip8.key.set_key(0x6); - } else { - chip8.key.set_key(0x6); } + if input.key_released(VirtualKeyCode::E) { + chip8.key.unset_key(0x6); + } + // R -> D if input.key_pressed(VirtualKeyCode::R) { chip8.key.set_key(0xD); - } else { - chip8.key.set_key(0xD); } + if input.key_released(VirtualKeyCode::R) { + chip8.key.unset_key(0xD); + } + // A -> 7 if input.key_pressed(VirtualKeyCode::A) { chip8.key.set_key(0x7); - } else { - chip8.key.set_key(0x7); } + if input.key_released(VirtualKeyCode::A) { + chip8.key.unset_key(0x7); + } + // S -> 8 if input.key_pressed(VirtualKeyCode::S) { chip8.key.set_key(0x8); - } else { - chip8.key.set_key(0x8); } + if input.key_released(VirtualKeyCode::S) { + chip8.key.unset_key(0x8); + } + // D -> 9 if input.key_pressed(VirtualKeyCode::D) { chip8.key.set_key(0x9); - } else { - chip8.key.set_key(0x9); } + if input.key_released(VirtualKeyCode::D) { + chip8.key.unset_key(0x9); + } + // F -> A if input.key_pressed(VirtualKeyCode::F) { - chip8.key.set_key(0x5); - } else { - chip8.key.set_key(0x5); + chip8.key.set_key(0xE); } + if input.key_released(VirtualKeyCode::F) { + chip8.key.unset_key(0xE); + } + // Z -> A if input.key_pressed(VirtualKeyCode::Z) { chip8.key.set_key(0xA); - } else { - chip8.key.set_key(0xA); } + if input.key_released(VirtualKeyCode::Z) { + chip8.key.unset_key(0xA); + } + // X -> 0 if input.key_pressed(VirtualKeyCode::X) { chip8.key.set_key(0x0); - } else { - chip8.key.set_key(0x0); } + if input.key_released(VirtualKeyCode::X) { + chip8.key.unset_key(0x0); + } + // C -> B if input.key_pressed(VirtualKeyCode::C) { chip8.key.set_key(0xB); - } else { - chip8.key.set_key(0xB); } + if input.key_released(VirtualKeyCode::C) { + chip8.key.unset_key(0xB); + } + // V -> F if input.key_pressed(VirtualKeyCode::V) { chip8.key.set_key(0xF); - } else { - chip8.key.set_key(0xF); + } + if input.key_released(VirtualKeyCode::V) { + chip8.key.unset_key(0xF); } }