From c1f339484bd0eb2f13d82d423e3c07f4697ea953 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 18 Oct 2025 17:43:22 +0100 Subject: [PATCH] Factor game logic into own module --- build.sh | 2 +- game.c | 302 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ game.h | 8 ++ main.c | 293 ++--------------------------------------------------- 4 files changed, 318 insertions(+), 287 deletions(-) create mode 100644 game.c create mode 100644 game.h diff --git a/build.sh b/build.sh index 23d4739..1f5b8c7 100755 --- a/build.sh +++ b/build.sh @@ -8,4 +8,4 @@ defs="-D_POSIX_C_SOURCE=200809L" $cc $warn $flags $libs $defs \ -o asteroids \ - fb.c input.c main.c maths.c renderer.c + fb.c game.c input.c main.c maths.c renderer.c diff --git a/game.c b/game.c new file mode 100644 index 0000000..79f46c5 --- /dev/null +++ b/game.c @@ -0,0 +1,302 @@ +#include "game.h" + +#include "input.h" +#include "renderer.h" + +#include +#include +#include +#include +#include + +#define FIRE_MEAN -0.15 +#define FIRE_JITTER 0.01 + +#define LIN_PWR 0.0001 +#define ROT_PWR 0.002 + +#define SHOT_VEL 0.04 + +#define MAX_SHAPES 256U +#define MAX_ENTITIES 128U +#define MAX_SHAPES_PER_ENTITY 2 + +#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) + +typedef struct { + bool visible; + int entity; + unsigned vert_count; + vec2_t verts[MAX_VERTS]; + uint8_t flags; +} shape_t; + +typedef struct { + vec2_t pos; + vec2_t vel; + mat2_t dir; + float omg; + unsigned shapes[MAX_SHAPES_PER_ENTITY]; + unsigned shape_count; + bool wrap; +} entity_t; + +static const vec2_t ship_verts[] = { + { 0.0, 0.11 }, + { 0.05, -0.07 }, + { 0.0, -0.04 }, + { -0.05, -0.07 }, +}; +static const vec2_t fire_verts[] = { + { 0.0, -0.15 }, + { 0.015, -0.07 }, + { -0.015, -0.07 }, +}; +static const vec2_t shot_verts[] = { { 0.0, -0.02 }, { 0.0, 0.02 } }; + +static entity_t entities[MAX_ENTITIES]; +static mat3_t transforms[MAX_ENTITIES]; +static shape_t shapes[MAX_SHAPES]; + +static unsigned entity_count; +static unsigned shape_count; +static bool quit; +static float aspect; + +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++; + memset(entities + id, 0, sizeof(entity_t)); + entities[id].dir = (mat2_t) { { 1, 0 }, { 0, 1 } }; + if (id_out != nullptr) + *id_out = id; + return entities + id; +} + +static shape_t *add_shape(unsigned entity, unsigned *id_out) +{ + assert(entities[entity].shape_count < MAX_SHAPES_PER_ENTITY); + const int id = shape_count++; + memset(shapes + id, 0, sizeof(shape_t)); + shapes[id].entity = entity; + entities[entity].shapes[entities[entity].shape_count++] = id; + if (id_out != nullptr) + *id_out = id; + return shapes + id; +} + +static void shoot() +{ + if (entity_count >= MAX_ENTITIES || shape_count >= MAX_SHAPES) + return; + + const entity_t *ship = entities + ship_entity_id; + const shape_t *ship_shape = shapes + ship_shape_id; + + unsigned id; + entity_t *e = add_entity(&id); + e->dir = ship->dir; + e->pos = vec2_add( + ship->pos, mat2_mul_vec2(ship->dir, ship_shape->verts[0])); + e->vel = vec2_add( + ship->vel, mat2_mul_vec2(ship->dir, (vec2_t) { 0, SHOT_VEL })); + + shape_t *s = add_shape(id, nullptr); + s->visible = true; + s->vert_count = NELEMS(shot_verts); + 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) { + const shape_t *last = shapes + shape_count - 1; + memcpy(shapes + id, shapes + shape_count - 1, sizeof(shape_t)); + for (unsigned i = 0; i < entities[last->entity].shape_count; ++i) { + if (entities[last->entity].shapes[i] == shape_count - 1) { + entities[last->entity].shapes[i] = id; + break; + } + } + } + --shape_count; +} + +static void remove_entity(unsigned id) +{ + assert(id != ship_entity_id); + + entity_t *e = entities + id; + for (unsigned i = 0; i < e->shape_count; ++i) + remove_shape(e->shapes[i]); + + if (id < entity_count - 1) { + const entity_t *last = entities + entity_count - 1; + memcpy(e, last, sizeof(entity_t)); + for (unsigned i = 0; i < e->shape_count; ++i) + shapes[e->shapes[i]].entity = id; + } + + --entity_count; +} + +static float rand_plusminus() +{ + return 2.0f * ((float)rand() / RAND_MAX - 0.5f); +} + +static void ship_update() +{ + entity_t *ship = entities + ship_entity_id; + + ship->omg += ROT_PWR * (float)input.rot; + + const vec2_t thrust = { 0, (float)input.fwd * 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->verts[0].y = FIRE_MEAN + FIRE_JITTER * rand_plusminus(); +} + +void game_init(float _aspect) +{ + input_on_press(key_press_callback); + input_on_release(key_release_callback); + input_on_repeat(key_repeat_callback); + + struct timeval tv; + gettimeofday(&tv, NULL); + srand(tv.tv_usec); + + aspect = _aspect; + quit = false; + + entity_t *ship = add_entity(&ship_entity_id); + ship->wrap = true; + + shape_t *ship_shape = add_shape(ship_entity_id, &ship_shape_id); + ship_shape->visible = true; + ship_shape->flags = WRAP | CONNECT; + ship_shape->vert_count = NELEMS(ship_verts); + memcpy(ship_shape->verts, ship_verts, sizeof(ship_verts)); + + shape_t *fire = add_shape(ship_entity_id, &fire_shape_id); + fire->visible = true; + fire->flags = WRAP | CONNECT; + fire->vert_count = NELEMS(fire_verts); + memcpy(fire->verts, fire_verts, sizeof(fire_verts)); +} + +bool game_update() +{ + ship_update(); + + for (unsigned i = 0; i < entity_count; ++i) { + entity_t *e = entities + i; + + e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir); + e->pos = vec2_add(e->pos, e->vel); + + if (e->wrap) { + if (e->pos.y > 1) + e->pos.y -= 2; + else if (e->pos.y <= -1) + e->pos.y += 2; + if (e->pos.x >= aspect) + e->pos.x -= 2 * aspect; + else if (e->pos.x < -aspect) + e->pos.x += 2 * aspect; + } else if ( + e->pos.y > 1 || e->pos.y <= -1 || e->pos.x >= aspect + || e->pos.x < -aspect) { + remove_entity(i--); + continue; + } + + transforms[i] + = mat3_mul_mat3(mat3_translation(e->pos), mat2_extend(e->dir)); + } + + return !quit; +} + +void game_draw() +{ + renderer_clear(); + + for (unsigned i = 0; i < shape_count; ++i) { + if (!shapes[i].visible) + continue; + + const mat3_t transform = transforms[shapes[i].entity]; + renderer_draw( + shapes[i].verts, shapes[i].vert_count, transform, + shapes[i].flags); + } +} diff --git a/game.h b/game.h new file mode 100644 index 0000000..d56f3aa --- /dev/null +++ b/game.h @@ -0,0 +1,8 @@ +#ifndef GAME_H +#define GAME_H + +void game_init(float aspect); +bool game_update(); +void game_draw(); + +#endif diff --git a/main.c b/main.c index 3e0d409..b36539d 100644 --- a/main.c +++ b/main.c @@ -1,305 +1,27 @@ +#include "game.h" #include "input.h" #include "renderer.h" -#include -#include #include -#include -#include #include -#include - -#define SHIP 0 - -#define FIRE 1 -#define FIRE_MEAN -0.15 -#define FIRE_JITTER 0.01 - -#define LIN_PWR 0.0001 -#define ROT_PWR 0.002 - -#define SHOT_VEL 0.04 - -#define MAX_SHAPES 256U -#define MAX_ENTITIES 128U -#define MAX_SHAPES_PER_ENTITY 2 #define MAX(a, b) ((a) < (b) ? (b) : (a)) -#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) - -typedef struct { - bool visible; - int entity; - unsigned vert_count; - vec2_t verts[MAX_VERTS]; - uint8_t flags; -} shape_t; - -typedef struct { - vec2_t pos; - vec2_t vel; - mat2_t dir; - float omg; - unsigned shapes[MAX_SHAPES_PER_ENTITY]; - unsigned shape_count; - bool wrap; -} entity_t; - -static vec2_t shot_verts[] = { { 0.0, -0.015 }, { 0.0, 0.015 } }; - -static shape_t shapes[MAX_SHAPES] = { - { - .visible = true, - .entity = SHIP, - .vert_count = 4, - .verts = { - { 0.0, 0.11 }, - { 0.05, -0.07 }, - { 0.0, -0.04 }, - { -0.05, -0.07 }, - }, - .flags = WRAP | CONNECT, - }, - { - .visible = false, - .entity = SHIP, - .vert_count = 3, - .verts = { - { 0.0, -0.15 }, - { 0.015, -0.07 }, - { -0.015, -0.07 }, - }, - .flags = WRAP | CONNECT, - }, -}; - -static entity_t entities[MAX_ENTITIES] = { - { - .dir = { { 1, 0 }, { 0, 1 } }, - .shape_count = 2, - .shapes = { 0, 1 }, - .wrap = true, - }, -}; - -static mat3_t transforms[MAX_ENTITIES]; - -static unsigned shape_count = 2; -static unsigned entity_count = 1; -static float aspect; -static bool quit = false; - -static struct { - int rot; - int fwd; -} input; - -static void shoot() -{ - if (entity_count >= MAX_ENTITIES || shape_count >= MAX_SHAPES) - return; - - const entity_t *ship = entities + SHIP; - const shape_t *ship_shape = shapes + SHIP; - - const int id = entity_count++; - entity_t *e = entities + id; - e->dir = ship->dir; - e->pos = vec2_add( - ship->pos, mat2_mul_vec2(ship->dir, ship_shape->verts[0])); - e->vel = vec2_add( - ship->vel, mat2_mul_vec2(ship->dir, (vec2_t) { 0, SHOT_VEL })); - e->omg = 0; - - const int shape_id = shape_count++; - shape_t *s = shapes + shape_id; - s->visible = true; - s->entity = id; - s->vert_count = NELEMS(shot_verts); - memcpy(s->verts, shot_verts, sizeof(shot_verts)); - s->flags = 0; - - e->shapes[0] = shape_id; - e->shape_count = 1; -} - -static void remove_shape(unsigned id) -{ - if (id < shape_count - 1) { - const shape_t *last = shapes + shape_count - 1; - memcpy(shapes + id, shapes + shape_count - 1, sizeof(shape_t)); - for (unsigned i = 0; i < entities[last->entity].shape_count; ++i) { - if (entities[last->entity].shapes[i] == shape_count - 1) { - entities[last->entity].shapes[i] = id; - break; - } - } - } - --shape_count; -} - -static void remove_entity(unsigned id) -{ - assert(id != SHIP); - - entity_t *e = entities + id; - for (unsigned i = 0; i < e->shape_count; ++i) - remove_shape(e->shapes[i]); - - if (id < entity_count - 1) { - const entity_t *last = entities + entity_count - 1; - memcpy(e, last, sizeof(entity_t)); - for (unsigned i = 0; i < e->shape_count; ++i) - shapes[e->shapes[i]].entity = id; - } - - --entity_count; -} - -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 mat3_t entity_transform(unsigned id) -{ - const entity_t *e = entities + id; - return mat3_mul_mat3(mat3_translation(e->pos), mat2_extend(e->dir)); -} - -static float rand_plusminus() -{ - return 2.0f * ((float)rand() / RAND_MAX - 0.5f); -} - -static void ship_update() -{ - entity_t *ship = entities + SHIP; - - ship->omg += ROT_PWR * (float)input.rot; - - const vec2_t thrust = { 0, (float)input.fwd * LIN_PWR }; - const vec2_t acc = mat2_mul_vec2(ship->dir, thrust); - ship->vel = vec2_add(ship->vel, acc); - - shapes[FIRE].visible = input.fwd != 0; - shapes[FIRE].verts[0].y = FIRE_MEAN + FIRE_JITTER * rand_plusminus(); -} - -static void update() -{ - ship_update(); - - for (unsigned i = 0; i < entity_count; ++i) { - entity_t *e = entities + i; - - e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir); - e->pos = vec2_add(e->pos, e->vel); - - if (e->wrap) { - if (e->pos.y > 1) - e->pos.y -= 2; - else if (e->pos.y <= -1) - e->pos.y += 2; - if (e->pos.x >= aspect) - e->pos.x -= 2 * aspect; - else if (e->pos.x < -aspect) - e->pos.x += 2 * aspect; - } else if ( - e->pos.y > 1 || e->pos.y <= -1 || e->pos.x >= aspect - || e->pos.x < -aspect) { - remove_entity(i--); - continue; - } - - transforms[i] = entity_transform(i); - } -} - -static void draw() -{ - for (unsigned i = 0; i < shape_count; ++i) { - if (!shapes[i].visible) - continue; - - const mat3_t transform = transforms[shapes[i].entity]; - renderer_draw( - shapes[i].verts, shapes[i].vert_count, transform, - shapes[i].flags); - } -} int main() { - struct timeval tv; - gettimeofday(&tv, NULL); - srand(tv.tv_usec); - const int input_fd = input_init(); - input_on_press(key_press_callback); - input_on_release(key_release_callback); - input_on_repeat(key_repeat_callback); const renderer_params_t renderer_params = renderer_init(); - aspect = renderer_params.aspect; const int drm_fd = renderer_params.drm_fd; renderer_clear(); renderer_swap(); + game_init(renderer_params.aspect); + const int max_fd = MAX(input_fd, drm_fd); fd_set set; - while (!quit) { + while (1) { FD_ZERO(&set); FD_SET(input_fd, &set); FD_SET(drm_fd, &set); @@ -312,10 +34,9 @@ int main() if (FD_ISSET(drm_fd, &set)) { renderer_handle(); - update(); - - renderer_clear(); - draw(); + if (!game_update()) + break; + game_draw(); renderer_swap(); }