From e7d5a5ce697fe4a544cd6c363084cccde4ad5f86 Mon Sep 17 00:00:00 2001 From: mcolonna Date: Mon, 18 Nov 2024 14:13:08 +0100 Subject: [PATCH] clean code * - add constants for each minimap color - rename, reorganize and make static minimap functions - main.c:draw_screen() calls draw_minimap() instead of render(). - clean code and norm --- include/const.h | 10 +++-- include/map.h | 8 +--- include/minimap.h | 22 ++++++++++ src/main.c | 9 ++-- src/minimap.c | 106 ++++++++++++++++++++++------------------------ src/render.c | 5 +-- 6 files changed, 88 insertions(+), 72 deletions(-) create mode 100644 include/minimap.h diff --git a/include/const.h b/include/const.h index 569cb65..9d5a34d 100644 --- a/include/const.h +++ b/include/const.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* const.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: grobledo +#+ +:+ +#+ */ +/* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 17:00:59 by mcolonna #+# #+# */ -/* Updated: 2024/11/15 14:58:24 by grobledo ### ########.fr */ +/* Updated: 2024/11/18 13:40:04 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,5 +27,9 @@ # define FOV 0.66 // Width of the view plane (unit: cases) -# define MINIMAP_SCALE 8 +# define MINIMAP_SCALE 8 // Size of a case on the minimap. +# define MINIMAP_COLOR_WALL 0xFFAF33 // Color of the walls on the minimap +# define MINIMAP_COLOR_EMPTY 0xFFFC33 // Color of the empty cases on the minimap +# define MINIMAP_COLOR_PLAYER 0xFF0000 // Color of the player on the minimap + #endif diff --git a/include/map.h b/include/map.h index 9bcddf3..370075a 100644 --- a/include/map.h +++ b/include/map.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* map.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: grobledo +#+ +:+ +#+ */ +/* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/01 13:59:04 by mcolonna #+# #+# */ -/* Updated: 2024/11/15 14:56:55 by grobledo ### ########.fr */ +/* Updated: 2024/11/12 15:10:07 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -157,8 +157,4 @@ bool check_map(const t_map *map); /// @return Wanted case. const t_map_case *map_get_case(const t_map *map, int x, int y); -void create_minimap(u_int32_t *img_data); -void draw_minimap(uint32_t *img_data, int map_x, int map_y); -void minimap_player(u_int32_t *img_data); - #endif diff --git a/include/minimap.h b/include/minimap.h new file mode 100644 index 0000000..3a3d243 --- /dev/null +++ b/include/minimap.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* minimap.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: mcolonna +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/11/18 13:09:05 by mcolonna #+# #+# */ +/* Updated: 2024/11/18 13:14:12 by mcolonna ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef MINIMAP_H +# define MINIMAP_H + +# include "utils.h" + +/// @brief Draw the minimap on the image. +/// @param img_data Data of the image to draw the minimap on. +void draw_minimap(u_int32_t *img_data); + +#endif diff --git a/src/main.c b/src/main.c index 57b9f21..69b1b19 100644 --- a/src/main.c +++ b/src/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: grobledo +#+ +:+ +#+ */ +/* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 16:57:40 by mcolonna #+# #+# */ -/* Updated: 2024/11/15 12:47:03 by grobledo ### ########.fr */ +/* Updated: 2024/11/18 13:29:26 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ #include "input.h" #include "const.h" #include "global.h" +#include "minimap.h" static int g_return_value = 0; @@ -33,6 +34,7 @@ static void draw_screen(void) mlx_loop_end(g_mlx); } render(img_data); + draw_minimap(img_data); mlx_put_image_to_window(g_mlx, g_win, img_ptr, 0, 0); mlx_destroy_image(g_mlx, img_ptr); } @@ -79,6 +81,3 @@ int main(int argc, char *argv[]) free(g_mlx); return (g_return_value); } - - - diff --git a/src/minimap.c b/src/minimap.c index bfaeaf1..10a667f 100644 --- a/src/minimap.c +++ b/src/minimap.c @@ -3,91 +3,87 @@ /* ::: :::::::: */ /* minimap.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: grobledo +#+ +:+ +#+ */ +/* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/15 11:41:15 by grobledo #+# #+# */ -/* Updated: 2024/11/15 15:10:06 by grobledo ### ########.fr */ +/* Updated: 2024/11/18 14:19:40 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ -#include "../include/global.h" -#include "../include/const.h" +#include "minimap.h" -// g_map.width -// g_map.height -// g_map.player.pos.x -// g_map.player.pos.y -// g_map.cases[y * g_map.width + x].wall == WALL / EMPTY +#include "const.h" +#include "map.h" +#include "global.h" -void create_minimap(u_int32_t *img_data) +static void draw_minimap_wall(uint32_t *img_data, int map_y, int map_x); +static void draw_minimap_player(u_int32_t *img_data); + +void draw_minimap(u_int32_t *img_data) { unsigned int map_x; unsigned int map_y; map_x = 0; map_y = 0; - while (map_y < g_map.width) { map_x = 0; while (map_x < g_map.height) { - draw_minimap(img_data, map_x, map_y); - map_x++; + draw_minimap_wall(img_data, map_x, map_y); + map_x++; } map_y++; } - minimap_player(img_data); + draw_minimap_player(img_data); } -void draw_minimap(uint32_t *img_data, int map_y, int map_x) +static void draw_minimap_wall(uint32_t *img_data, int map_y, int map_x) { - int pixel_x; - int pixel_y; - int i; - int j; + const int map_case = map_y * g_map.width + map_x; + int pixel_x; + int pixel_y; + int pixel; - i = 0; - - while (i++ < MINIMAP_SCALE) + pixel_y = map_y * MINIMAP_SCALE; + while (pixel_y < (map_y + 1) * MINIMAP_SCALE) { - j = 0; - while (j++ < MINIMAP_SCALE) + pixel_x = map_x * MINIMAP_SCALE; + while (pixel_x < (map_x + 1) * MINIMAP_SCALE) { - pixel_y = map_y * MINIMAP_SCALE + j; - pixel_x = map_x * MINIMAP_SCALE + i; if (pixel_x < SCREEN_WIDTH && pixel_y < SCREEN_HEIGHT) { - if (g_map.cases[map_y * g_map.width + map_x].wall == WALL) - img_data[pixel_y * SCREEN_WIDTH + pixel_x] = 0xFFAF33; - else if (g_map.cases[map_y * g_map.width + map_x].wall == EMPTY && g_map.cases[map_y * g_map.width + map_x].inside) - img_data[pixel_y * SCREEN_WIDTH + pixel_x] = 0xFFFC33; + pixel = pixel_y * SCREEN_WIDTH + pixel_x; + if (g_map.cases[map_case].wall == WALL) + img_data[pixel] = MINIMAP_COLOR_WALL; + else if (g_map.cases[map_case].wall == EMPTY + && g_map.cases[map_case].inside) + img_data[pixel] = MINIMAP_COLOR_EMPTY; } + pixel_x++; + } + pixel_y++; + } +} + +static void draw_minimap_player(u_int32_t *img_data) +{ + const int player_x = g_map.player.pos.x * MINIMAP_SCALE; + const int player_y = g_map.player.pos.y * MINIMAP_SCALE; + int pixel_x; + int pixel_y; + int pixel; + + pixel_x = player_x - 2; + while (++pixel_x < player_x + 2) + { + pixel_y = player_y - 2; + while (++pixel_y < player_y + 2) + { + pixel = pixel_y * SCREEN_WIDTH + pixel_x; + if (pixel_x < SCREEN_WIDTH && pixel_y < SCREEN_HEIGHT) + img_data[pixel] = MINIMAP_COLOR_PLAYER; } } } -void minimap_player(u_int32_t *img_data) -{ - int player_x; - int player_y; - int pixel_x; - int pixel_y; - int i; - int j; - - i = -2; - player_x = g_map.player.pos.x * MINIMAP_SCALE; - player_y = g_map.player.pos.y * MINIMAP_SCALE; - while(i++ < 2) - { - j = -2; - while(j++ < 2) - { - pixel_x = player_x + i; - pixel_y = player_y + j; - if (pixel_x < SCREEN_WIDTH && pixel_y < SCREEN_HEIGHT) - img_data[pixel_y * SCREEN_WIDTH + pixel_x] = 0xFF0000; - } - } - -} \ No newline at end of file diff --git a/src/render.c b/src/render.c index 6823640..d5c88e5 100644 --- a/src/render.c +++ b/src/render.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* render.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: grobledo +#+ +:+ +#+ */ +/* By: mcolonna +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/14 14:55:05 by grobledo #+# #+# */ -/* Updated: 2024/11/15 13:00:52 by grobledo ### ########.fr */ +/* Updated: 2024/11/13 18:33:00 by mcolonna ### ########.fr */ /* */ /* ************************************************************************** */ @@ -118,6 +118,5 @@ int render(u_int32_t *img_data) draw_vertical_line(img_data, &ray); ray.x++; } - create_minimap(img_data); return (0); }