From b76ba4c6eeab4c9e4f77fa1f6f81cdec3f9ff025 Mon Sep 17 00:00:00 2001 From: "Juan J. Martinez" Date: Sun, 6 Aug 2023 20:36:07 +0100 Subject: Ask for confirmation when abandoning the game --- README.md | 2 +- src/game.c | 86 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 82 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 47280dc..95921b9 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ The game can be controlled by keyboard or joystick. | Cursor right | Joystick right | Move Right | | Z | Button 1 | Jump | | P | - | Pause / resume game | -| ESC | - | Exit the game | +| ESC | - | Abandon / exit the game | Check `-h` CLI flag for options. diff --git a/src/game.c b/src/game.c index 2d41821..c2420ca 100644 --- a/src/game.c +++ b/src/game.c @@ -3,6 +3,7 @@ #include #include "keyb.h" +#include "control.h" #include "vga.h" #include "sound.h" #include "text.h" @@ -333,6 +334,79 @@ static void run_stageclear() wait_frames(64); } +static uint8_t run_confirm_quit() +{ + timer_stop(); + + blit_target(TARGET_BUFFER); + blit_erase(0); + + hud = HUD_ALL; + hud_render(); + put_text(108, 90, "ABANDON GAME?", 1); + + wait_vsync(); + blit_copy_all(); + + blit_target(TARGET_SCREEN); + + uint8_t sel = 0; + uint8_t ctl = CTL_NONE; + uint8_t cooldown = 0; + +update_menu: + wait_vsync(); + if (sel) + { + put_text(144, 105, " YES", 4); + put_text(144, 115, ">NO", 5); + } + else + { + put_text(144, 105, ">YES", 5); + put_text(144, 115, " NO", 4); + } + + while (1) + { + if (cooldown) + cooldown--; + else + { + ctl = control_read(); + + if ((ctl & CTL_UP) || (ctl & CTL_DOWN)) + { + cooldown = 10; + sel ^= 1; + goto update_menu; + } + + if (keys[KEY_ENTER]) + break; + } + + wait_vsync(); + } + + /* restore the game screen only when not abandoned */ + if (sel) + { + blit_target(TARGET_BUFFER); + map_render(); + wait_vsync(); + blit_copy_all(); + + blit_target(TARGET_SCREEN); + wait_vsync(); + entities_draw(); + } + + timer_resume(); + + return sel == 0; +} + void run_game() { continuegame = 0; @@ -389,7 +463,7 @@ next_stage: sound_music_pattern(PAT_PLAY); - while (!keys[KEY_ESC]) + while (1) { if (clock_updated) { @@ -400,6 +474,12 @@ next_stage: if (hud) hud_render(); + if (keys[KEY_ESC]) + { + if (run_confirm_quit()) + break; + } + if (keys[KEY_P]) { /* pause / resume */ @@ -505,10 +585,6 @@ next_stage: } sound_music_pattern(PAT_SILENCE); - - /* wait for ESC to be release */ - while (keys[KEY_ESC]) - wait_vsync(); } void add_score(uint8_t v) -- cgit v1.2.3