diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-03-19 10:59:04 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-03-19 10:59:04 (EDT) |
commit | 2b3d6f3c16342a25e852aa86c0f5ab3bc214a4ca (patch) | |
tree | c690e2159ca91f416e3af0dc634fe9ae967f5f20 /src | |
parent | 68cbaa12b3308716b1cb6dac82b138da7d3cbe9c (diff) | |
download | dodge-balls-2b3d6f3c16342a25e852aa86c0f5ab3bc214a4ca.zip dodge-balls-2b3d6f3c16342a25e852aa86c0f5ab3bc214a4ca.tar.gz dodge-balls-2b3d6f3c16342a25e852aa86c0f5ab3bc214a4ca.tar.bz2 |
main-menu, help: Handle quit through call stack
Diffstat (limited to 'src')
-rw-r--r-- | src/help.c | 13 | ||||
-rw-r--r-- | src/help.h | 2 | ||||
-rw-r--r-- | src/main-menu.c | 37 | ||||
-rw-r--r-- | src/main-menu.h | 2 |
4 files changed, 37 insertions, 17 deletions
@@ -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; } @@ -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_ */ |