Compare commits

..

1 Commits

Author SHA1 Message Date
82afb4fdd1 Make everything 2x smaller 2025-10-19 13:07:28 +01:00
5 changed files with 7 additions and 85 deletions

6
README
View File

@@ -3,10 +3,8 @@
This is a clone (ish) of the classic game Asteroids, written in C23 This is a clone (ish) of the classic game Asteroids, written in C23
for Linux. To build the program, you'll need the Linux headers, a for Linux. To build the program, you'll need the Linux headers, a
C23-compatible compiler and libdrm, along with its headers. There is C23-compatible compiler and libdrm, along with its headers. There is
a build script that uses GCC; simply running this (./build.sh) is a build script that uses GCC; simply running this (`./build.sh`) is
likely to work on many systems. However, you will likely need to likely to work on many systems.
adjust the input and video devices used (in input.c and renderer.c
respectively) to match your system.
The game directly uses Linux DRM, so it doesn't play nice with X, The game directly uses Linux DRM, so it doesn't play nice with X,
display managers etc. You'll likely want to change to a virtual TTY display managers etc. You'll likely want to change to a virtual TTY

View File

@@ -9,5 +9,4 @@ defs="-D_POSIX_C_SOURCE=200809L"
$cc $warn $flags $libs $defs \ $cc $warn $flags $libs $defs \
-o asteroids \ -o asteroids \
asteroids.c collisions.c entity.c fb.c game.c input.c \ asteroids.c collisions.c entity.c fb.c game.c input.c \
main.c maths.c physics.c scene.c renderer.c rng.c \ main.c maths.c physics.c scene.c renderer.c rng.c text.c
self_destruct.c text.c

15
game.c
View File

@@ -8,7 +8,6 @@
#include "renderer.h" #include "renderer.h"
#include "rng.h" #include "rng.h"
#include "scene.h" #include "scene.h"
#include "self_destruct.h"
#include "text.h" #include "text.h"
#include <assert.h> #include <assert.h>
@@ -16,8 +15,6 @@
#include <math.h> #include <math.h>
#include <string.h> #include <string.h>
#define FPS 60
#define INIT_LEVEL 1 #define INIT_LEVEL 1
#define SHIP_COLLIDE_R 0.025 #define SHIP_COLLIDE_R 0.025
@@ -26,12 +23,11 @@
#define FIRE_JITTER 0.005 #define FIRE_JITTER 0.005
#define LIN_PWR 0.00005 #define LIN_PWR 0.00005
#define ROT_PWR 0.002 #define ROT_PWR 0.001
#define SHOT_VEL 0.03 #define SHOT_VEL 0.015
#define SHOT_COLLIDE_R 0.015 #define SHOT_COLLIDE_R 0.015
#define SHOT_MASS 0.00025 #define SHOT_MASS 0.0005
#define SHOT_LIFETIME_S 4
#define ARROW_SCALE 0.0125 #define ARROW_SCALE 0.0125
#define ARROW_WIDTH 4 #define ARROW_WIDTH 4
@@ -178,7 +174,6 @@ static void shoot()
scene_add(id, shot_verts, NELEMS(shot_verts), false); scene_add(id, shot_verts, NELEMS(shot_verts), false);
physics_add(id, pos, ship->dir, vel, 0, SHOT_MASS); physics_add(id, pos, ship->dir, vel, 0, SHOT_MASS);
collisions_add(id, SHOT_COLLIDE_R, SHOT_COLLIDE_PRIOR, shot_collide); collisions_add(id, SHOT_COLLIDE_R, SHOT_COLLIDE_PRIOR, shot_collide);
self_destruct_add(id, FPS * SHOT_LIFETIME_S);
const vec2_t p_ship = vec2_scale(ship->vel, ship->mass); const vec2_t p_ship = vec2_scale(ship->vel, ship->mass);
const vec2_t p_shot = vec2_scale(vel, SHOT_MASS); const vec2_t p_shot = vec2_scale(vel, SHOT_MASS);
@@ -218,7 +213,6 @@ static void create_field()
scene_clear(); scene_clear();
physics_init(); physics_init();
collisions_init(); collisions_init();
self_destruct_clear();
ship_entity_id = entity_add(); ship_entity_id = entity_add();
scene_add(ship_entity_id, ship_verts, NELEMS(ship_verts), true); scene_add(ship_entity_id, ship_verts, NELEMS(ship_verts), true);
@@ -315,7 +309,6 @@ void game_update()
ship_update(); ship_update();
physics_update(); physics_update();
self_destruct_update();
collisions_update(); collisions_update();
scene_update(); scene_update();
@@ -324,7 +317,7 @@ void game_update()
const bool items_enabled const bool items_enabled
= !clear && score >= ITEM_MIN_SCORE && armour_level < 9; = !clear && score >= ITEM_MIN_SCORE && armour_level < 9;
if (items_enabled && !item if (items_enabled && !item
&& rng_canon() < 1.0f / (FPS * ITEM_SPAWN_EXP_S)) && rng_canon() < 1.0f / (60 * ITEM_SPAWN_EXP_S))
add_item(); add_item();
} }

View File

@@ -1,60 +0,0 @@
#include "self_destruct.h"
#include "entity.h"
#include <assert.h>
#include <string.h>
#define MAX 256U
typedef struct {
unsigned entity_id;
unsigned component_id;
unsigned rem;
} cmp_t;
static unsigned count;
static cmp_t cmps[MAX];
static void update(unsigned new_entity_id, void *ref)
{
cmp_t *c = (cmp_t *)ref;
c->entity_id = new_entity_id;
}
static void remove(void *ref)
{
cmp_t *c = (cmp_t *)ref;
const cmp_t *last = cmps + (count - 1);
if (c < last) {
memcpy(c, last, sizeof(cmp_t));
entity_update_component(c->entity_id, c->component_id, c);
}
--count;
}
void self_destruct_clear()
{
count = 0;
}
void self_destruct_update()
{
for (unsigned i = 0; i < count; ++i) {
if (cmps[i].rem == 0)
entity_mark(cmps[i].entity_id);
else
--cmps[i].rem;
}
}
void self_destruct_add(unsigned id, unsigned frames)
{
assert(count < MAX);
cmp_t *c = cmps + count++;
*c = (cmp_t) {
.entity_id = id,
.rem = frames,
.component_id = entity_add_component(id, update, remove, c),
};
}

View File

@@ -1,8 +0,0 @@
#ifndef SELF_DESTRUCT_H
#define SELF_DESTRUCT_H
void self_destruct_clear();
void self_destruct_update();
void self_destruct_add(unsigned id, unsigned frames);
#endif