summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-05 23:35:16 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-06 00:03:24 (EDT)
commitaa8ac0850cb363778577c27b9afc5c2223d7f770 (patch)
treec789b652f01e56217479d5ed61a98879827d2523
parenta42ea8a19ab40a02c0c46b2d60ced87c5da1f7eb (diff)
downloadmazefight-aa8ac0850cb363778577c27b9afc5c2223d7f770.zip
mazefight-aa8ac0850cb363778577c27b9afc5c2223d7f770.tar.gz
mazefight-aa8ac0850cb363778577c27b9afc5c2223d7f770.tar.bz2
splash: Generate random value for seed text input
-rw-r--r--src/splash.c41
1 files changed, 25 insertions, 16 deletions
diff --git a/src/splash.c b/src/splash.c
index b36ec33..08eaeed 100644
--- a/src/splash.c
+++ b/src/splash.c
@@ -19,7 +19,9 @@
#include <SDL.h>
#include <SDL_ttf.h>
+#include <errno.h>
#include <stdlib.h>
+#include <string.h>
#include <math.h>
#include <time.h>
#include "defs.h"
@@ -91,6 +93,7 @@ _mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font,
SDL_Color chkb_color;
SDL_Color chkm_color;
double rand_max_len;
+ char *seed;
struct mftk_widget *grid;
butn_color.r = MF_COLOR_BUTN_R, butn_color.g = MF_COLOR_BUTN_G;
@@ -100,13 +103,19 @@ _mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font,
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;
- /* Assigned to a variable to satisfy -Wbad-function-cast */
rand_max_len = ceil(log10(RAND_MAX));
+ seed = calloc(rand_max_len + 1, sizeof(*seed));
+ if (seed == NULL || sprintf(seed, "%d", rand()) < 0) {
+ SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
+ "Couldn't allocate string: %s",
+ strerror(errno));
+ return NULL;
+ }
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,
+ mftk_text_new('0', '9', rand_max_len, seed, text_font,
text_color),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T,
mftk_label_new(text_font, "Size", text_color, renderer),
@@ -152,16 +161,28 @@ _mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font,
int
mf_splash(SDL_Renderer *renderer)
{
+ struct mf_maze *maze = NULL;
+ SDL_Color maze_color;
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;
- struct mf_maze *maze = NULL;
- SDL_Color maze_color;
SDL_Event event;
+ /* Create maze */
+ maze = mf_maze_new(time(NULL),
+ MF_WINDOW_W / MF_SPLASH_MAZE_CELL_W,
+ MF_WINDOW_H / MF_SPLASH_MAZE_CELL_W);
+ if (maze == NULL) {
+ goto err;
+ }
+ maze_color.r = MF_COLOR_MAZE_R;
+ maze_color.g = MF_COLOR_MAZE_G;
+ maze_color.b = MF_COLOR_MAZE_B;
+ maze_color.a = MF_COLOR_MAZE_A;
+
font_path = mf_strcat(mf_get_fonts_dir(), "/FifteenTwenty-Regular.ttf");
title_font = TTF_OpenFont(font_path, MF_SPLASH_TITLE_FONT_S);
if (title_font == NULL) {
@@ -200,18 +221,6 @@ mf_splash(SDL_Renderer *renderer)
free(font_path);
font_path = NULL;
- /* Create maze */
- maze = mf_maze_new(time(NULL),
- MF_WINDOW_W / MF_SPLASH_MAZE_CELL_W,
- MF_WINDOW_H / MF_SPLASH_MAZE_CELL_W);
- if (maze == NULL) {
- goto err;
- }
- maze_color.r = MF_COLOR_MAZE_R;
- maze_color.g = MF_COLOR_MAZE_G;
- maze_color.b = MF_COLOR_MAZE_B;
- maze_color.a = MF_COLOR_MAZE_A;
-
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
while (SDL_WaitEvent(&event)) {