From 03f1bedc6143d1934c7d44a3410d1b8b442d3c23 Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Thu, 16 Oct 2025 14:22:10 +0100 Subject: [PATCH] Add pausing --- game.c | 43 ++++++++++++++++++++++++++++++++++--------- input.c | 11 ++++++++++- input.h | 1 + text.c | 31 +++++++++++++++++++++++++++++++ 4 files changed, 76 insertions(+), 10 deletions(-) diff --git a/game.c b/game.c index 4587cde..97ae085 100644 --- a/game.c +++ b/game.c @@ -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"); diff --git a/input.c b/input.c index 47b4e18..10d00ea 100644 --- a/input.c +++ b/input.c @@ -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; diff --git a/input.h b/input.h index 7347bae..2ae6798 100644 --- a/input.h +++ b/input.h @@ -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(); diff --git a/text.c b/text.c index 07c0f1a..5ee8d4f 100644 --- a/text.c +++ b/text.c @@ -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 },