From 778519f359332f7338d8ae7a6b20b394e8a092f9 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 18 Mar 2021 16:46:34 -0400 Subject: main-menu: More title text refactoring --- (limited to 'src/main-menu.c') diff --git a/src/main-menu.c b/src/main-menu.c index 58f0a5d..07a1302 100644 --- a/src/main-menu.c +++ b/src/main-menu.c @@ -26,46 +26,39 @@ #include "output.h" #include "util.h" -static void -_db_main_menu_title(const char *font_path, SDL_Renderer *renderer) +static SDL_Texture * +_db_main_menu_title(const char *font_path, SDL_Color *color, + SDL_Renderer *renderer, SDL_Rect *rect) { - SDL_Color color; - TTF_Font *font; - SDL_Surface *surface; - SDL_Texture *texture; - SDL_Rect rect; - - color.r = 0x00; - color.g = 0x00; - color.b = 0xFF; - color.a = 0xFF; + TTF_Font *font; + SDL_Surface *surface; + SDL_Texture *texture; font = TTF_OpenFont(font_path, 48); if (font == NULL) { db_err("Failed to open font (%s)", TTF_GetError()); - return; + return NULL; } - surface = TTF_RenderText_Blended(font, "Dodge Balls", color); + surface = TTF_RenderText_Blended(font, "Dodge Balls", *color); if (surface == NULL) { db_err("Failed to create surface (%s)", TTF_GetError()); - return; + return NULL; } texture = SDL_CreateTextureFromSurface(renderer, surface); if (texture == NULL) { db_err("Failed to create texture (%s)", SDL_GetError()); - return; + return NULL; } - rect.x = 16; - rect.y = 16; - rect.w = surface->w; - rect.h = surface->h; - SDL_RenderCopy(renderer, texture, NULL, &rect); + rect->w = surface->w; + rect->h = surface->h; - SDL_DestroyTexture(texture); SDL_FreeSurface(surface); + TTF_CloseFont(font); + + return texture; } void @@ -74,6 +67,9 @@ db_main_menu(void) const char *games_dir; char *font_path; SDL_Renderer *renderer; + SDL_Color text_color; + SDL_Rect dest_rect; + SDL_Texture *texture_title; struct db_game **games; int n; int i; @@ -87,8 +83,21 @@ db_main_menu(void) SDL_SetRenderDrawColor(renderer, 0x7F, 0x7F, 0x7F, 0xFF); SDL_RenderClear(renderer); + text_color.r = 0x00; + text_color.g = 0x00; + text_color.b = 0xFF; + text_color.a = 0xFF; + /* Render title text */ - _db_main_menu_title(font_path, renderer); + texture_title = _db_main_menu_title(font_path, &text_color, renderer, + &dest_rect); + if (texture_title == NULL) { + goto out; + } + dest_rect.x = 16; + dest_rect.y = 16; + SDL_RenderCopy(renderer, texture_title, NULL, &dest_rect); + SDL_DestroyTexture(texture_title); /* Find games */ n = db_games_find(games_dir, &games); @@ -105,5 +114,6 @@ db_main_menu(void) SDL_RenderPresent(renderer); SDL_Delay(1000); + out: free(font_path); } -- cgit v0.9.1