Wrap all entities and shapes

This commit is contained in:
2025-10-18 17:43:22 +01:00
parent 8d13b862c5
commit fe3c615517
3 changed files with 32 additions and 50 deletions

44
game.c
View File

@@ -28,14 +28,14 @@
#define ASTEROID_R_JITTER 0.02
#define ASTEROID_MIN_DIST 0.8
#define REPLACE_MIN 2
#define REPLACE_MAX 4
#define REPLACE_MIN 3
#define REPLACE_MAX 5
#define REPLACE_RANGE (REPLACE_MAX - REPLACE_MIN)
#define REPLACE_R_COEFF 0.5
#define REPLACE_R_JITTER 0.02
#define REPLACE_A_JITTER 0.4
#define REPLACE_V_JITTER 0.001
#define REPLACE_RADIAL_V_COEFF 0.005
#define REPLACE_RADIAL_V_COEFF 0.006
#define REPLACE_OMG_JITTER 0.01
#define LIN_PWR 0.0001
@@ -43,7 +43,7 @@
#define SHOT_VEL 0.04
#define SHOT_COLLIDE_R 0.005
#define SHOT_MASS 0.005
#define SHOT_MASS 0.01
#define MAX_SHAPES 256U
#define MAX_ENTITIES 128U
@@ -67,7 +67,6 @@ typedef struct {
unsigned shape_count;
float radius;
collision_tag_t tag;
bool wrap;
bool dead;
} entity_t;
@@ -76,7 +75,7 @@ typedef struct {
int entity;
unsigned vert_count;
vec2_t verts[MAX_VERTS];
uint8_t flags;
bool connect;
} shape_t;
typedef struct {
@@ -162,13 +161,12 @@ static entity_t *gen_asteroid(float r_mean, unsigned *id_out)
{
unsigned id;
entity_t *e = add_entity(&id);
e->wrap = true;
e->radius = r_mean;
e->tag = COLLISION_ASTEROID;
shape_t *s = add_shape(id, nullptr);
s->visible = true;
s->flags = WRAP | CONNECT;
s->connect = true;
const unsigned n
= ASTEROID_MIN_VERTS + rng_uint32() % ASTEROID_VERT_RANGE;
@@ -407,19 +405,18 @@ void game_init(float _aspect)
entity_count = shape_count = 0;
entity_t *ship = add_entity(&ship_entity_id);
ship->wrap = true;
ship->radius = SHIP_COLLIDE_R;
ship->tag = COLLISION_SHIP;
shape_t *ship_shape = add_shape(ship_entity_id, &ship_shape_id);
ship_shape->visible = true;
ship_shape->flags = WRAP | CONNECT;
ship_shape->connect = true;
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->connect = true;
fire->vert_count = NELEMS(fire_verts);
memcpy(fire->verts, fire_verts, sizeof(fire_verts));
@@ -440,21 +437,14 @@ void game_update()
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;
}
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;
transforms[i]
= mat3_mul_mat3(mat3_translation(e->pos), mat2_extend(e->dir));
@@ -476,6 +466,6 @@ void game_draw()
const mat3_t transform = transforms[shapes[i].entity];
renderer_draw(
shapes[i].verts, shapes[i].vert_count, transform,
shapes[i].flags);
shapes[i].connect);
}
}

View File

@@ -32,7 +32,7 @@ static void set_pixel(uint32_t x, uint32_t y)
fbs[back].buf[offset] = 0xff'ff'ff;
}
static void draw_line(vec3_t v1, vec3_t v2, uint8_t flags)
static void draw_line(vec3_t v1, vec3_t v2)
{
const float delta_x = v2.x - v1.x;
const float delta_y = v2.y - v1.y;
@@ -43,20 +43,16 @@ static void draw_line(vec3_t v1, vec3_t v2, uint8_t flags)
for (unsigned i = 0; i <= step; ++i) {
float x = roundf(v1.x + i * step_x);
float y = roundf(v1.y + i * step_y);
if (x >= width)
x -= width;
else if (x < 0)
x += width;
if (flags & WRAP) {
if (x >= width)
x -= width;
else if (x < 0)
x += width;
if (y >= height)
y -= height;
else if (y < 0)
y += height;
} else if (x >= width || x < 0 || y >= height || y < 0) {
continue;
}
float y = roundf(v1.y + i * step_y);
if (y >= height)
y -= height;
else if (y < 0)
y += height;
set_pixel(x, y);
}
@@ -146,7 +142,7 @@ void renderer_swap()
}
void renderer_draw(
const vec2_t *vs, unsigned count, mat3_t model, uint8_t flags)
const vec2_t *vs, unsigned count, mat3_t model, bool connect)
{
mat3_t transform = mat3_mul_mat3(view, model);
assert(count < MAX_VERTS);
@@ -154,8 +150,7 @@ void renderer_draw(
for (unsigned i = 0; i < count; ++i)
vert_buf[i] = mat3_mul_vec3(transform, vec2_extend(vs[i]));
for (unsigned i = 1; i < count; ++i)
draw_line(vert_buf[i - 1], vert_buf[i], flags);
if (flags & CONNECT)
draw_line(vert_buf[count - 1], vert_buf[0], flags);
draw_line(vert_buf[i - 1], vert_buf[i]);
if (connect)
draw_line(vert_buf[count - 1], vert_buf[0]);
}

View File

@@ -7,9 +7,6 @@
#define MAX_VERTS 8U
#define WRAP 1
#define CONNECT (1 << 1)
typedef struct {
int drm_fd;
float aspect;
@@ -23,6 +20,6 @@ void renderer_clear();
void renderer_swap();
void renderer_draw(
const vec2_t *vs, unsigned count, mat3_t model, uint8_t flags);
const vec2_t *vs, unsigned count, mat3_t model, bool connect);
#endif