diff --git a/controller.c b/controller.c new file mode 100644 index 0000000..54979a4 --- /dev/null +++ b/controller.c @@ -0,0 +1,65 @@ +#include + +#define VB_PWR 0x0001 +#define VB_SGN 0x0002 +#define VB_A 0x0004 +#define VB_B 0x0008 +#define VB_RT 0x0010 +#define VB_LT 0x0020 +#define VB_RU 0x0040 +#define VB_RR 0x0080 +#define VB_LR 0x0100 +#define VB_LL 0x0200 +#define VB_LD 0x0400 +#define VB_LU 0x0800 +#define VB_STA 0x1000 +#define VB_SEL 0x2000 +#define VB_RL 0x4000 +#define VB_RD 0x8000 + +static uint16_t symToMask(SDL_KeyCode sym) { + switch (sym) { + default: return 0; + case SDLK_a: + return VB_SEL; + case SDLK_s: + return VB_STA; + case SDLK_d: + return VB_B; + case SDLK_f: + return VB_A; + case SDLK_e: + return VB_LT; + case SDLK_r: + return VB_RT; + case SDLK_i: + return VB_RU; + case SDLK_j: + return VB_RL; + case SDLK_k: + return VB_RD; + case SDLK_l: + return VB_RR; + case SDLK_UP: + return VB_LU; + case SDLK_LEFT: + return VB_LL; + case SDLK_DOWN: + return VB_LD; + case SDLK_RIGHT: + return VB_LR; + } +} + +void ctrlInit(ControllerState *ctrl) { + ctrl->keys = VB_SGN; +} +void ctrlKeyDown(ControllerState *ctrl, SDL_Keycode sym) { + ctrl->keys |= symToMask(sym); +} +void ctrlKeyUp(ControllerState *ctrl, SDL_Keycode sym) { + ctrl->keys &= ~symToMask(sym); +} +uint16_t ctrlKeys(ControllerState *ctrl) { + return ctrl->keys; +} \ No newline at end of file diff --git a/controller.h b/controller.h new file mode 100644 index 0000000..fe9dabe --- /dev/null +++ b/controller.h @@ -0,0 +1,16 @@ +#ifndef SHROOMS_VB_NATIVE_CONTROLLER_ +#define SHROOMS_VB_NATIVE_CONTROLLER_ + +#include +#include + +typedef struct { + uint16_t keys; +} ControllerState; + +void ctrlInit(ControllerState *ctrl); +void ctrlKeyDown(ControllerState *ctrl, SDL_Keycode sym); +void ctrlKeyUp(ControllerState *ctrl, SDL_Keycode sym); +uint16_t ctrlKeys(ControllerState *ctrl); + +#endif \ No newline at end of file diff --git a/game.c b/game.c index 4f756ee..6ecb687 100644 --- a/game.c +++ b/game.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -43,12 +44,15 @@ int runGame(VB *sim, GraphicsContext *gfx) { uint32_t clocks; SDL_Event event; GameState state; + ControllerState ctrl; uint64_t ticks, prevTicks; state.gfx = gfx; vbSetUserData(sim, &state); vbSetFrameCallback(sim, &onFrame); + ctrlInit(&ctrl); + gfxUpdateLeftEye(gfx, LEFT_EYE_DEFAULT); gfxUpdateRightEye(gfx, RIGHT_EYE_DEFAULT); @@ -57,12 +61,19 @@ int runGame(VB *sim, GraphicsContext *gfx) { prevTicks = tickNs(); vbEmulate(sim, &clocks); ticks = tickNs(); + sleepNanos(((MAX_STEP_CLOCKS - clocks) * 50) - (ticks - prevTicks)); + while (SDL_PollEvent(&event)) { if (event.type == SDL_QUIT) { return 0; } + if (event.type == SDL_KEYDOWN) { + ctrlKeyDown(&ctrl, event.key.keysym.sym); + } + if (event.type == SDL_KEYUP) { + ctrlKeyUp(&ctrl, event.key.keysym.sym); + } } - sleepNanos(((MAX_STEP_CLOCKS - clocks) * 50) - (ticks - prevTicks)); - prevTicks = ticks; + vbSetKeys(sim, ctrlKeys(&ctrl)); } } diff --git a/makefile b/makefile index ab15d20..ea33f24 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,7 @@ else endif build: - @$(CC) cli.c game.c graphics.c main.c assets/assets.s -I . \ + @$(CC) cli.c controller.c game.c graphics.c main.c assets/assets.s -I . \ $(SHROOMSFLAGS) $(SDL2FLAGS) \ -D POSIX_C_SOURCE=199309L \ -o shrooms-vb \ diff --git a/shrooms-vb-core b/shrooms-vb-core index 37b9a94..50903c7 160000 --- a/shrooms-vb-core +++ b/shrooms-vb-core @@ -1 +1 @@ -Subproject commit 37b9a943384a230b93089b7617d1b3f5344480d7 +Subproject commit 50903c7513b00cc62cb20a1578e7f6484914a9da