Files
asteroids/main.c
2025-10-13 21:10:43 +01:00

215 lines
3.5 KiB
C

#include "input.h"
#include "renderer.h"
#include <linux/input-event-codes.h>
#include <stdio.h>
#include <sys/select.h>
#define SHIP 0
#define FIRE 1
#define LIN_PWR 0.0001
#define ROT_PWR 0.002
#define MAX_SHAPES 8U
#define MAX_ENTITIES 8U
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#define NELEMS(arr) (sizeof(arr) / sizeof(arr[0]))
typedef struct {
bool visible;
int entity;
unsigned vert_count;
vec2_t verts[MAX_VERTS];
} shape_t;
typedef struct {
vec2_t pos;
vec2_t vel;
mat2_t dir;
float omg;
} entity_t;
static shape_t shapes[MAX_SHAPES] = {
{
.visible = true,
.entity = SHIP,
.vert_count = 4,
.verts = {
{ 0.0, 0.11 },
{ 0.05, -0.07 },
{ 0.0, -0.04 },
{ -0.05, -0.07 },
},
},
{
.visible = false,
.entity = SHIP,
.vert_count = 3,
.verts = {
{ 0.015, -0.07 },
{ -0.015, -0.07 },
{ 0.0, -0.15 },
},
},
};
static entity_t entities[MAX_ENTITIES] = {
{ .dir = { { 1, 0 }, { 0, 1 } } },
};
static unsigned shape_count = 2;
static unsigned entity_count = 1;
static float aspect;
static bool quit = false;
static struct {
int rot;
int fwd;
} input;
static void key_press_callback(int key)
{
switch (key) {
case KEY_Q:
quit = true;
break;
case KEY_UP:
++input.fwd;
break;
case KEY_LEFT:
++input.rot;
break;
case KEY_RIGHT:
--input.rot;
break;
default:
break;
}
}
static void key_release_callback(int key)
{
switch (key) {
case KEY_Q:
quit = true;
break;
case KEY_UP:
--input.fwd;
break;
case KEY_LEFT:
--input.rot;
break;
case KEY_RIGHT:
++input.rot;
break;
default:
break;
}
}
static mat3_t entity_transform(unsigned id)
{
const entity_t *e = entities + id;
return mat3_mul_mat3(mat3_translation(e->pos), mat2_extend(e->dir));
}
static void ship_update()
{
entity_t *ship = entities + SHIP;
ship->omg += ROT_PWR * (float)input.rot;
const vec2_t thrust = { 0, (float)input.fwd * LIN_PWR };
const vec2_t acc = mat2_mul_vec2(ship->dir, thrust);
ship->vel = vec2_add(ship->vel, acc);
shapes[FIRE].visible = input.fwd != 0;
}
static void update()
{
ship_update();
for (unsigned i = 0; i < entity_count; ++i) {
entity_t *e = entities + i;
e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir);
e->pos = vec2_add(e->pos, e->vel);
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;
}
}
static void draw()
{
static mat3_t transforms[MAX_ENTITIES];
for (unsigned i = 0; i < entity_count; ++i)
transforms[i] = entity_transform(i);
for (unsigned i = 0; i < shape_count; ++i) {
if (!shapes[i].visible)
continue;
const mat3_t transform = transforms[shapes[i].entity];
renderer_draw(shapes[i].verts, shapes[i].vert_count, transform);
}
}
int main()
{
const int input_fd = input_init();
input_on_press(key_press_callback);
input_on_release(key_release_callback);
const renderer_params_t renderer_params = renderer_init();
aspect = renderer_params.aspect;
const int drm_fd = renderer_params.drm_fd;
renderer_clear();
renderer_swap();
const int max_fd = MAX(input_fd, drm_fd);
fd_set set;
while (!quit) {
FD_ZERO(&set);
FD_SET(input_fd, &set);
FD_SET(drm_fd, &set);
select(max_fd + 1, &set, nullptr, nullptr, nullptr);
if (FD_ISSET(input_fd, &set))
input_handle();
if (FD_ISSET(drm_fd, &set)) {
renderer_handle();
update();
renderer_clear();
draw();
renderer_swap();
}
}
renderer_cleanup();
input_cleanup();
return 0;
}