Skip to content

Commit

Permalink
main: Implement keyboard controls
Browse files Browse the repository at this point in the history
  • Loading branch information
moto999999 committed Feb 22, 2022
1 parent 4b7bf86 commit e949a80
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
58 changes: 55 additions & 3 deletions Chip 8/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -63,6 +66,7 @@ int main(int argc, char* args[])
{
quit = true;
}
handleEvent(&e, &chip8);
}

/*
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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;
}
}
}
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down

0 comments on commit e949a80

Please sign in to comment.