Move all input handling into input module

This commit is contained in:
2025-10-18 17:43:22 +01:00
parent f992dd3d39
commit 79dc3c1256
5 changed files with 88 additions and 103 deletions

74
game.c
View File

@@ -66,11 +66,6 @@ static unsigned ship_entity_id;
static unsigned ship_shape_id;
static unsigned fire_shape_id;
static struct {
int rot;
int fwd;
} input;
static entity_t *add_entity(unsigned *id_out)
{
const unsigned id = entity_count++;
@@ -115,61 +110,6 @@ static void shoot()
memcpy(s->verts, shot_verts, sizeof(shot_verts));
}
static void key_press_callback(int key)
{
switch (key) {
case KEY_Q:
quit = true;
break;
case KEY_UP:
++input.fwd;
break;
case KEY_LEFT:
++input.rot;
break;
case KEY_RIGHT:
--input.rot;
break;
case KEY_SPACE:
shoot();
break;
default:
break;
}
}
static void key_release_callback(int key)
{
switch (key) {
case KEY_UP:
--input.fwd;
break;
case KEY_LEFT:
--input.rot;
break;
case KEY_RIGHT:
++input.rot;
break;
default:
break;
}
}
static void key_repeat_callback(int key)
{
switch (key) {
case KEY_SPACE:
shoot();
break;
}
}
static void remove_shape(unsigned id)
{
if (id < shape_count - 1) {
@@ -207,22 +147,20 @@ static void ship_update()
{
entity_t *ship = entities + ship_entity_id;
ship->omg += ROT_PWR * (float)input.rot;
ship->omg += ROT_PWR * (float)input.spin;
const vec2_t thrust = { 0, (float)input.fwd * LIN_PWR };
const vec2_t thrust = { 0, (float)input.thrust * LIN_PWR };
const vec2_t acc = mat2_mul_vec2(ship->dir, thrust);
ship->vel = vec2_add(ship->vel, acc);
shape_t *fire = shapes + fire_shape_id;
fire->visible = input.fwd != 0;
fire->visible = input.thrust != 0;
fire->verts[0].y = FIRE_MEAN + FIRE_JITTER * rng_plusminus();
}
void game_init(float _aspect)
{
input_on_press(key_press_callback);
input_on_release(key_release_callback);
input_on_repeat(key_repeat_callback);
input_on_shoot(shoot);
aspect = _aspect;
quit = false;
@@ -243,7 +181,7 @@ void game_init(float _aspect)
memcpy(fire->verts, fire_verts, sizeof(fire_verts));
}
bool game_update()
void game_update()
{
ship_update();
@@ -272,8 +210,6 @@ bool game_update()
transforms[i]
= mat3_mul_mat3(mat3_translation(e->pos), mat2_extend(e->dir));
}
return !quit;
}
void game_draw()

2
game.h
View File

@@ -2,7 +2,7 @@
#define GAME_H
void game_init(float aspect);
bool game_update();
void game_update();
void game_draw();
#endif

85
input.c
View File

@@ -10,19 +10,67 @@ enum {
RELEASE = 0,
PRESS = 1,
REPEAT = 2,
NCALLBACKS,
};
input_state_t input;
static int input_fd;
static input_callback_t callbacks[NCALLBACKS];
static input_callback_t shoot;
static void key_press(int key)
{
switch (key) {
case KEY_Q:
input.quit = true;
break;
case KEY_UP:
++input.thrust;
break;
case KEY_LEFT:
++input.spin;
break;
case KEY_RIGHT:
--input.spin;
break;
case KEY_SPACE:
if (shoot != nullptr)
shoot();
break;
}
}
static void key_release(int key)
{
switch (key) {
case KEY_UP:
--input.thrust;
break;
case KEY_LEFT:
--input.spin;
break;
case KEY_RIGHT:
++input.spin;
break;
}
}
static void key_repeat(int key)
{
if (key == KEY_SPACE && shoot != nullptr)
shoot();
}
int input_init()
{
memset(callbacks, 0, sizeof(callbacks));
memset(&input, 0, sizeof(input));
shoot = nullptr;
input_fd = open("/dev/input/event0", O_RDONLY);
assert(input_fd != -1);
const int res = ioctl(input_fd, EVIOCGRAB, (void *)1);
assert(res != -1);
@@ -35,19 +83,9 @@ void input_cleanup()
close(input_fd);
}
void input_on_press(input_callback_t cb)
void input_on_shoot(input_callback_t cb)
{
callbacks[PRESS] = cb;
}
void input_on_release(input_callback_t cb)
{
callbacks[RELEASE] = cb;
}
void input_on_repeat(input_callback_t cb)
{
callbacks[REPEAT] = cb;
shoot = cb;
}
void input_handle()
@@ -55,8 +93,17 @@ void input_handle()
struct input_event ev;
const int len = read(input_fd, &ev, sizeof(ev));
assert(len == sizeof(ev));
if (ev.type == EV_KEY && 0 <= ev.value && ev.value < NCALLBACKS) {
if (callbacks[ev.value] != nullptr)
callbacks[ev.value](ev.code);
if (ev.type == EV_KEY) {
switch (ev.value) {
case PRESS:
key_press(ev.code);
break;
case RELEASE:
key_release(ev.code);
break;
case REPEAT:
key_repeat(ev.code);
break;
}
}
}

15
input.h
View File

@@ -1,15 +1,20 @@
#ifndef INPUT_H
#define INPUT_H
typedef void (*input_callback_t)(int key);
typedef void (*input_callback_t)();
typedef struct {
int spin;
int thrust;
bool quit;
} input_state_t;
extern input_state_t input;
int input_init();
void input_cleanup();
void input_on_press(input_callback_t cb);
void input_on_release(input_callback_t cb);
void input_on_repeat(input_callback_t cb);
void input_on_shoot(input_callback_t cb);
void input_handle();
#endif

15
main.c
View File

@@ -10,21 +10,19 @@
int main()
{
rng_init();
const int input_fd = input_init();
const renderer_params_t renderer_params = renderer_init();
const int drm_fd = renderer_params.drm_fd;
rng_init();
game_init(renderer_params.aspect);
renderer_clear();
renderer_swap();
game_init(renderer_params.aspect);
const int drm_fd = renderer_params.drm_fd;
const int max_fd = MAX(input_fd, drm_fd);
fd_set set;
while (1) {
while (!input.quit) {
FD_ZERO(&set);
FD_SET(input_fd, &set);
FD_SET(drm_fd, &set);
@@ -37,8 +35,7 @@ int main()
if (FD_ISSET(drm_fd, &set)) {
renderer_handle();
if (!game_update())
break;
game_update();
game_draw();
renderer_swap();