/*
* Copyright (C) 2021 P. J. McDermott
*
* This file is part of Maze Fight
*
* Maze Fight is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Maze Fight is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Maze Fight. If not, see .
*/
#include
#include
#include
#include
#include
#include
#include
#include "defs.h"
#include "dirs.h"
#include "maze.h"
#include "splash.h"
#include "tk.h"
#include "util.h"
static int
_mf_splash_size(void *user_data, int state)
{
switch (state) {
case 0:
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Size: 15x15");
break;
case 1:
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Size: 20x20");
break;
case 2:
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Size: 30x30");
break;
default:
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Size: unknown");
break;
}
return 0;
}
static int
_mf_splash_fow(void *user_data, int state)
{
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Fog of war: %s",
state ? "enabled" : "disabled");
return 0;
}
static int
_mf_splash_reveal(void *user_data, int state)
{
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Reveal maze: %s",
state ? "enabled" : "disabled");
return 0;
}
static int
_mf_splash_quit(void *user_data)
{
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Quitting");
return 0;
}
static int
_mf_splash_play(void *user_data)
{
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Playing");
return 0;
}
static struct mftk_widget *
_mf_splash_form(SDL_Renderer *renderer, TTF_Font *text_font,
SDL_Color *text_color)
{
SDL_Color butn_color;
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;
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;
chkb_color.b = MF_COLOR_CHKB_B, chkb_color.a = MF_COLOR_CHKB_A;
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;
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, seed, 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,
&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"),
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),
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),
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),
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),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T
),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T
);
return grid;
}
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_window *win = NULL;
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) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't open font: %s",
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;
text_color.r = MF_COLOR_FORE_R, text_color.g = MF_COLOR_FORE_G;
text_color.b = MF_COLOR_FORE_B, text_color.a = MF_COLOR_FORE_A;
win = mftk_window_new(mftk_box_new(MF_WINDOW_W, MF_WINDOW_H, 0, 0,
MF_SPLASH_FORM_P, &form_color,
mftk_grid_new(2, 1, MF_SPLASH_TITLE_M, 0,
mftk_label_new(title_font, "Maze Fight",
&text_color, renderer),
MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T,
_mf_splash_form(renderer, text_font,
&text_color),
MFTK_GRID_HALIGN_C|MFTK_GRID_VALIGN_T
)
)
);
TTF_CloseFont(title_font);
title_font = NULL;
free(font_path);
font_path = NULL;
SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_BLEND);
while (SDL_WaitEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
goto quit;
default:
break;
}
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);
}
quit:
mftk_window_destroy(&win);
TTF_CloseFont(text_font);
text_font = NULL;
mf_maze_destroy(&maze);
return 0;
err:
if (font_path != NULL) {
free(font_path);
}
if (title_font != NULL) {
TTF_CloseFont(title_font);
}
if (text_font != NULL) {
TTF_CloseFont(text_font);
}
mftk_window_destroy(&win);
mf_maze_destroy(&maze);
return -1;
}