From 342ade3296295847e56139e8d79ea51a4e12c715 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 18 Mar 2021 18:40:47 -0400 Subject: main-menu: Store button information in structs --- (limited to 'src/main-menu.c') diff --git a/src/main-menu.c b/src/main-menu.c index f6398e8..e5b7726 100644 --- a/src/main-menu.c +++ b/src/main-menu.c @@ -26,6 +26,16 @@ #include "output.h" #include "util.h" +struct _db_main_menu_button { + SDL_Texture *texture_text; + SDL_Texture *texture_over; + SDL_Rect rect; + struct _db_main_menu_button *u; + struct _db_main_menu_button *d; + struct _db_main_menu_button *l; + struct _db_main_menu_button *r; +}; + static SDL_Texture * _db_main_menu_text(TTF_Font *font, const char *text, SDL_Color *color, int width, SDL_Renderer *renderer, SDL_Rect *rect) @@ -56,24 +66,19 @@ _db_main_menu_text(TTF_Font *font, const char *text, SDL_Color *color, void db_main_menu(void) { - const char *games_dir; - char *font_path; - SDL_Renderer *renderer; - SDL_Color text_color; - SDL_Color over_color; - SDL_Rect dest_rect; - TTF_Font *font; - SDL_Texture *texture_title; - SDL_Texture *texture_help_text; - SDL_Texture *texture_help_over; - SDL_Texture *texture_quit_text; - SDL_Texture *texture_quit_over; - struct db_game **games; - int n; - char *name_desc; - SDL_Texture **texture_games_text; - SDL_Texture **texture_games_over; - int i; + const char *games_dir; + char *font_path; + SDL_Renderer *renderer; + SDL_Color text_color; + SDL_Color over_color; + SDL_Rect dest_rect; + TTF_Font *font; + SDL_Texture *texture_title; + struct db_game **games; + int n; + struct _db_main_menu_button **buttons; + char *name_desc; + int i; games_dir = db_get_games_dir(); font_path = db_strcat(db_get_fonts_dir(), "/UbuntuTitling-Bold.ttf"); @@ -94,12 +99,6 @@ db_main_menu(void) over_color.b = 0xFF; over_color.a = 0xFF; - n = 0; - texture_help_text = NULL; - texture_help_over = NULL; - texture_quit_text = NULL; - texture_quit_over = NULL; - /* Render title text */ font = TTF_OpenFont(font_path, 48); if (font == NULL) { @@ -125,46 +124,64 @@ db_main_menu(void) return; } - texture_help_text = _db_main_menu_text(font, "How to Play", &text_color, - 0, renderer, &dest_rect); - if (texture_help_text == NULL) { + /* Find games */ + n = db_games_find(games_dir, &games); + buttons = calloc(n + 2, sizeof(*buttons)); + if (buttons == NULL) { + db_err("Failed to allocate memory"); + goto err; + } + + /* Render help button */ + buttons[0] = malloc(sizeof(**buttons)); + if (buttons[0] == NULL) { + db_err("Failed to allocate memory"); goto err; } - texture_help_over = _db_main_menu_text(font, "How to Play", &over_color, - 0, renderer, &dest_rect); - if (texture_help_over == NULL) { + buttons[0]->texture_text = _db_main_menu_text(font, "How to Play", + &text_color, 0, renderer, &buttons[0]->rect); + if (buttons[0]->texture_text == NULL) { goto err; } - dest_rect.x = 640 - 16 - dest_rect.w; - dest_rect.y = 16; - SDL_RenderCopy(renderer, texture_help_text, NULL, &dest_rect); - - texture_quit_text = _db_main_menu_text(font, "Quit", &text_color, - 0, renderer, &dest_rect); - if (texture_quit_text == NULL) { + buttons[0]->texture_over = _db_main_menu_text(font, "How to Play", + &over_color, 0, renderer, &buttons[0]->rect); + if (buttons[0]->texture_over == NULL) { + goto err; + } + buttons[0]->rect.x = 640 - 16 - buttons[0]->rect.w; + buttons[0]->rect.y = 16; + SDL_RenderCopy(renderer, buttons[0]->texture_text, NULL, + &buttons[0]->rect); + + /* Render quit button */ + buttons[1] = malloc(sizeof(**buttons)); + if (buttons[1] == NULL) { + db_err("Failed to allocate memory"); + goto err; + } + buttons[1]->texture_text = _db_main_menu_text(font, "Quit", + &text_color, 0, renderer, &buttons[1]->rect); + if (buttons[1]->texture_text == NULL) { goto err; } - texture_quit_over = _db_main_menu_text(font, "Quit", &over_color, - 0, renderer, &dest_rect); - if (texture_quit_over == NULL) { + buttons[1]->texture_over = _db_main_menu_text(font, "Quit", + &over_color, 0, renderer, &buttons[1]->rect); + if (buttons[1]->texture_over == NULL) { goto err; } - dest_rect.x = 640 - 16 - dest_rect.w; - dest_rect.y = 48; - SDL_RenderCopy(renderer, texture_quit_text, NULL, &dest_rect); + buttons[1]->rect.x = 640 - 16 - buttons[1]->rect.w; + buttons[1]->rect.y = 48; + SDL_RenderCopy(renderer, buttons[1]->texture_text, NULL, + &buttons[1]->rect); - /* Find games */ - n = db_games_find(games_dir, &games); + /* Render game buttons */ if (n > 0) { - texture_games_text = calloc(n, sizeof(*texture_games_text)); - if (texture_games_text == NULL) { - goto err; - } - texture_games_over = calloc(n, sizeof(*texture_games_over)); - if (texture_games_over == NULL) { - goto err; - } for (i = 0; i < n; ++i) { + buttons[i + 2] = malloc(sizeof(**buttons)); + if (buttons[i + 2] == NULL) { + db_err("Failed to allocate memory"); + goto err; + } name_desc = malloc((strlen(db_game_get_name(games[i])) + strlen(db_game_get_desc( games[i])) + 2) @@ -175,22 +192,22 @@ db_main_menu(void) } sprintf(name_desc, "%s\n%s", db_game_get_name(games[i]), db_game_get_desc(games[i])); - texture_games_text[i] = _db_main_menu_text(font, + buttons[i + 2]->texture_text = _db_main_menu_text(font, name_desc, &text_color, 608, renderer, &dest_rect); - if (texture_games_text[i] == NULL) { + if (buttons[i + 2]->texture_text == NULL) { free(name_desc); goto err; } dest_rect.x = 16; - dest_rect.y = 80 + 48 * i; - SDL_RenderCopy(renderer, texture_games_text[i], NULL, + dest_rect.y = 80 + 48 * i + 2; + SDL_RenderCopy(renderer, buttons[i + 2]->texture_text, NULL, &dest_rect); - texture_games_over[i] = _db_main_menu_text(font, + buttons[i + 2]->texture_over = _db_main_menu_text(font, name_desc, &over_color, 608, renderer, &dest_rect); free(name_desc); - if (texture_games_over[i] == NULL) { + if (buttons[i + 2]->texture_over == NULL) { goto err; } } @@ -208,8 +225,4 @@ db_main_menu(void) } free(font_path); TTF_CloseFont(font); - SDL_DestroyTexture(texture_help_text); - SDL_DestroyTexture(texture_help_over); - SDL_DestroyTexture(texture_quit_text); - SDL_DestroyTexture(texture_quit_over); } -- cgit v0.9.1