From 067ac12c8ca4658dbc555d83de533ee7e350a5bd Mon Sep 17 00:00:00 2001 From: KikooDX Date: Mon, 20 Dec 2021 18:00:17 +0100 Subject: [PATCH] skip level & ask before exiting --- inc/input.h | 1 + inc/util.h | 1 + src/input.c | 4 ++-- src/main.c | 10 ++++++++-- src/util.c | 19 +++++++++++++++++++ 5 files changed, 31 insertions(+), 4 deletions(-) diff --git a/inc/input.h b/inc/input.h index e4676e7..ed0c5cf 100644 --- a/inc/input.h +++ b/inc/input.h @@ -8,6 +8,7 @@ enum Key { K_JUMP, K_POLARITY, K_EXIT, + K_SKIP, K_EDITOR, K_SCROLL_UP, K_SCROLL_DOWN, diff --git a/inc/util.h b/inc/util.h index 12c91c8..1172806 100644 --- a/inc/util.h +++ b/inc/util.h @@ -7,3 +7,4 @@ float maxf(float, float); int min(int, int); int max(int, int); void dputs_outline(int x, int y, int halign, int valign, const char *); +int ask_confirm(char *); diff --git a/src/input.c b/src/input.c index 1846a8c..e6b4718 100644 --- a/src/input.c +++ b/src/input.c @@ -3,8 +3,8 @@ static struct Input input; static const int default_map[K_COUNT] = { - KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_SHIFT, - KEY_ALPHA, KEY_EXIT, KEY_F3, KEY_F2, KEY_F1}; + KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, KEY_SHIFT, KEY_ALPHA, + KEY_EXIT, KEY_TAN, KEY_F3, KEY_F2, KEY_F1}; void input_init(void) diff --git a/src/main.c b/src/main.c index 9e0c85d..946c453 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "level.h" #include "player.h" #include "time.h" +#include "util.h" #include #include #include @@ -27,7 +28,7 @@ main(void) init(); level_load(0); - do { + for (;;) { int i; draw(); for (i = 0; i < frameskip; i++) { @@ -39,8 +40,13 @@ main(void) has_ticked = 0; } update(); + if (input_pressed(K_EXIT) && ask_confirm("exit game")) + goto game_loop_end; + if (input_pressed(K_SKIP) && ask_confirm("skip level")) + level_next(); } - } while (input_up(K_EXIT)); + }; +game_loop_end: deinit(); return 0; diff --git a/src/util.c b/src/util.c index 1540320..cf3af50 100644 --- a/src/util.c +++ b/src/util.c @@ -1,5 +1,6 @@ #include "util.h" #include +#include int sign(int x) @@ -50,3 +51,21 @@ dputs_outline(int x, int y, int halign, int valign, const char *text) } dprint_opt(x, y, C_BLACK, C_NONE, halign, valign, "%s", text); } + +int +ask_confirm(char *prompt) +{ + extern volatile int has_ticked; + dclear(C_BLACK); + dprint_opt(DWIDTH / 2, DHEIGHT / 2, C_WHITE, C_NONE, DTEXT_CENTER, + DTEXT_MIDDLE, "%s? EXE confirm, any other key cancel", + prompt); + dupdate(); + for (;;) { + const key_event_t e = getkey(); + if (e.type == KEYEV_DOWN) { + has_ticked = 0; + return e.key == KEY_EXE; + } + } +}