Add pausing

This commit is contained in:
2025-10-18 17:43:22 +01:00
parent 07616120d3
commit 5604675ca6
4 changed files with 76 additions and 10 deletions

43
game.c
View File

@@ -123,6 +123,9 @@ static float aspect;
static unsigned level;
static bool dead;
static bool clear;
static bool paused;
static unsigned asteroid_count;
static uint8_t counter;
static unsigned score;
@@ -233,10 +236,10 @@ static void spawn_asteroid()
const float rnd = rng_canon();
if (rnd < 0.4)
r = ASTEROID_MEDIUM;
else if (rnd < 0.8)
r = ASTEROID_LARGE;
else
else if (level > 3 && rnd < 0.45)
r = ASTEROID_HUGE;
else
r = ASTEROID_LARGE;
unsigned id;
entity_t *e = gen_asteroid(r, &id);
@@ -349,7 +352,9 @@ static void bounce(collision_t c)
static void cleared()
{
clear = true;
renderer_set_wrap(false);
counter = 0;
}
static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
@@ -401,6 +406,13 @@ static void destroy_asteroid(entity_t *a, vec2_t shot_vel)
}
}
static void die()
{
dead = true;
clear = false;
counter = 0;
}
static void handle_collisions(const collision_t *collisions, unsigned count)
{
for (unsigned i = 0; i < count; ++i) {
@@ -409,7 +421,7 @@ static void handle_collisions(const collision_t *collisions, unsigned count)
entity_t *b = entities + c.entities[1];
if (a->tag == COLLISION_SHIP || b->tag == COLLISION_SHIP) {
dead = true;
die();
continue;
}
@@ -459,7 +471,8 @@ static void draw_arrow()
static void create_field()
{
dead = false;
counter = 0;
clear = false;
paused = false;
entity_count = shape_count = 0;
asteroid_count = 0;
@@ -497,10 +510,17 @@ static void reset()
create_field();
}
static void pause()
{
paused = !paused;
counter = 0;
}
void game_init(float _aspect)
{
input_on_shoot(shoot);
input_on_restart(reset);
input_on_pause(pause);
aspect = _aspect;
reset();
@@ -508,9 +528,9 @@ void game_init(float _aspect)
void game_update()
{
if (dead || asteroid_count == 0)
if (dead || clear || paused)
++counter;
if (dead)
if (dead || paused)
return;
ship_update();
@@ -521,7 +541,7 @@ void game_update()
e->dir = mat2_mul_mat2(mat2_rotation(e->omg), e->dir);
e->pos = vec2_add(e->pos, e->vel);
if (asteroid_count == 0 && i == ship_entity_id) {
if (clear && i == ship_entity_id) {
if (e->pos.x > aspect)
win();
}
@@ -560,10 +580,15 @@ void game_draw()
text_draw_score(aspect, score);
if (paused && !(counter & COUNTER_MASK)) {
text_draw_centre("PAUSED");
return;
}
if (dead && !(counter & COUNTER_MASK))
text_draw_centre("GAME OVER");
if (!dead && asteroid_count == 0) {
if (clear) {
draw_arrow();
if (!(counter & COUNTER_MASK))
text_draw_centre("CLEAR");

11
input.c
View File

@@ -19,6 +19,7 @@ static int input_fd;
static input_callback_t restart;
static input_callback_t quit;
static input_callback_t shoot;
static input_callback_t pause_;
static void key_press(int key)
{
@@ -46,6 +47,9 @@ static void key_press(int key)
if (shoot != nullptr)
shoot();
break;
case KEY_P:
if (pause_ != nullptr)
pause_();
}
}
@@ -74,7 +78,7 @@ static void key_repeat(int key)
int input_init()
{
memset(&input, 0, sizeof(input));
shoot = nullptr;
shoot = restart = quit = pause_ = nullptr;
input_fd = open("/dev/input/event0", O_RDONLY);
assert(input_fd != -1);
@@ -105,6 +109,11 @@ void input_on_quit(input_callback_t cb)
quit = cb;
}
void input_on_pause(input_callback_t cb)
{
pause_ = cb;
}
void input_handle()
{
struct input_event ev;

View File

@@ -16,6 +16,7 @@ void input_cleanup();
void input_on_shoot(input_callback_t cb);
void input_on_restart(input_callback_t cb);
void input_on_quit(input_callback_t cb);
void input_on_pause(input_callback_t cb);
void input_handle();

31
text.c
View File

@@ -130,6 +130,13 @@ static const glyph_t font[] = {
{ { 1, 4 }, { -1, 4 }, { -1, -4 }, { 1, -4 } },
},
},
['D'] = {
.line_count = 1,
.line_lens = { 5 },
.lines = {
{ { -1, 4 }, { -1, -4 }, { 1, -4 }, { 0, 4 }, { -1, 4 } },
},
},
['E'] = {
.line_count = 2,
.line_lens = { 4, 2 },
@@ -166,6 +173,13 @@ static const glyph_t font[] = {
{ { -1, 4 }, { -1, -4 }, { 1, -4 }, { 1, 4 }, { -1, 4 } },
},
},
['P'] = {
.line_count = 1,
.line_lens = { 5 },
.lines = {
{ { -1, -4 }, { -1, 4 }, { 1, 4 }, { 1, -2 }, { -1, -2 } },
},
},
['R'] = {
.line_count = 1,
.line_lens = { 6 },
@@ -176,6 +190,23 @@ static const glyph_t font[] = {
},
},
},
['S'] = {
.line_count = 1,
.line_lens = { 6 },
.lines = {
{
{ 1, 4 }, { -1, 4 }, { -1, -2 },
{ 1, -2 }, { 1, -4 }, { -1, -4 },
},
},
},
['U'] = {
.line_count = 1,
.line_lens = { 4 },
.lines = {
{ { -1, 4 }, { -1, -4 }, { 1, -4 }, { 1, 4 } },
},
},
['V'] = {
.line_count = 1,
.line_lens = { 3 },