diff --git a/main.c b/main.c index f5d15c6..4065946 100644 --- a/main.c +++ b/main.c @@ -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,12 +255,11 @@ 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 - || e->pos.x < -aspect) { - remove_entity(i--); - continue; - } + } 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); } } diff --git a/renderer.c b/renderer.c index 61c52c6..bd52ba6 100644 --- a/renderer.c +++ b/renderer.c @@ -28,11 +28,11 @@ static void page_flip_handler(int, unsigned, unsigned, unsigned, void *) static void set_pixel(uint32_t x, uint32_t y) { - const uint32_t offset = y * (fbs[back].pitch / 4) + x; + const uint32_t offset = y * (fbs[back].pitch / 4) + x; 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); - 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; + + 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; + } 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); } diff --git a/renderer.h b/renderer.h index 00b1007..87cf451 100644 --- a/renderer.h +++ b/renderer.h @@ -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