summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorP. J. McDermott <pj@pehjota.net>2021-08-07 19:59:19 (EDT)
committer P. J. McDermott <pj@pehjota.net>2021-08-07 20:01:57 (EDT)
commit73d85fa7352179ac26ee4346d5d7148329325777 (patch)
tree061ca701cfe2a8a9b54833778db105899069fd46 /src
parent7400b756adb8c65e5e30d4a46dbd052d547b6104 (diff)
downloadmazefight-73d85fa7352179ac26ee4346d5d7148329325777.zip
mazefight-73d85fa7352179ac26ee4346d5d7148329325777.tar.gz
mazefight-73d85fa7352179ac26ee4346d5d7148329325777.tar.bz2
game: Add timer
Diffstat (limited to 'src')
-rw-r--r--src/game.c37
1 files changed, 33 insertions, 4 deletions
diff --git a/src/game.c b/src/game.c
index 0b546c0..6a8be75 100644
--- a/src/game.c
+++ b/src/game.c
@@ -19,6 +19,8 @@
#include <SDL.h>
#include <SDL_ttf.h>
+#include <errno.h>
+#include <string.h>
#include "defs.h"
#include "dirs.h"
#include "game.h"
@@ -26,6 +28,12 @@
#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__)))
{
@@ -34,15 +42,23 @@ _mf_game_exit(void *user_data __attribute__((__unused__)))
static struct mftk_widget *
_mf_game_form(SDL_Renderer *renderer, TTF_Font *text_font,
- SDL_Color *text_color)
+ 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;
- return mftk_button_new(text_font, "Exit", text_color, &butn_color,
- MF_BTN_P, &_mf_game_exit, NULL, renderer);
+ 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
@@ -54,9 +70,11 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer)
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;
@@ -92,7 +110,9 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer)
MF_WINDOW_W - MF_WINDOW_H, MF_WINDOW_H,
MF_FORM_P, &form_color,
_mf_game_form(renderer, text_font,
- &text_color)));
+ &text_color, &game)));
+
+ game.beg = SDL_GetTicks();
fr = 30;
while(1) {
@@ -125,6 +145,15 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer)
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();