From a42ea8a19ab40a02c0c46b2d60ced87c5da1f7eb Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Thu, 05 Aug 2021 20:23:50 -0400 Subject: tk: Add text widget (without event handling) --- (limited to 'src/splash.c') diff --git a/src/splash.c b/src/splash.c index 07036b8..b36ec33 100644 --- a/src/splash.c +++ b/src/splash.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include "defs.h" #include "dirs.h" @@ -83,23 +84,15 @@ _mf_splash_play(void *user_data) } static struct mftk_widget * -_mf_splash_form(SDL_Renderer *renderer, const char *font_path, +_mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font, SDL_Color *text_color) { - TTF_Font *text_font = NULL; SDL_Color butn_color; SDL_Color chkb_color; SDL_Color chkm_color; + double rand_max_len; struct mftk_widget *grid; - text_font = TTF_OpenFont(font_path, MF_SPLASH_TEXT_FONT_S); - if (text_font == NULL) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Couldn't open font: %s", - TTF_GetError()); - return NULL; - } - butn_color.r = MF_COLOR_BUTN_R, butn_color.g = MF_COLOR_BUTN_G; butn_color.b = MF_COLOR_BUTN_B, butn_color.a = MF_COLOR_BUTN_A; chkb_color.r = MF_COLOR_CHKB_R, chkb_color.g = MF_COLOR_CHKB_G; @@ -107,7 +100,15 @@ _mf_splash_form(SDL_Renderer *renderer, const char *font_path, chkm_color.r = MF_COLOR_CHKM_R, chkm_color.g = MF_COLOR_CHKM_G; chkm_color.b = MF_COLOR_CHKM_B, chkm_color.a = MF_COLOR_CHKM_A; - grid = mftk_grid_new(4, 2, MF_SPLASH_ROW_M, MF_SPLASH_COL_M, + /* Assigned to a variable to satisfy -Wbad-function-cast */ + rand_max_len = ceil(log10(RAND_MAX)); + + grid = mftk_grid_new(5, 2, MF_SPLASH_ROW_M, MF_SPLASH_COL_M, + mftk_label_new(text_font, "Seed", text_color, renderer), + MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T, + mftk_text_new('0', '9', rand_max_len, "0", text_font, + text_color), + MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T, mftk_label_new(text_font, "Size", text_color, renderer), MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T, mftk_radio_new(MF_SPLASH_CHK_BTN_W, MF_SPLASH_CHK_BTN_P, @@ -145,9 +146,6 @@ _mf_splash_form(SDL_Renderer *renderer, const char *font_path, MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T ); - TTF_CloseFont(text_font); - text_font = NULL; - return grid; } @@ -156,6 +154,7 @@ mf_splash(SDL_Renderer *renderer) { char *font_path = NULL; TTF_Font *title_font = NULL; + TTF_Font *text_font = NULL; SDL_Color form_color; SDL_Color text_color; struct mftk_widget *box; @@ -171,6 +170,13 @@ mf_splash(SDL_Renderer *renderer) TTF_GetError()); goto err; } + text_font = TTF_OpenFont(font_path, MF_SPLASH_TEXT_FONT_S); + if (text_font == NULL) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Couldn't open font: %s", + TTF_GetError()); + goto err; + } form_color.r = MF_COLOR_FORM_R, form_color.g = MF_COLOR_FORM_G; form_color.b = MF_COLOR_FORM_B, form_color.a = MF_COLOR_FORM_A; @@ -182,12 +188,11 @@ mf_splash(SDL_Renderer *renderer) mftk_label_new(title_font, "Maze Fight", &text_color, renderer), MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T, - _mf_splash_form(renderer, font_path, + _mf_splash_form(renderer, text_font, &text_color), MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T ) ); - /* TODO: Widgets */ mftk_widget_layout(box); TTF_CloseFont(title_font); @@ -229,6 +234,8 @@ mf_splash(SDL_Renderer *renderer) quit: mftk_widget_destroy(&box); + TTF_CloseFont(text_font); + text_font = NULL; mf_maze_destroy(&maze); return 0; @@ -240,6 +247,9 @@ mf_splash(SDL_Renderer *renderer) if (title_font != NULL) { TTF_CloseFont(title_font); } + if (text_font != NULL) { + TTF_CloseFont(text_font); + } mftk_widget_destroy(&box); mf_maze_destroy(&maze); return -1; -- cgit v0.9.1