From a100ac901dd85a2f7152e5c8376f6ca1cdb0d56c Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 18 Mar 2021 16:27:03 -0400 Subject: main-menu: Factor title text rendering into new fn --- (limited to 'src') diff --git a/src/main-menu.c b/src/main-menu.c index 01af44e..6dae575 100644 --- a/src/main-menu.c +++ b/src/main-menu.c @@ -26,6 +26,49 @@ #include "output.h" #include "util.h" +static void +_db_main_menu_title(const char *fonts_dir, SDL_Renderer *renderer) +{ + 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; + + font = TTF_OpenFont( + db_strcat(fonts_dir, "/UbuntuTitling-Bold.ttf"), 48); + if (font == NULL) { + db_err("Failed to open font (%s)", TTF_GetError()); + return; + } + + surface = TTF_RenderText_Blended(font, "Dodge Balls", color); + if (surface == NULL) { + db_err("Failed to create surface (%s)", TTF_GetError()); + return; + } + + texture = SDL_CreateTextureFromSurface(renderer, surface); + if (texture == NULL) { + db_err("Failed to create texture (%s)", SDL_GetError()); + return; + } + + rect.x = 16; + rect.y = 16; + rect.w = surface->w; + rect.h = surface->h; + SDL_RenderCopy(renderer, texture, NULL, &rect); + + SDL_DestroyTexture(texture); + SDL_FreeSurface(surface); +} + void db_main_menu(void) { @@ -34,11 +77,6 @@ db_main_menu(void) SDL_Window *window; SDL_Renderer *renderer; SDL_Texture *texture; - SDL_Color text_color; - TTF_Font *font; - SDL_Surface *text_surface; - SDL_Texture *text_texture; - SDL_Rect dest_rect; struct db_game **games; int n; int i; @@ -55,32 +93,7 @@ db_main_menu(void) SDL_RenderClear(renderer); /* Render title text */ - text_color.r = 0x00; - text_color.g = 0x00; - text_color.b = 0xFF; - text_color.a = 0xFF; - font = TTF_OpenFont( - db_strcat(fonts_dir, "/UbuntuTitling-Bold.ttf"), 48); - if (font == NULL) { - db_err("Failed to open font (%s)", TTF_GetError()); - return; - } - text_surface = TTF_RenderText_Blended(font, "Dodge Balls", - text_color); - if (text_surface == NULL) { - db_err("Failed to create surface (%s)", TTF_GetError()); - return; - } - text_texture = SDL_CreateTextureFromSurface(renderer, text_surface); - if (text_texture == NULL) { - db_err("Failed to create texture (%s)", SDL_GetError()); - return; - } - dest_rect.x = 16; - dest_rect.y = 16; - dest_rect.w = text_surface->w; - dest_rect.h = text_surface->h; - SDL_RenderCopy(renderer, text_texture, NULL, &dest_rect); + _db_main_menu_title(fonts_dir, renderer); /* Find games */ n = db_games_find(games_dir, &games); @@ -96,7 +109,4 @@ db_main_menu(void) SDL_RenderPresent(renderer); SDL_Delay(1000); - - SDL_DestroyTexture(text_texture); - SDL_FreeSurface(text_surface); } -- cgit v0.9.1