summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-07 13:58:00 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-07 13:58:00 (EDT)
commitbc8cf40d1317b40042dd78d64c509f0eaccc7142 (patch)
tree763b56fb275b48e5e7681b1006143bc6e0be946c
parentbae0f7abb36e01cbb09721eb86c001bf0bc32202 (diff)
downloadmazefight-bc8cf40d1317b40042dd78d64c509f0eaccc7142.zip
mazefight-bc8cf40d1317b40042dd78d64c509f0eaccc7142.tar.gz
mazefight-bc8cf40d1317b40042dd78d64c509f0eaccc7142.tar.bz2
game: New screen
-rw-r--r--src/game.c148
-rw-r--r--src/game.h28
-rw-r--r--src/local.mk2
-rw-r--r--src/menu.c29
4 files changed, 191 insertions, 16 deletions
diff --git a/src/game.c b/src/game.c
new file mode 100644
index 0000000..7fce1c1
--- /dev/null
+++ b/src/game.c
@@ -0,0 +1,148 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#include <SDL.h>
+#include <SDL_ttf.h>
+#include "defs.h"
+#include "dirs.h"
+#include "game.h"
+#include "maze.h"
+#include "tk.h"
+#include "util.h"
+
+static int
+_mf_game_exit(void *user_data __attribute__((__unused__)))
+{
+ return 1;
+}
+
+static struct mftk_widget *
+_mf_game_form(SDL_Renderer *renderer, TTF_Font *text_font,
+ SDL_Color *text_color)
+{
+ SDL_Color butn_color;
+
+ 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;
+
+ return mftk_button_new(text_font, "Exit", text_color, &butn_color,
+ MF_SPLASH_BTN_P, &_mf_game_exit, NULL, renderer);
+}
+
+int
+mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer)
+{
+ struct mf_maze *maze = NULL;
+ SDL_Color maze_color;
+ char *font_path = 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(seed, size, size);
+ 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");
+ 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;
+ }
+ free(font_path);
+ font_path = NULL;
+
+ 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(MF_WINDOW_H, 0, mftk_box_new(
+ MF_WINDOW_W - MF_WINDOW_H, MF_WINDOW_H,
+ MF_WINDOW_W - MF_WINDOW_H, MF_WINDOW_H,
+ MF_SPLASH_FORM_P, &form_color,
+ _mf_game_form(renderer, text_font,
+ &text_color)));
+
+ while (SDL_WaitEvent(&event)) {
+ switch (event.type) {
+ case SDL_QUIT:
+ goto quit;
+ case SDL_KEYDOWN:
+ switch (event.key.keysym.sym) {
+ case SDLK_ESCAPE:
+ goto exit;
+ default:
+ break;
+ }
+ default:
+ break;
+ }
+ switch (mftk_window_event(win, &event)) {
+ case 0:
+ break;
+ case 1:
+ goto exit;
+ default:
+ goto err;
+ }
+ 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_WINDOW_H / size);
+ mftk_window_render(win, renderer);
+ SDL_RenderPresent(renderer);
+ }
+
+ exit:
+ mftk_window_destroy(&win);
+ TTF_CloseFont(text_font);
+ text_font = NULL;
+ mf_maze_destroy(&maze);
+ return 0;
+
+ quit:
+ mftk_window_destroy(&win);
+ TTF_CloseFont(text_font);
+ text_font = NULL;
+ mf_maze_destroy(&maze);
+ return 1;
+
+ err:
+ if (font_path != NULL) {
+ free(font_path);
+ }
+ if (text_font != NULL) {
+ TTF_CloseFont(text_font);
+ }
+ mftk_window_destroy(&win);
+ mf_maze_destroy(&maze);
+ return -1;
+}
diff --git a/src/game.h b/src/game.h
new file mode 100644
index 0000000..e3f5d64
--- /dev/null
+++ b/src/game.h
@@ -0,0 +1,28 @@
+/*
+ * 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 <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef MF_GAME_H_
+#define MF_GAME_H_
+
+#include <SDL.h>
+
+int
+mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer);
+
+#endif /* MF_GAME_H_ */
diff --git a/src/local.mk b/src/local.mk
index caa664c..dc06380 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -2,6 +2,8 @@ mazefight_SOURCES += \
%reldir%/defs.h \
%reldir%/dirs.c \
%reldir%/dirs.h \
+ %reldir%/game.c \
+ %reldir%/game.h \
%reldir%/main.c \
%reldir%/maze.c \
%reldir%/maze.h \
diff --git a/src/menu.c b/src/menu.c
index 24a4cac..a4f7457 100644
--- a/src/menu.c
+++ b/src/menu.c
@@ -26,6 +26,7 @@
#include <time.h>
#include "defs.h"
#include "dirs.h"
+#include "game.h"
#include "maze.h"
#include "menu.h"
#include "tk.h"
@@ -34,10 +35,11 @@
const int MF_MENU_SIZES_[] = {15, 20, 30};
struct _mf_menu {
- long seed;
- int size;
- int fow;
- int reveal;
+ long seed;
+ int size;
+ int fow;
+ int reveal;
+ SDL_Renderer *renderer;
};
static int
@@ -91,14 +93,8 @@ _mf_menu_play(void *user_data)
{
struct _mf_menu *menu = (struct _mf_menu *) user_data;
- SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, "Playing with seed %ld, "
- "size %dx%d, fog of war %s, and maze %s",
- menu->seed, menu->size, menu->size,
- menu->fow ? "enabled" : "disabled",
- menu->reveal ? "revealed" : "hidden");
- /* TODO */
-
- return 0;
+ return mf_game(menu->seed, menu->size, menu->fow, menu->reveal,
+ menu->renderer);
}
static struct mftk_widget *
@@ -202,10 +198,11 @@ mf_menu(SDL_Renderer *renderer)
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.seed = rand();
+ menu.size = 15;
+ menu.fow = SDL_TRUE;
+ menu.reveal = SDL_FALSE;
+ menu.renderer = renderer;
font_path = mf_strcat(mf_get_fonts_dir(), "/FifteenTwenty-Regular.ttf");
title_font = TTF_OpenFont(font_path, MF_SPLASH_TITLE_FONT_S);