From e949a803f887865d6735960f56aee1770c1b221d Mon Sep 17 00:00:00 2001 From: moto999999 Date: Tue, 22 Feb 2022 17:47:47 +0100 Subject: [PATCH] main: Implement keyboard controls --- Chip 8/main.cpp | 58 ++++++++++++++++++++++++++++++++++++++++++++++--- README.md | 18 +++++++++++++++ 2 files changed, 73 insertions(+), 3 deletions(-) diff --git a/Chip 8/main.cpp b/Chip 8/main.cpp index 7c84557..d4f5190 100644 --- a/Chip 8/main.cpp +++ b/Chip 8/main.cpp @@ -16,6 +16,9 @@ bool init(SDL_Window** window, SDL_Renderer** renderer); //Frees media and shuts down SDL void close(SDL_Window** window, SDL_Renderer** renderer); +// Handles key presses +void handleEvent(SDL_Event* e, Chip8* chip8); + int main(int argc, char* args[]) { //The window we'll be rendering to @@ -63,6 +66,7 @@ int main(int argc, char* args[]) { quit = true; } + handleEvent(&e, &chip8); } /* @@ -88,10 +92,9 @@ int main(int argc, char* args[]) } } chip8.drawFlag = false; // The screen has been updated, disable the flag - - //Update screen - SDL_RenderPresent(renderer); } + //Update screen + SDL_RenderPresent(renderer); } } } @@ -154,3 +157,52 @@ void close(SDL_Window** window, SDL_Renderer** renderer) //Quit SDL subsystems SDL_Quit(); } + +void handleEvent(SDL_Event* e, Chip8* chip8) { + // Check if a button is pressed + if (e->type == SDL_KEYDOWN && e->key.repeat == 0) + { + switch (e->key.keysym.sym) + { // If pressed update key array in chip8 object + case SDLK_1: chip8->key[0] = 1; break; + case SDLK_2: chip8->key[1] = 1; break; + case SDLK_3: chip8->key[2] = 1; break; + case SDLK_4: chip8->key[3] = 1; break; + case SDLK_q: chip8->key[4] = 1; break; + case SDLK_w: chip8->key[5] = 1; break; + case SDLK_e: chip8->key[6] = 1; break; + case SDLK_r: chip8->key[7] = 1; break; + case SDLK_a: chip8->key[8] = 1; break; + case SDLK_s: chip8->key[9] = 1; break; + case SDLK_d: chip8->key[10] = 1; break; + case SDLK_f: chip8->key[11] = 1; break; + case SDLK_z: chip8->key[12] = 1; break; + case SDLK_x: chip8->key[13] = 1; break; + case SDLK_c: chip8->key[14] = 1; break; + case SDLK_v: chip8->key[15] = 1; break; + } + } + // Check if a button is released + else if (e->type == SDL_KEYUP && e->key.repeat == 0) + { + switch (e->key.keysym.sym) + {// If released update key array in chip8 object + case SDLK_1: chip8->key[0] = 0; break; + case SDLK_2: chip8->key[1] = 0; break; + case SDLK_3: chip8->key[2] = 0; break; + case SDLK_4: chip8->key[3] = 0; break; + case SDLK_q: chip8->key[4] = 0; break; + case SDLK_w: chip8->key[5] = 0; break; + case SDLK_e: chip8->key[6] = 0; break; + case SDLK_r: chip8->key[7] = 0; break; + case SDLK_a: chip8->key[8] = 0; break; + case SDLK_s: chip8->key[9] = 0; break; + case SDLK_d: chip8->key[10] = 0; break; + case SDLK_f: chip8->key[11] = 0; break; + case SDLK_z: chip8->key[12] = 0; break; + case SDLK_x: chip8->key[13] = 0; break; + case SDLK_c: chip8->key[14] = 0; break; + case SDLK_v: chip8->key[15] = 0; break; + } + } +} diff --git a/README.md b/README.md index 169e303..5bc96f2 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,24 @@ # CHIP-8 emulator A CHIP-8 emulator written in C++ using SDL2. +## Controls +### Keypad + +| | | | | +|---|---|---|---| +| 1 | 2 | 3 | C | +| 4 | 5 | 6 | D | +| 7 | 8 | 9 | E | +| A | 0 | B | F | + +### Keyboard +| | | | | +|---|---|---|---| +| 1 | 2 | 3 | 4 | +| Q | W | E | R | +| A | S | D | F | +| Z | X | C | V | + ## Dependencies - SDL2 2.0.20