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
This commit is contained in:
mcolonna 2024-11-18 14:13:08 +01:00
parent 57b099d013
commit e7d5a5ce69
6 changed files with 88 additions and 72 deletions

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* const.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* map.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

22
include/minimap.h Normal file
View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minimap.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -3,91 +3,87 @@
/* ::: :::::::: */
/* minimap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
}
}
}

View file

@ -3,10 +3,10 @@
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: grobledo <grobledo@student.42.fr> +#+ +:+ +#+ */
/* By: mcolonna <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}