diff options
author | P. J. McDermott <pj@pehjota.net> | 2021-08-08 16:41:14 (EDT) |
---|---|---|
committer | P. J. McDermott <pj@pehjota.net> | 2021-08-08 16:41:34 (EDT) |
commit | c234b317843200c5059c57c16621fb722f579fb0 (patch) | |
tree | 6e60229f6b21e7ca6ccc1d493cbea1855b9f1c0b /src | |
parent | 8f383005a08607d88f198a4e9ba55527986a71d2 (diff) | |
download | mazefight-c234b317843200c5059c57c16621fb722f579fb0.zip mazefight-c234b317843200c5059c57c16621fb722f579fb0.tar.gz mazefight-c234b317843200c5059c57c16621fb722f579fb0.tar.bz2 |
game: Implement fog of war
Diffstat (limited to 'src')
-rw-r--r-- | src/defs.h | 4 | ||||
-rw-r--r-- | src/game.c | 75 |
2 files changed, 77 insertions, 2 deletions
@@ -80,6 +80,10 @@ #define MF_COLOR_CHKM_G 0x00 #define MF_COLOR_CHKM_B 0x00 #define MF_COLOR_CHKM_A 0xFF +#define MF_COLOR_FOGW_R 0x2F /* Fog of war color */ +#define MF_COLOR_FOGW_G 0x2F +#define MF_COLOR_FOGW_B 0x2F +#define MF_COLOR_FOGW_A 0xFF #define MF_COLOR_PLYR_R 0x00 /* Player color */ #define MF_COLOR_PLYR_G 0xAF #define MF_COLOR_PLYR_B 0x7F @@ -62,6 +62,73 @@ _mf_game_form(SDL_Renderer *renderer, TTF_Font *text_font, ); } +static int +_mf_game_fow(SDL_Renderer *renderer, struct mf_maze *maze, + struct mf_player *player, int cw) +{ + int x1; + int y1; + int travel; + int dx; + int dy; + int x2; + int y2; + SDL_Rect rect; + + if (SDL_SetRenderDrawColor(renderer, + MF_COLOR_FOGW_R, MF_COLOR_FOGW_G, + MF_COLOR_FOGW_B, MF_COLOR_FOGW_A) < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \ + "Couldn't render fog: %s", \ + SDL_GetError()); \ + return -1; \ + } + + mf_player_get_vector(player, &x1, &y1, &travel, &dx, &dy); + x2 = x1, y2 = y1; + while (!mf_maze_is_wall(maze, dx >= 0 ? x2 : x1, dy >= 0 ? y2 : y1, + dx, dy)) { + if (dx >= 0) x2 += dx; + else x1 += dx; + if (dy >= 0) y2 += dy; + else y1 += dy; + } + ++x2, ++y2; + x1 *= cw, y1 *= cw, x2 *= cw, y2 *= cw; + if (dx >= 0) x1 += travel * dx; + else x2 += travel * dx; + if (dy >= 0) y1 += travel * dy; + else y2 += travel * dy; + ++x2, ++y2; + +#define mf_game_w_ MF_WINDOW_H +#define mf_game_h_ MF_WINDOW_H +#define mf_game_render_() \ + do { \ + if (SDL_RenderFillRect(renderer, &rect) < 0) { \ + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, \ + "Couldn't render fog: %s", \ + SDL_GetError()); \ + return -1; \ + } \ + } while (0) + + rect.x= 0, rect.y= 0, rect.w= x1, rect.h= mf_game_h_; + mf_game_render_(); + rect.x= x1, rect.y= 0, rect.w= x2-x1, rect.h= y1; + mf_game_render_(); + rect.x= x1, rect.y= y2, rect.w= x2-x1, rect.h= mf_game_h_-y2; + mf_game_render_(); + rect.x= x2, rect.y= 0, rect.w= mf_game_w_-x2, rect.h= mf_game_h_; + mf_game_render_(); + +#undef mf_game_w_ +#undef mf_game_h_ +#undef mf_game_render_ + + return 0; +} + int mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *renderer) { @@ -153,15 +220,19 @@ mf_game(long seed, int size, int fow, int reveal, SDL_Renderer *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); mf_player_render(player, renderer); + if (fow == SDL_TRUE && _mf_game_fow(renderer, maze, player, + MF_WINDOW_H / size) < 0) { + goto err; + } + 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; + goto err; } mftk_text_set_value(game.timer, game.time_buf); mftk_window_render(win, renderer); |