Allow display wrapping to be specified per shape
This commit is contained in:
13
main.c
13
main.c
@@ -32,6 +32,7 @@ typedef struct {
|
|||||||
int entity;
|
int entity;
|
||||||
unsigned vert_count;
|
unsigned vert_count;
|
||||||
vec2_t verts[MAX_VERTS];
|
vec2_t verts[MAX_VERTS];
|
||||||
|
uint8_t flags;
|
||||||
} shape_t;
|
} shape_t;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
@@ -57,6 +58,7 @@ static shape_t shapes[MAX_SHAPES] = {
|
|||||||
{ 0.0, -0.04 },
|
{ 0.0, -0.04 },
|
||||||
{ -0.05, -0.07 },
|
{ -0.05, -0.07 },
|
||||||
},
|
},
|
||||||
|
.flags = WRAP | CONNECT,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
.visible = false,
|
.visible = false,
|
||||||
@@ -67,6 +69,7 @@ static shape_t shapes[MAX_SHAPES] = {
|
|||||||
{ 0.015, -0.07 },
|
{ 0.015, -0.07 },
|
||||||
{ -0.015, -0.07 },
|
{ -0.015, -0.07 },
|
||||||
},
|
},
|
||||||
|
.flags = WRAP | CONNECT,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -114,6 +117,7 @@ static void shoot()
|
|||||||
s->entity = id;
|
s->entity = id;
|
||||||
s->vert_count = NELEMS(shot_verts);
|
s->vert_count = NELEMS(shot_verts);
|
||||||
memcpy(s->verts, shot_verts, sizeof(shot_verts));
|
memcpy(s->verts, shot_verts, sizeof(shot_verts));
|
||||||
|
s->flags = 0;
|
||||||
|
|
||||||
e->shapes[0] = shape_id;
|
e->shapes[0] = shape_id;
|
||||||
e->shape_count = 1;
|
e->shape_count = 1;
|
||||||
@@ -251,13 +255,12 @@ static void update()
|
|||||||
e->pos.x -= 2 * aspect;
|
e->pos.x -= 2 * aspect;
|
||||||
else if (e->pos.x < -aspect)
|
else if (e->pos.x < -aspect)
|
||||||
e->pos.x += 2 * aspect;
|
e->pos.x += 2 * aspect;
|
||||||
} else {
|
} else if (
|
||||||
if (e->pos.y > 1 || e->pos.y <= -1 || e->pos.x >= aspect
|
e->pos.y > 1 || e->pos.y <= -1 || e->pos.x >= aspect
|
||||||
|| e->pos.x < -aspect) {
|
|| e->pos.x < -aspect) {
|
||||||
remove_entity(i--);
|
remove_entity(i--);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
transforms[i] = entity_transform(i);
|
transforms[i] = entity_transform(i);
|
||||||
}
|
}
|
||||||
@@ -270,7 +273,9 @@ static void draw()
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
const mat3_t transform = transforms[shapes[i].entity];
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
20
renderer.c
20
renderer.c
@@ -32,7 +32,7 @@ static void set_pixel(uint32_t x, uint32_t y)
|
|||||||
fbs[back].buf[offset] = 0xff'ff'ff;
|
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_x = v2.x - v1.x;
|
||||||
const float delta_y = v2.y - v1.y;
|
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) {
|
for (unsigned i = 0; i <= step; ++i) {
|
||||||
float x = roundf(v1.x + i * step_x);
|
float x = roundf(v1.x + i * step_x);
|
||||||
|
float y = roundf(v1.y + i * step_y);
|
||||||
|
|
||||||
|
if (flags & WRAP) {
|
||||||
if (x >= width)
|
if (x >= width)
|
||||||
x -= width;
|
x -= width;
|
||||||
else if (x < 0)
|
else if (x < 0)
|
||||||
x += width;
|
x += width;
|
||||||
|
|
||||||
float y = roundf(v1.y + i * step_y);
|
|
||||||
if (y >= height)
|
if (y >= height)
|
||||||
y -= height;
|
y -= height;
|
||||||
else if (y < 0)
|
else if (y < 0)
|
||||||
y += height;
|
y += height;
|
||||||
|
} else if (x >= width || x < 0 || y >= height || y < 0) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
set_pixel(x, y);
|
set_pixel(x, y);
|
||||||
}
|
}
|
||||||
@@ -141,13 +145,17 @@ void renderer_swap()
|
|||||||
back = (back + 1) & 1;
|
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);
|
mat3_t transform = mat3_mul_mat3(view, model);
|
||||||
assert(count < MAX_VERTS);
|
assert(count < MAX_VERTS);
|
||||||
|
|
||||||
for (unsigned i = 0; i < count; ++i)
|
for (unsigned i = 0; i < count; ++i)
|
||||||
vert_buf[i] = mat3_mul_vec3(transform, vec2_extend(vs[i]));
|
vert_buf[i] = mat3_mul_vec3(transform, vec2_extend(vs[i]));
|
||||||
for (unsigned i = 1; i < count; ++i)
|
for (unsigned i = 1; i < count; ++i)
|
||||||
draw_line(vert_buf[i - 1], vert_buf[i]);
|
draw_line(vert_buf[i - 1], vert_buf[i], flags);
|
||||||
draw_line(vert_buf[count - 1], vert_buf[0]);
|
|
||||||
|
if (flags & CONNECT)
|
||||||
|
draw_line(vert_buf[count - 1], vert_buf[0], flags);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,9 @@
|
|||||||
|
|
||||||
#define MAX_VERTS 8U
|
#define MAX_VERTS 8U
|
||||||
|
|
||||||
|
#define WRAP 1
|
||||||
|
#define CONNECT (1 << 1)
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
int drm_fd;
|
int drm_fd;
|
||||||
float aspect;
|
float aspect;
|
||||||
@@ -19,6 +22,7 @@ void renderer_handle();
|
|||||||
void renderer_clear();
|
void renderer_clear();
|
||||||
void renderer_swap();
|
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
|
#endif
|
||||||
|
|||||||
Reference in New Issue
Block a user