diff --git a/Makefile b/Makefile index 7cdee35..3e0a197 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ ECHO=echo -e CFLAGS=-Wall -Werror -std=gnu99 -O0 -g -Iinclude LIBS=-lSDL2 -FILES=build/main.o build/mem.o build/stack.o +FILES=build/main.o build/mem.o build/stack.o build/keyboard.o OUT=bin/chip8.out all: $(FILES) @@ -22,6 +22,10 @@ build/stack.o: src/stack.c @$(ECHO) "CC\t\t"$< @$(CC) $(CFLAGS) $< -c -o $@ $(LIBS) +build/keyboard.o: src/keyboard.c + @$(ECHO) "CC\t\t"$< + @$(CC) $(CFLAGS) $< -c -o $@ $(LIBS) + run: all @$(ECHO) "Runing the Chip8 compiler" @$(OUT) diff --git a/include/chip8.h b/include/chip8.h index 8dda08f..9a93101 100644 --- a/include/chip8.h +++ b/include/chip8.h @@ -2,6 +2,7 @@ #define __CHIP8_H #include "config.h" +#include "keyboard.h" #include "mem.h" #include "registers.h" #include "stack.h" @@ -11,6 +12,7 @@ struct chip8 struct chip8_memory memory; struct chip8_registers registers; struct chip8_stack stack; + struct chip8_keyboard keyboard; }; #endif diff --git a/include/config.h b/include/config.h index 7d4b4fa..39a2e1a 100644 --- a/include/config.h +++ b/include/config.h @@ -10,5 +10,6 @@ #define CHIP8_TOTAL_DATA_REGISTERS 16 #define CHIP8_TOTAL_STACK_DEPTH 16 +#define CHIP8_TOTAL_KEYS 16 #endif diff --git a/include/keyboard.h b/include/keyboard.h new file mode 100644 index 0000000..785053b --- /dev/null +++ b/include/keyboard.h @@ -0,0 +1,16 @@ +#ifndef __KEYBOARD_H +#define __KEYBOARD_H + +#include "config.h" + +struct chip8_keyboard +{ + _Bool keys[CHIP8_TOTAL_KEYS]; +}; + +int chip8_keyboard_map (const char *map, char key); +void chip8_keyboard_down (struct chip8_keyboard *kbd, int key); +void chip8_keyboard_up (struct chip8_keyboard *kbd, int key); +_Bool chip8_keyboard_is_down (struct chip8_keyboard *kbd, int key); + +#endif diff --git a/src/keyboard.c b/src/keyboard.c new file mode 100644 index 0000000..b0f026d --- /dev/null +++ b/src/keyboard.c @@ -0,0 +1,46 @@ +#include "keyboard.h" +#include + +static void +chip8_keyboard_ensure_in_bounds (int key) +{ + assert (key >= 0 && key < CHIP8_TOTAL_KEYS); +} + +int +chip8_keyboard_map (const char *map, char key) +{ + for (int i = 0; i < CHIP8_TOTAL_KEYS; i++) + { + if (map[i] == key) + { + return i; + } + } + + return -1; +} + +void +chip8_keyboard_down (struct chip8_keyboard *kbd, int key) +{ + chip8_keyboard_ensure_in_bounds (key); + + kbd->keys[key] = 1; +} + +void +chip8_keyboard_up (struct chip8_keyboard *kbd, int key) +{ + chip8_keyboard_ensure_in_bounds (key); + + kbd->keys[key] = 0; +} + +_Bool +chip8_keyboard_is_down (struct chip8_keyboard *kbd, int key) +{ + chip8_keyboard_ensure_in_bounds (key); + + return kbd->keys[key]; +} diff --git a/src/main.c b/src/main.c index ca0416d..d7376a7 100644 --- a/src/main.c +++ b/src/main.c @@ -2,16 +2,14 @@ #include "chip8.h" +const char keyboard_map[CHIP8_TOTAL_KEYS] + = { SDLK_0, SDLK_1, SDLK_2, SDLK_3, SDLK_4, SDLK_5, SDLK_6, SDLK_7, + SDLK_8, SDLK_9, SDLK_a, SDLK_b, SDLK_c, SDLK_d, SDLK_e, SDLK_f }; + int main (int argc, char **argv) { struct chip8 chip8; - chip8.registers.SP = 0; - chip8_stack_push (&chip8, 0xff); - chip8_stack_push (&chip8, 0xaa); - - printf ("%x\n", chip8_stack_pop (&chip8)); - printf ("%x\n", chip8_stack_pop (&chip8)); SDL_Init (SDL_INIT_EVERYTHING); SDL_Window *window = SDL_CreateWindow ( @@ -26,9 +24,33 @@ main (int argc, char **argv) SDL_Event event; while (SDL_PollEvent (&event)) { - if (event.type == SDL_QUIT) + switch (event.type) { + case SDL_QUIT: goto out; + break; + + case SDL_KEYDOWN: + { + char key = event.key.keysym.sym; + int vkey = chip8_keyboard_map (keyboard_map, key); + if (vkey != -1) + { + chip8_keyboard_down (&chip8.keyboard, vkey); + } + } + break; + + case SDL_KEYUP: + { + char key = event.key.keysym.sym; + int vkey = chip8_keyboard_map (keyboard_map, key); + if (vkey != -1) + { + chip8_keyboard_up (&chip8.keyboard, vkey); + } + } + break; } }