summaryrefslogtreecommitdiffstats
path: root/src/game.c
blob: 6a8be75c6bce9f02295252b6667867d9a2eb85de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 * 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 <errno.h>
#include <string.h>
#include "defs.h"
#include "dirs.h"
#include "game.h"
#include "maze.h"
#include "tk.h"
#include "util.h"

struct _mf_game {
	struct mftk_widget *timer;
	char                time_buf[6];
	Uint32              beg;
};

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, struct _mf_game *game)
{
	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;

	game->timer = mftk_text_new('\0', '\0', 5, "00:00", text_font,
			text_color, SDL_FALSE, NULL, NULL, NULL);
	return mftk_grid_new(2, 1, MF_ROW_M, MF_COL_M,
			game->timer,
				MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_T,
			mftk_button_new(text_font, "Exit", text_color,
				&butn_color, MF_BTN_P, &_mf_game_exit, NULL,
				renderer),
				MFTK_GRID_HALIGN_R|MFTK_GRID_VALIGN_B
			);
}

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 _mf_game     game;
	struct mftk_window *win         = NULL;
	int                 fr;
	Uint32              beg;
	int                 secs;
	Uint32              end;
	Uint32              delay;
	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_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_FORM_P, &form_color,
				_mf_game_form(renderer, text_font,
					&text_color, &game)));

	game.beg = SDL_GetTicks();

	fr = 30;
	while(1) {
		beg = SDL_GetTicks();
		while (SDL_PollEvent(&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);
		secs = (beg - game.beg) / 1000;
		if (sprintf(game.time_buf, "%02d:%02d", secs / 60, secs % 60)
				< 0) {
			SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
					"Couldn't allocate string: %s",
					strerror(errno));
			return -1;
		}
		mftk_text_set_value(game.timer, game.time_buf);
		mftk_window_render(win, renderer);
		SDL_RenderPresent(renderer);
		end = SDL_GetTicks();
		if ((Uint32) (1000 / fr) > (end - beg)) {
			delay = 1000 / fr - (end - beg);
		} else {
			SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
					"Frame took longer than frame period");
			delay = 0;
		}
		SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION,
				"Frame took %u ms, delaying %u ms",
				end - beg, delay);
		SDL_Delay(delay);
	}

 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;
}