summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-03-18 19:46:24 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-03-18 19:46:24 (EDT)
commitd0bfb0c2ba3b11ff8bd28f894dd32660d5837b3c (patch)
treeca5607f80cd093955a086b129955a64ace500899
parentb3c3497499f0346ccd952e8398f2b0bdb3da32ed (diff)
downloaddodge-balls-d0bfb0c2ba3b11ff8bd28f894dd32660d5837b3c.zip
dodge-balls-d0bfb0c2ba3b11ff8bd28f894dd32660d5837b3c.tar.gz
dodge-balls-d0bfb0c2ba3b11ff8bd28f894dd32660d5837b3c.tar.bz2
main-menu: Handle keyboard events
-rw-r--r--src/main-menu.c74
1 files changed, 66 insertions, 8 deletions
diff --git a/src/main-menu.c b/src/main-menu.c
index 613de69..d934ec4 100644
--- a/src/main-menu.c
+++ b/src/main-menu.c
@@ -26,6 +26,8 @@
#include "output.h"
#include "util.h"
+static int _db_main_menu_quit;
+
struct _db_main_menu_button {
SDL_Texture *texture_text;
SDL_Texture *texture_over;
@@ -68,17 +70,18 @@ _db_main_menu_text(TTF_Font *font, const char *text, SDL_Color *color,
}
static void
-_db_main_menu_help(void *user_data __attribute__((__unused__)))
+_db_main_menu_action_help(void *user_data __attribute__((__unused__)))
{
}
static void
-_db_main_menu_quit(void *user_data __attribute__((__unused__)))
+_db_main_menu_action_quit(void *user_data __attribute__((__unused__)))
{
+ _db_main_menu_quit = 1;
}
static void
-_db_main_menu_game(void *user_data)
+_db_main_menu_action_game(void *user_data)
{
struct db_game *game;
@@ -104,6 +107,7 @@ db_main_menu(void)
char *name_desc;
int i;
struct _db_main_menu_button *active;
+ SDL_Event event;
games_dir = db_get_games_dir();
font_path = db_strcat(db_get_fonts_dir(), "/UbuntuTitling-Bold.ttf");
@@ -176,7 +180,7 @@ db_main_menu(void)
}
buttons[0]->rect.x = 640 - 16 - buttons[0]->rect.w;
buttons[0]->rect.y = 16;
- buttons[0]->action = &_db_main_menu_help;
+ buttons[0]->action = &_db_main_menu_action_help;
buttons[0]->user_data = NULL;
/* Render quit button */
@@ -197,7 +201,7 @@ db_main_menu(void)
}
buttons[1]->rect.x = 640 - 16 - buttons[1]->rect.w;
buttons[1]->rect.y = 48;
- buttons[1]->action = &_db_main_menu_quit;
+ buttons[1]->action = &_db_main_menu_action_quit;
buttons[1]->user_data = NULL;
/* Render game buttons */
@@ -241,7 +245,7 @@ db_main_menu(void)
buttons[i + 2]->r = buttons[0];
buttons[i + 2]->p = buttons[i + 1];
buttons[i + 1]->n = buttons[i + 2];
- buttons[i + 2]->action = &_db_main_menu_game;
+ buttons[i + 2]->action = &_db_main_menu_action_game;
buttons[i + 2]->user_data = games[i];
}
if (n > 0) {
@@ -277,7 +281,59 @@ db_main_menu(void)
active = buttons[1];
}
- while (1) {
+ _db_main_menu_quit = 0;
+ while (SDL_WaitEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ _db_main_menu_quit = 1;
+ break;
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym) {
+ case SDLK_UP:
+ if (active->u != NULL) {
+ active = active->u;
+ }
+ break;
+ case SDLK_DOWN:
+ if (active->d != NULL) {
+ active = active->d;
+ }
+ break;
+ case SDLK_LEFT:
+ if (active->l != NULL) {
+ active = active->l;
+ }
+ break;
+ case SDLK_RIGHT:
+ if (active->r != NULL) {
+ active = active->r;
+ }
+ break;
+ case SDLK_TAB:
+ if (event.key.keysym.mod &
+ KMOD_SHIFT) {
+ if (active->p != NULL) {
+ active =
+ active->p;
+ }
+ } else {
+ if (active->n != NULL) {
+ active =
+ active->n;
+ }
+ }
+ break;
+ case SDLK_RETURN:
+ active->action(
+ active->user_data);
+ break;
+ default:
+ break;
+ }
+ break;
+ default:
+ break;
+ }
for (i = 0; i < n + 2; ++i) {
if (buttons[i] == active) {
SDL_RenderCopy(renderer,
@@ -290,7 +346,9 @@ db_main_menu(void)
}
}
SDL_RenderPresent(renderer);
- SDL_Delay(1000); break;
+ if (_db_main_menu_quit == 1) {
+ break;
+ }
}
err: