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()