Keyboard input

This commit is contained in:
Pyrex 2024-02-25 16:25:34 -08:00
parent 3f837d7052
commit 4866226840
4 changed files with 55 additions and 0 deletions

View File

@ -1,3 +1,5 @@
#include <stdbool.h>
#include "device.h"
uint32_t device_pixels[DEVICE_H][DEVICE_W];
bool device_buttons[DEVICE_BUTTON_N];

View File

@ -2,11 +2,27 @@
#define CROCPARTY_DEVICE_H
#include <stdint.h>
#include <stdbool.h>
// 240 x 135 is also cool
#define DEVICE_W 128
#define DEVICE_H 128
typedef enum {
DEVICE_BUTTON_L=0,
DEVICE_BUTTON_R=1,
DEVICE_BUTTON_U=2,
DEVICE_BUTTON_D=3,
DEVICE_BUTTON_0=4,
DEVICE_BUTTON_1=5,
DEVICE_BUTTON_2=6,
DEVICE_BUTTON_3=7,
DEVICE_BUTTON_N=8,
} DeviceButton;
extern uint32_t device_pixels[DEVICE_H][DEVICE_W];
extern bool device_buttons[DEVICE_BUTTON_N];
#endif //CROCPARTY_DEVICE_H

View File

@ -26,4 +26,8 @@ void game_draw() {
device_pixels[y][x] = color;
}
}
for (int i = 0; i < DEVICE_BUTTON_N; i++) {
device_pixels[0][i] = device_buttons[i] ? 0x000000ff : 0xffffffff;
}
}

View File

@ -9,6 +9,7 @@
void sdl_host_suggest_dimensions(uint32_t* window_w, uint32_t* window_h);
void sdl_host_loop();
void sdl_host_handle_keyboard_event(SDL_KeyboardEvent event);
SDL_Window* sdl_host_window;
SDL_Renderer* sdl_host_renderer;
@ -129,6 +130,9 @@ void sdl_host_loop() {
SDL_Event e;
while (SDL_PollEvent(&e)) {
if (e.type == SDL_QUIT) { goto quit; }
if (e.type == SDL_KEYDOWN || e.type == SDL_KEYUP) {
sdl_host_handle_keyboard_event(e.key);
}
}
// trigger game logic
@ -146,4 +150,33 @@ void sdl_host_loop() {
}
}
quit: ;
}
void sdl_host_handle_keyboard_event(SDL_KeyboardEvent event) {
bool is_down;
switch (event.type) {
case SDL_KEYDOWN: is_down = true; break;
case SDL_KEYUP: is_down = false; break;
default: return;
}
if (event.repeat) {
return;
}
DeviceButton db;
switch (event.keysym.sym) {
case SDLK_LEFT: db = DEVICE_BUTTON_L; break;
case SDLK_RIGHT: db = DEVICE_BUTTON_R; break;
case SDLK_UP: db = DEVICE_BUTTON_U; break;
case SDLK_DOWN: db = DEVICE_BUTTON_D; break;
case SDLK_z: db = DEVICE_BUTTON_0; break;
case SDLK_x: db = DEVICE_BUTTON_1; break;
case SDLK_c: db = DEVICE_BUTTON_2; break;
case SDLK_v: db = DEVICE_BUTTON_3; break;
default: return;
}
device_buttons[db] = is_down;
}