From 73d85fa7352179ac26ee4346d5d7148329325777 Mon Sep 17 00:00:00 2001 From: P. J. McDermott Date: Sat, 07 Aug 2021 19:59:19 -0400 Subject: game: Add timer --- (limited to 'src') 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 #include +#include +#include #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(); -- cgit v0.9.1