Allow display wrapping to be specified per shape

This commit is contained in:
Camden Dixie O'Brien
2025-10-13 22:11:30 +01:00
parent bbb13108e0
commit 794a149f00
3 changed files with 39 additions and 22 deletions

13
main.c
View File

@@ -32,6 +32,7 @@ typedef struct {
int entity;
unsigned vert_count;
vec2_t verts[MAX_VERTS];
uint8_t flags;
} shape_t;
typedef struct {
@@ -57,6 +58,7 @@ static shape_t shapes[MAX_SHAPES] = {
{ 0.0, -0.04 },
{ -0.05, -0.07 },
},
.flags = WRAP | CONNECT,
},
{
.visible = false,
@@ -67,6 +69,7 @@ static shape_t shapes[MAX_SHAPES] = {
{ 0.015, -0.07 },
{ -0.015, -0.07 },
},
.flags = WRAP | CONNECT,
},
};
@@ -114,6 +117,7 @@ static void shoot()
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;
@@ -251,13 +255,12 @@ static void update()
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
} 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);
}
@@ -270,7 +273,9 @@ static void draw()
continue;
const mat3_t transform = transforms[shapes[i].entity];
renderer_draw(shapes[i].verts, shapes[i].vert_count, transform);
renderer_draw(
shapes[i].verts, shapes[i].vert_count, transform,
shapes[i].flags);
}
}

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)
static void draw_line(vec3_t v1, vec3_t v2, uint8_t flags)
{
const float delta_x = v2.x - v1.x;
const float delta_y = v2.y - v1.y;
@@ -43,16 +43,20 @@ static void draw_line(vec3_t v1, vec3_t v2)
for (unsigned i = 0; i <= step; ++i) {
float x = roundf(v1.x + i * step_x);
float y = roundf(v1.y + i * step_y);
if (flags & WRAP) {
if (x >= width)
x -= width;
else if (x < 0)
x += width;
float y = roundf(v1.y + i * step_y);
if (y >= height)
y -= height;
else if (y < 0)
y += height;
} else if (x >= width || x < 0 || y >= height || y < 0) {
continue;
}
set_pixel(x, y);
}
@@ -141,13 +145,17 @@ void renderer_swap()
back = (back + 1) & 1;
}
void renderer_draw(const vec2_t *vs, unsigned count, mat3_t model)
void renderer_draw(
const vec2_t *vs, unsigned count, mat3_t model, uint8_t flags)
{
mat3_t transform = mat3_mul_mat3(view, model);
assert(count < MAX_VERTS);
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]);
draw_line(vert_buf[count - 1], vert_buf[0]);
draw_line(vert_buf[i - 1], vert_buf[i], flags);
if (flags & CONNECT)
draw_line(vert_buf[count - 1], vert_buf[0], flags);
}

View File

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