From 2b3d6f3c16342a25e852aa86c0f5ab3bc214a4ca Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Fri, 19 Mar 2021 10:59:04 -0400 Subject: main-menu, help: Handle quit through call stack --- (limited to 'src') diff --git a/src/help.c b/src/help.c index 71c2f35..0399c36 100644 --- a/src/help.c +++ b/src/help.c @@ -85,7 +85,7 @@ _db_help_triangle(SDL_Renderer *renderer, Uint8 r, Uint8 g, Uint8 b, Uint8 a, SDL_RenderDrawPoints(renderer, points, i); } -void +int db_help(void) { char *font_path; @@ -121,6 +121,8 @@ db_help(void) return; } + TTF_CloseFont(font); + texture = SDL_CreateTextureFromSurface(renderer, surface); if (texture == NULL) { db_err("Failed to create texture (%s)", SDL_GetError()); @@ -141,7 +143,7 @@ db_help(void) while (SDL_WaitEvent(&event)) { switch (event.type) { case SDL_QUIT: - _db_help_quit = 1; + _db_help_quit = 2; break; case SDL_KEYDOWN: switch (event.key.keysym.sym) { @@ -181,7 +183,7 @@ db_help(void) default: break; } - if (_db_help_quit == 1) { + if (_db_help_quit > 0) { break; } SDL_SetRenderDrawColor(renderer, 0x7F, 0x7F, 0x7F, 0xFF); @@ -204,4 +206,9 @@ db_help(void) } SDL_RenderPresent(renderer); } + + SDL_FreeSurface(surface); + SDL_DestroyTexture(texture); + + return _db_help_quit - 1; } diff --git a/src/help.h b/src/help.h index 0c9d279..8f3acc0 100644 --- a/src/help.h +++ b/src/help.h @@ -20,6 +20,6 @@ #ifndef DB_HELP_H_ #define DB_HELP_H_ -void db_help(void); +int db_help(void); #endif /* DB_HELP_H_ */ diff --git a/src/main-menu.c b/src/main-menu.c index 8782c3e..685ea8c 100644 --- a/src/main-menu.c +++ b/src/main-menu.c @@ -40,7 +40,7 @@ struct _db_main_menu_button { struct _db_main_menu_button *r; struct _db_main_menu_button *p; struct _db_main_menu_button *n; - void (*action)(void *); + int (*action)(void *); void *user_data; }; @@ -71,19 +71,21 @@ _db_main_menu_text(TTF_Font *font, const char *text, SDL_Color *color, return texture; } -static void +static int _db_main_menu_action_help(void *user_data __attribute__((__unused__))) { - db_help(); + return db_help(); } -static void +static int _db_main_menu_action_quit(void *user_data __attribute__((__unused__))) { _db_main_menu_quit = 1; + + return 0; } -static void +static int _db_main_menu_action_game(void *user_data) { struct db_game *game; @@ -91,9 +93,11 @@ _db_main_menu_action_game(void *user_data) game = user_data; db_dbg("Loading game \"%s\"", db_game_get_name(game)); + + return 0; } -void +int db_main_menu(void) { const char *games_dir; @@ -132,7 +136,7 @@ db_main_menu(void) if (font == NULL) { db_err("Failed to open font (%s)", TTF_GetError()); free(font_path); - return; + return -1; } texture_title = _db_main_menu_text(font, "Dodge Balls", &text_color, 0, renderer, &dest_rect); @@ -147,7 +151,7 @@ db_main_menu(void) if (font == NULL) { db_err("Failed to open font (%s)", TTF_GetError()); free(font_path); - return; + return -1; } /* Find games */ @@ -324,8 +328,12 @@ db_main_menu(void) break; case SDLK_RETURN: case SDLK_KP_ENTER: - active->action( - active->user_data); + if (active->action(active-> + user_data) + > 0) + { + _db_main_menu_quit = 2; + } break; default: break; @@ -352,7 +360,10 @@ db_main_menu(void) if (db_pt_in_rect(event.button.x, event.button.y, &active->rect)) { - active->action(active->user_data); + if (active->action(active->user_data) > + 0) { + _db_main_menu_quit = 2; + } } break; default: @@ -373,7 +384,7 @@ db_main_menu(void) } } SDL_RenderPresent(renderer); - if (_db_main_menu_quit == 1) { + if (_db_main_menu_quit > 0) { break; } } @@ -382,4 +393,6 @@ db_main_menu(void) free(font_path); TTF_CloseFont(font); SDL_DestroyTexture(texture_title); + + return _db_main_menu_quit - 1; } diff --git a/src/main-menu.h b/src/main-menu.h index cc55ca7..7dffbd9 100644 --- a/src/main-menu.h +++ b/src/main-menu.h @@ -20,6 +20,6 @@ #ifndef DB_MAIN_MENU_H_ #define DB_MAIN_MENU_H_ -void db_main_menu(void); +int db_main_menu(void); #endif /* DB_MAIN_MENU_H_ */ -- cgit v0.9.1