60 lines
1.3 KiB
C
60 lines
1.3 KiB
C
#include "input.h"
|
|
#include "chip8.h"
|
|
#include <stdio.h>
|
|
|
|
void input_update(SDL_Event *event, uint8_t *keyboard)
|
|
{
|
|
uint8_t key = map_sdl_to_chip8(event->key.keysym.sym);
|
|
if (key != 0xFF)
|
|
{
|
|
if (event->type == SDL_KEYDOWN)
|
|
{
|
|
keyboard[key] = 1;
|
|
}
|
|
else if (event->type == SDL_KEYUP)
|
|
{
|
|
keyboard[key] = 0;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
uint8_t map_sdl_to_chip8(SDL_Keycode key)
|
|
{
|
|
// Map to a numpad-like layout:
|
|
// 1 2 3 4 → 1 2 3 C
|
|
// Q W E R → 4 5 6 D
|
|
// A S D F → 7 8 9 E
|
|
// Z X C V → A 0 B F
|
|
switch (key)
|
|
{
|
|
case SDLK_1: return 0x1;
|
|
case SDLK_2: return 0x2;
|
|
case SDLK_3: return 0x3;
|
|
case SDLK_4: return 0xC;
|
|
case SDLK_q: return 0x4;
|
|
case SDLK_w: return 0x5;
|
|
case SDLK_e: return 0x6;
|
|
case SDLK_r: return 0xD;
|
|
case SDLK_a: return 0x7;
|
|
case SDLK_s: return 0x8;
|
|
case SDLK_d: return 0x9;
|
|
case SDLK_f: return 0xE;
|
|
case SDLK_z: return 0xA;
|
|
case SDLK_x: return 0x0;
|
|
case SDLK_c: return 0xB;
|
|
case SDLK_v: return 0xF;
|
|
default: return 0xFF;
|
|
}
|
|
}
|
|
|
|
|
|
uint8_t get_pressed_key(t_chip8 *emu)
|
|
{
|
|
for (int i = 0; i < 16; i++)
|
|
{
|
|
if (emu->keyboard.keyboard[i])
|
|
return i;
|
|
}
|
|
return 0xFF; // No key pressed
|
|
}
|