From a1de326ac5d89cbf9555c9a0a9a662af35c6dcf7 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Thu, 15 Jun 2023 22:58:30 +0100 Subject: Implement a countdown clock in the timer Updated the HUD to show the time. --- src/game.c | 13 ++++++++++++- src/game.h | 5 +++-- src/keyb.h | 2 +- src/main.c | 10 +++++++++- src/timer.c | 45 ++++++++++++++++++++++++++++++++++++++++----- src/timer.h | 7 +++++-- 6 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/game.c b/src/game.c index 70c3235..b49e859 100644 --- a/src/game.c +++ b/src/game.c @@ -12,12 +12,14 @@ static uint32_t hiscore = 15000; static uint8_t hud; +static volatile uint8_t clock_updated; /* game variables */ static uint8_t lives; static uint32_t score; static uint8_t stage; static uint8_t gold; +static uint8_t time; void hud_render() { @@ -49,7 +51,7 @@ void hud_render() if (hud & HUD_TIME) { - sprintf(b, "%02d", 0); + sprintf(b, "%02d", time); put_text(172, 4, b); } @@ -68,14 +70,23 @@ void run_game() score = 0; stage = 0; gold = 30; + time = GAME_TIME_MAX; hud = HUD_ALL; map_init(binary_stage_start); map_render(); + timer_start(GAME_TIME_MAX, &clock_updated); + while (!keys[KEY_ESC]) { + if (clock_updated) + { + time = timer_value(); + hud |= HUD_TIME; + } + if (hud) hud_render(); diff --git a/src/game.h b/src/game.h index e8c270e..ff90121 100644 --- a/src/game.h +++ b/src/game.h @@ -1,8 +1,9 @@ #ifndef _GAME_H #define _GAME_H -#define GAME_LIVES_START 3 -#define GAME_LIVES_MAX 9 +#define GAME_LIVES_START 3 +#define GAME_LIVES_MAX 9 +#define GAME_TIME_MAX 60 #define HUD_CLEAN 0 #define HUD_LIVES 1 diff --git a/src/keyb.h b/src/keyb.h index 5e47502..3a9e10c 100644 --- a/src/keyb.h +++ b/src/keyb.h @@ -9,7 +9,7 @@ void keyb_free(); #define KEY_ESC 1 #define KEY_TAB 15 #define KEY_ENTER 28 -#define KEY_SPACEBAR 57 +#define KEY_SPACE 57 #define KEY_A 30 #define KEY_S 31 diff --git a/src/main.c b/src/main.c index 306f5f1..0711cf9 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include #include +#include "timer.h" #include "keyb.h" #include "vga.h" #include "data.h" @@ -12,10 +13,17 @@ /* disable paging because our int handlers are written in C */ int _crt0_startup_flags = _CRT0_FLAG_LOCK_MEMORY; +void free_all() +{ + timer_free(); + keyb_free(); +} + int main(int argc, char *argv[]) { + timer_init(); keyb_init(); - atexit(keyb_free); + atexit(free_all); /* set VGA 320x200, 256 col */ if (!set_mode(0x13)) diff --git a/src/timer.c b/src/timer.c index d7810b6..ae76037 100644 --- a/src/timer.c +++ b/src/timer.c @@ -4,14 +4,31 @@ #include volatile uint32_t ticks = 0; -volatile static uint32_t ticks_to = 0; +volatile uint32_t clock = 0; +volatile uint8_t clock_secs = 0; +volatile uint8_t clock_enabled = 0; +volatile uint8_t *clock_updated = NULL; static _go32_dpmi_seginfo old_handler, new_handler; static void timer_handler() { ticks++; - ticks_to++; + + if (clock_enabled) + { + clock += 5494; + if (clock > 100000) + { + clock -= 100000; + if (clock_secs) + { + clock_secs--; + if (clock_updated) + *clock_updated = 1; + } + } + } } void timer_init() @@ -28,8 +45,26 @@ void timer_free() fprintf(stderr, "Failed to free the timer :(\n"); } -void timer_wait(uint8_t t) +void timer_start(uint8_t secs, uint8_t *updated) +{ + *updated = 0; + clock_updated = updated; + clock_secs = secs; + clock_enabled = 1; +} + +uint8_t timer_value() +{ + *clock_updated = 0; + return clock_secs; +} + +void timer_stop() +{ + clock_enabled = 0; +} + +void timer_resume() { - while (ticks_to < t); - ticks_to = 0; + clock_enabled = 1; } diff --git a/src/timer.h b/src/timer.h index 8ae7e60..3f66be5 100644 --- a/src/timer.h +++ b/src/timer.h @@ -7,7 +7,10 @@ extern volatile uint32_t ticks; void timer_init(); void timer_free(); -/* each t is 54.9254 ms */ -void timer_wait(uint8_t t); +/* countdown clock */ +void timer_start(uint8_t secs, uint8_t *updated); +uint8_t timer_value(); +void timer_stop(); +void timer_resume(); #endif /* _TIMER_H */ -- cgit v1.2.3