diff options
author | Juan J. Martinez <jjm@usebox.net> | 2023-06-15 22:58:30 +0100 |
---|---|---|
committer | Juan J. Martinez <jjm@usebox.net> | 2023-06-15 22:58:30 +0100 |
commit | a1de326ac5d89cbf9555c9a0a9a662af35c6dcf7 (patch) | |
tree | ffe56e86be4acefcee8b89753cf31f7a8c1db20d | |
parent | 07e829e591394fa182e75ecab86051f1fb850ce7 (diff) | |
download | gold-mine-run-a1de326ac5d89cbf9555c9a0a9a662af35c6dcf7.tar.gz gold-mine-run-a1de326ac5d89cbf9555c9a0a9a662af35c6dcf7.zip |
Implement a countdown clock in the timer
Updated the HUD to show the time.
-rw-r--r-- | src/game.c | 13 | ||||
-rw-r--r-- | src/game.h | 5 | ||||
-rw-r--r-- | src/keyb.h | 2 | ||||
-rw-r--r-- | src/main.c | 10 | ||||
-rw-r--r-- | src/timer.c | 45 | ||||
-rw-r--r-- | src/timer.h | 7 |
6 files changed, 70 insertions, 12 deletions
@@ -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(); @@ -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 @@ -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 @@ -4,6 +4,7 @@ #include <stdio.h> #include <crt0.h> +#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 <dpmi.h> 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 */ |