/*
* 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 "menu.h"
#include "tk.h"
#include "util.h"
const int MF_MENU_SIZES_[] = {15, 20, 30};
struct _mf_menu {
long seed;
int size;
int fow;
int reveal;
int quit;
};
static int
_mf_menu_seed(void *user_data, const char *seed)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
menu->seed = atol(seed);
return 0;
}
static int
_mf_menu_size(void *user_data, int state)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
menu->size = MF_MENU_SIZES_[state];
return 0;
}
static int
_mf_menu_fow(void *user_data, int state)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
menu->fow = state;
return 0;
}
static int
_mf_menu_reveal(void *user_data, int state)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
menu->reveal = state;
return 0;
}
static int
_mf_menu_quit(void *user_data)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
menu->quit = SDL_TRUE;
return 0;
}
static int
_mf_menu_play(void *user_data)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
/* TODO */
return 0;
}
static struct mftk_widget *
_mf_menu_form(SDL_Renderer *renderer, TTF_Font *text_font,
SDL_Color *text_color, struct _mf_menu *menu)
{
SDL_Color butn_color;
SDL_Color chkb_color;
SDL_Color chkm_color;
double rand_max_len;
char *seed_buf;
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_buf = calloc(rand_max_len + 1, sizeof(*seed_buf));
if (seed_buf == NULL || sprintf(seed_buf, "%ld", menu->seed) < 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_buf,
text_font, text_color, &_mf_menu_seed, menu),
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_menu_size, menu, 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_menu_fow, menu, 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_menu_reveal, menu, 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_menu_quit, menu, renderer),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T,
mftk_button_new(text_font, "Play", text_color,
&butn_color, MF_SPLASH_BTN_P,
&_mf_menu_play, menu, renderer),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T
),
MFTK_GRID_HALIGN_L|MFTK_GRID_VALIGN_T
);
return grid;
}
int
mf_menu(SDL_Renderer *renderer)
{
struct mf_maze *maze = NULL;
SDL_Color maze_color;
struct _mf_menu menu;
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;
menu.seed = rand();
menu.size = 15;
menu.fow = SDL_TRUE;
menu.reveal = SDL_FALSE;
menu.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) {
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_menu_form(renderer, text_font,
&text_color, &menu),
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;
}
mftk_window_event(win, &event);
if (menu.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_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;
}