summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-07 00:20:07 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-07 00:20:07 (EDT)
commitb085e7cee14f2b9c0a0c7c85c34b5d6ae6faf515 (patch)
tree38d0dcc9d87147d604688d5f68f345a5c30149d7
parent9c6c2d41417246a711381352185b9b4340ce6857 (diff)
downloadmazefight-b085e7cee14f2b9c0a0c7c85c34b5d6ae6faf515.zip
mazefight-b085e7cee14f2b9c0a0c7c85c34b5d6ae6faf515.tar.gz
mazefight-b085e7cee14f2b9c0a0c7c85c34b5d6ae6faf515.tar.bz2
splash: Make callbacks useful
-rw-r--r--src/splash.c86
1 files changed, 57 insertions, 29 deletions
diff --git a/src/splash.c b/src/splash.c
index 5c9adb7..5a85330 100644
--- a/src/splash.c
+++ b/src/splash.c
@@ -31,10 +31,20 @@
#include "tk.h"
#include "util.h"
+struct _mf_splash {
+ long seed;
+ int size;
+ int fow;
+ int reveal;
+ int quit;
+};
+
static int
_mf_splash_seed(void *user_data, const char *seed)
{
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Seed: %ld", atol(seed));
+ struct _mf_splash *splash = (struct _mf_splash *) user_data;
+
+ splash->seed = atol(seed);
return 0;
}
@@ -42,22 +52,19 @@ _mf_splash_seed(void *user_data, const char *seed)
static int
_mf_splash_size(void *user_data, int state)
{
+ struct _mf_splash *splash = (struct _mf_splash *) user_data;
+
switch (state) {
case 0:
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
- "Size: 15x15");
+ splash->size = 15;
break;
case 1:
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
- "Size: 20x20");
+ splash->size = 20;
break;
case 2:
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
- "Size: 30x30");
+ splash->size = 30;
break;
default:
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
- "Size: unknown");
break;
}
return 0;
@@ -66,42 +73,52 @@ _mf_splash_size(void *user_data, int state)
static int
_mf_splash_fow(void *user_data, int state)
{
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Fog of war: %s",
- state ? "enabled" : "disabled");
+ struct _mf_splash *splash = (struct _mf_splash *) user_data;
+
+ splash->fow = state;
+
return 0;
}
static int
_mf_splash_reveal(void *user_data, int state)
{
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Reveal maze: %s",
- state ? "enabled" : "disabled");
+ struct _mf_splash *splash = (struct _mf_splash *) user_data;
+
+ splash->reveal = state;
+
return 0;
}
static int
_mf_splash_quit(void *user_data)
{
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Quitting");
+ struct _mf_splash *splash = (struct _mf_splash *) user_data;
+
+ splash->quit = SDL_TRUE;
+
return 0;
}
static int
_mf_splash_play(void *user_data)
{
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Playing");
+ struct _mf_splash *splash = (struct _mf_splash *) user_data;
+
+ /* TODO */
+
return 0;
}
static struct mftk_widget *
_mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font,
- SDL_Color *text_color)
+ SDL_Color *text_color, struct _mf_splash *splash)
{
SDL_Color butn_color;
SDL_Color chkb_color;
SDL_Color chkm_color;
double rand_max_len;
- char *seed;
+ char *seed_buf;
struct mftk_widget *grid;
butn_color.r = MF_COLOR_BUTN_R, butn_color.g = MF_COLOR_BUTN_G;
@@ -112,8 +129,8 @@ _mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font,
chkm_color.b = MF_COLOR_CHKM_B, chkm_color.a = MF_COLOR_CHKM_A;
rand_max_len = ceil(log10(RAND_MAX));
- seed = calloc(rand_max_len + 1, sizeof(*seed));
- if (seed == NULL || sprintf(seed, "%d", rand()) < 0) {
+ seed_buf = calloc(rand_max_len + 1, sizeof(*seed_buf));
+ if (seed_buf == NULL || sprintf(seed_buf, "%ld", splash->seed) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't allocate string: %s",
strerror(errno));
@@ -123,41 +140,42 @@ _mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font,
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, seed, text_font,
- text_color, _mf_splash_seed, NULL),
+ mftk_text_new('0', '9', rand_max_len, seed_buf,
+ text_font, text_color, _mf_splash_seed, splash),
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,
&chkb_color, &chkm_color, MF_SPLASH_CHK_LBL_P,
MF_SPLASH_CHK_ITM_P, text_font, text_color,
- _mf_splash_size, NULL, renderer, 0, 3, "15x15",
- "20x20", "30x30"),
+ _mf_splash_size, splash, renderer, 0, 3,
+ "15x15", "20x20", "30x30"),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T,
mftk_label_new(text_font, "Fog of war", text_color,
renderer),
MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T,
mftk_check_new(MF_SPLASH_CHK_BTN_W, MF_SPLASH_CHK_BTN_P,
&chkb_color, &chkm_color, SDL_TRUE, 0, NULL,
- NULL, NULL, _mf_splash_fow, NULL, renderer),
+ NULL, NULL, _mf_splash_fow, splash, renderer),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T,
mftk_label_new(text_font, "Reveal maze", text_color,
renderer),
MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T,
mftk_check_new(MF_SPLASH_CHK_BTN_W, MF_SPLASH_CHK_BTN_P,
&chkb_color, &chkm_color, SDL_FALSE, 0, NULL,
- NULL, NULL, _mf_splash_reveal, NULL, renderer),
+ NULL, NULL, _mf_splash_reveal, splash,
+ renderer),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T,
mftk_blank_new(),
MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T,
mftk_grid_new(1, 2, 0, MF_SPLASH_BTN_M,
mftk_button_new(text_font, "Quit", text_color,
&butn_color, MF_SPLASH_BTN_P,
- &_mf_splash_quit, NULL, renderer),
+ &_mf_splash_quit, splash, renderer),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T,
mftk_button_new(text_font, "Play", text_color,
&butn_color, MF_SPLASH_BTN_P,
- &_mf_splash_play, NULL, renderer),
+ &_mf_splash_play, splash, renderer),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T
),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T
@@ -171,6 +189,7 @@ mf_splash(SDL_Renderer *renderer)
{
struct mf_maze *maze = NULL;
SDL_Color maze_color;
+ struct _mf_splash splash;
char *font_path = NULL;
TTF_Font *title_font = NULL;
TTF_Font *text_font = NULL;
@@ -191,6 +210,12 @@ mf_splash(SDL_Renderer *renderer)
maze_color.b = MF_COLOR_MAZE_B;
maze_color.a = MF_COLOR_MAZE_A;
+ splash.seed = rand();
+ splash.size = 15;
+ splash.fow = SDL_TRUE;
+ splash.reveal = SDL_FALSE;
+ splash.quit = SDL_FALSE;
+
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) {
@@ -219,7 +244,7 @@ mf_splash(SDL_Renderer *renderer)
&text_color, renderer),
MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T,
_mf_splash_form(renderer, text_font,
- &text_color),
+ &text_color, &splash),
MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T
)
)
@@ -239,13 +264,16 @@ mf_splash(SDL_Renderer *renderer)
default:
break;
}
+ mftk_window_event(win, &event);
+ if (splash.quit) {
+ goto quit;
+ }
SDL_SetRenderDrawColor(renderer,
MF_COLOR_BACK_R, MF_COLOR_BACK_G,
MF_COLOR_BACK_B, MF_COLOR_BACK_A);
SDL_RenderClear(renderer);
mf_maze_render(maze, renderer, &maze_color,
MF_SPLASH_MAZE_CELL_W);
- mftk_window_event(win, &event);
mftk_window_render(win, renderer);
SDL_RenderPresent(renderer);
}