implemented keyboard
This commit is contained in:
parent
733d0b73a7
commit
d4944caf67
6
Makefile
6
Makefile
@ -3,7 +3,7 @@ ECHO=echo -e
|
|||||||
CFLAGS=-Wall -Werror -std=gnu99 -O0 -g -Iinclude
|
CFLAGS=-Wall -Werror -std=gnu99 -O0 -g -Iinclude
|
||||||
LIBS=-lSDL2
|
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
|
OUT=bin/chip8.out
|
||||||
|
|
||||||
all: $(FILES)
|
all: $(FILES)
|
||||||
@ -22,6 +22,10 @@ build/stack.o: src/stack.c
|
|||||||
@$(ECHO) "CC\t\t"$<
|
@$(ECHO) "CC\t\t"$<
|
||||||
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
|
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
|
||||||
|
|
||||||
|
build/keyboard.o: src/keyboard.c
|
||||||
|
@$(ECHO) "CC\t\t"$<
|
||||||
|
@$(CC) $(CFLAGS) $< -c -o $@ $(LIBS)
|
||||||
|
|
||||||
run: all
|
run: all
|
||||||
@$(ECHO) "Runing the Chip8 compiler"
|
@$(ECHO) "Runing the Chip8 compiler"
|
||||||
@$(OUT)
|
@$(OUT)
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
#define __CHIP8_H
|
#define __CHIP8_H
|
||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
#include "keyboard.h"
|
||||||
#include "mem.h"
|
#include "mem.h"
|
||||||
#include "registers.h"
|
#include "registers.h"
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
@ -11,6 +12,7 @@ struct chip8
|
|||||||
struct chip8_memory memory;
|
struct chip8_memory memory;
|
||||||
struct chip8_registers registers;
|
struct chip8_registers registers;
|
||||||
struct chip8_stack stack;
|
struct chip8_stack stack;
|
||||||
|
struct chip8_keyboard keyboard;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -10,5 +10,6 @@
|
|||||||
|
|
||||||
#define CHIP8_TOTAL_DATA_REGISTERS 16
|
#define CHIP8_TOTAL_DATA_REGISTERS 16
|
||||||
#define CHIP8_TOTAL_STACK_DEPTH 16
|
#define CHIP8_TOTAL_STACK_DEPTH 16
|
||||||
|
#define CHIP8_TOTAL_KEYS 16
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
16
include/keyboard.h
Normal file
16
include/keyboard.h
Normal file
@ -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
|
46
src/keyboard.c
Normal file
46
src/keyboard.c
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#include "keyboard.h"
|
||||||
|
#include <assert.h>
|
||||||
|
|
||||||
|
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];
|
||||||
|
}
|
36
src/main.c
36
src/main.c
@ -2,16 +2,14 @@
|
|||||||
|
|
||||||
#include "chip8.h"
|
#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
|
int
|
||||||
main (int argc, char **argv)
|
main (int argc, char **argv)
|
||||||
{
|
{
|
||||||
struct chip8 chip8;
|
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_Init (SDL_INIT_EVERYTHING);
|
||||||
SDL_Window *window = SDL_CreateWindow (
|
SDL_Window *window = SDL_CreateWindow (
|
||||||
@ -26,9 +24,33 @@ main (int argc, char **argv)
|
|||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
while (SDL_PollEvent (&event))
|
while (SDL_PollEvent (&event))
|
||||||
{
|
{
|
||||||
if (event.type == SDL_QUIT)
|
switch (event.type)
|
||||||
{
|
{
|
||||||
|
case SDL_QUIT:
|
||||||
goto out;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user