From 412dc89336bd0d211703ecd6351cad185ef5f32f Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Thu, 14 Aug 2025 19:52:34 +0200 Subject: [PATCH 1/5] fix: Transferred trailing magic error messages as consts --- src/consts.h | 9 ++++++++- src/map/populate_map.c | 4 ++-- src/map/setters.c | 16 ++++++++-------- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/consts.h b/src/consts.h index 84f5e96..563e3b4 100644 --- a/src/consts.h +++ b/src/consts.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ -/* Updated: 2025/08/12 14:49:36 by tchampio ### ########.fr */ +/* Updated: 2025/08/14 19:50:28 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,5 +48,12 @@ # define EMALMAP "Map is malformed (invalid chars or missing walls)" # define ENOPLAYER "No player" # define EHOLES "Holes in map" +# define ETRAILING "Trailing chars after map" +# define EBADCHARCOLOR "invalid characters in color definition" +# define EBADVALCOLOR "invalid value for colors" +# define ETOOMANYCOLORS "too many colors" +# define ENOTENOUGHCOLORS "Not enough colors" +# define EDUPTEX "Duplicated texture directive" +# define EMAPBFTEX "Map started before all the textures" #endif diff --git a/src/map/populate_map.c b/src/map/populate_map.c index e35240e..4caa366 100644 --- a/src/map/populate_map.c +++ b/src/map/populate_map.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:44:02 by kcolin #+# #+# */ -/* Updated: 2025/07/23 13:34:33 by kcolin ### ########.fr */ +/* Updated: 2025/08/14 19:46:26 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,7 +70,7 @@ bool populate_maps(t_mapdata *map, int fd) { if (line[0] != '\n' && end_reached) { - ft_strlcpy(map->error, "Trailing chars after map", ERRLEN); + ft_strlcpy(map->error, ETRAILING, ERRLEN); retvalue = false; } if (line[0] != '\n') diff --git a/src/map/setters.c b/src/map/setters.c index 0a82082..c3c003d 100644 --- a/src/map/setters.c +++ b/src/map/setters.c @@ -6,12 +6,13 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */ -/* Updated: 2025/07/31 14:08:27 by kcolin ### ########.fr */ +/* Updated: 2025/08/14 19:50:20 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #include "../../libft/includes/libft.h" #include "../utils/frees.h" +#include "../consts.h" #include "mapdata.h" bool perform_color_checks(int *color, char *strcolor, t_mapdata *map) @@ -24,8 +25,7 @@ bool perform_color_checks(int *color, char *strcolor, t_mapdata *map) { if (!ft_isdigit(strcolor[i])) { - ft_strlcpy(map->error, "invalid characters in color definition", - ERRLEN); + ft_strlcpy(map->error, EBADCHARCOLOR, ERRLEN); return (false); } i++; @@ -33,7 +33,7 @@ bool perform_color_checks(int *color, char *strcolor, t_mapdata *map) colorvalue = ft_atoi(strcolor); if (colorvalue < 0 || colorvalue > 255) { - ft_strlcpy(map->error, "invalid value for colors", ERRLEN); + ft_strlcpy(map->error, EBADVALCOLOR, ERRLEN); return (false); } *color = colorvalue; @@ -53,14 +53,14 @@ unsigned int set_color(const char *s, t_mapdata *map) while (tab[i]) { if (i > 2) - ft_strlcpy(map->error, "too many colors", ERRLEN); + ft_strlcpy(map->error, ETOOMANYCOLORS, ERRLEN); isok = perform_color_checks(&rgb[i], tab[i], map); free(tab[i]); i++; } free(tab); if (i <= 2) - return (ft_strlcpy(map->error, "Not enough colors", ERRLEN), 0); + return (ft_strlcpy(map->error, ENOTENOUGHCOLORS, ERRLEN), 0); if (!isok) return (0); finalcolor = ((rgb[0] & 0xff) << 16) @@ -77,7 +77,7 @@ int try_set_texture(t_mapdata *map, char **texture, char *texture_name) { if (*texture != NULL) { - ft_strlcpy(map->error, "Duplicated texture directive", ERRLEN); + ft_strlcpy(map->error, EDUPTEX, ERRLEN); return (2); } *texture = ft_strdup(texture_name); @@ -99,7 +99,7 @@ int set_textures(char *line, t_mapdata *map) tab = ft_split(line, ' '); if (tab[0][0] == '1') return (free_tab(tab), ft_strlcpy(map->error, - "Map started before all the textures", ERRLEN), 1); + EMAPBFTEX, ERRLEN), 1); retvalue = 0; if (tab[0] && tab[1]) { From 94075506f6529f0ae81558a528bea0a3df2f7b2a Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Thu, 14 Aug 2025 21:25:03 +0200 Subject: [PATCH 2/5] fix: Transferred sprite pathes to constants --- src/consts.h | 6 +++++- src/sprites/create_sprite.c | 10 +++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/consts.h b/src/consts.h index 563e3b4..ce437e1 100644 --- a/src/consts.h +++ b/src/consts.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:54:36 by kcolin #+# #+# */ -/* Updated: 2025/08/14 19:50:28 by tchampio ### ########.fr */ +/* Updated: 2025/08/14 21:23:25 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,6 +35,10 @@ // Sprite consts # define SPRITE_TRANPARENCY_COLOR 0xff00dc # define MAX_SPRITES 30 +# define JUGG_TEX "ressources/juggernog.xpm" +# define REV_TEX "ressources/revive.xpm" +# define DBLTAP_TEX "ressources/doubletap.xpm" +# define MYSTERY_TEX "ressources/box.xpm" # ifdef BONUS # define COMPILED_TEXT "Compiled with bonuses" diff --git a/src/sprites/create_sprite.c b/src/sprites/create_sprite.c index 1bea488..b55299f 100644 --- a/src/sprites/create_sprite.c +++ b/src/sprites/create_sprite.c @@ -6,7 +6,7 @@ /* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/08/12 15:43:19 by tchampio #+# #+# */ -/* Updated: 2025/08/12 16:14:27 by tchampio ### ########.fr */ +/* Updated: 2025/08/14 21:24:03 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,15 +34,15 @@ t_sprite *place_right_sprite(t_cub3d_data *data, char c, double x, double y) t_sprite *sprite; if (c == 'M') - sprite = create_sprite(data, "ressources/box.xpm", x + 0.5, y + 0.5); + sprite = create_sprite(data, MYSTERY_TEX, x + 0.5, y + 0.5); if (c == 'Q') sprite = create_sprite(data, - "ressources/revive.xpm", x + 0.5, y + 0.5); + REV_TEX, x + 0.5, y + 0.5); if (c == 'J') sprite = create_sprite(data, - "ressources/juggernog.xpm", x + 0.5, y + 0.5); + JUGG_TEX, x + 0.5, y + 0.5); if (c == 'D') sprite = create_sprite(data, - "ressources/doubletap.xpm", x + 0.5, y + 0.5); + DBLTAP_TEX, x + 0.5, y + 0.5); return (sprite); } From 4a70e4469720c351cb5abb5463979beeef3dd38d Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Thu, 14 Aug 2025 21:27:53 +0200 Subject: [PATCH 3/5] fix: Added a check for the sprites, to avoid segfaults and/or invalid writes --- src/utils/inits.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/utils/inits.c b/src/utils/inits.c index d904b41..39045e4 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */ -/* Updated: 2025/08/12 16:13:50 by tchampio ### ########.fr */ +/* Updated: 2025/08/14 21:26:14 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,8 +69,11 @@ void place_base_sprites(t_cub3d_data *data, char **map) { if (map[y][x] == 'M' || map[y][x] == 'Q' || map[y][x] == 'J' || map[y][x] == 'D') - data->sprite_list[c++] = place_right_sprite(data, - map[y][x], x, y); + { + if (c < MAX_SPRITES) + data->sprite_list[c++] = place_right_sprite(data, + map[y][x], x, y); + } x++; } y++; From 0d6cc4063ac33141695ac07f4bda5cc5c3d3a7bd Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Thu, 14 Aug 2025 21:38:49 +0200 Subject: [PATCH 4/5] dev: changed the c var in place_base_sprites to a counter in data struct --- src/cub3d_data.h | 3 ++- src/utils/inits.c | 10 ++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/cub3d_data.h b/src/cub3d_data.h index aff808c..1993a16 100644 --- a/src/cub3d_data.h +++ b/src/cub3d_data.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ -/* Updated: 2025/08/11 11:59:21 by tchampio ### ########.fr */ +/* Updated: 2025/08/14 21:36:15 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ typedef struct s_cub3d_data int delta; int last_tick; t_sprite **sprite_list; + int sprite_counter; double zbuffer[WIDTH]; int sprite_order[MAX_SPRITES]; double sprite_distances[MAX_SPRITES]; diff --git a/src/utils/inits.c b/src/utils/inits.c index 39045e4..d533f07 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */ -/* Updated: 2025/08/14 21:26:14 by tchampio ### ########.fr */ +/* Updated: 2025/08/14 21:37:58 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,10 +58,8 @@ void place_base_sprites(t_cub3d_data *data, char **map) { int y; int x; - int c; y = 0; - c = 0; while (y < data->map->mapheight) { x = 0; @@ -70,9 +68,9 @@ void place_base_sprites(t_cub3d_data *data, char **map) if (map[y][x] == 'M' || map[y][x] == 'Q' || map[y][x] == 'J' || map[y][x] == 'D') { - if (c < MAX_SPRITES) - data->sprite_list[c++] = place_right_sprite(data, - map[y][x], x, y); + if (data->sprite_counter < MAX_SPRITES) + data->sprite_list[data->sprite_counter++] = + place_right_sprite(data, map[y][x], x, y); } x++; } From 3511b6aaf8cdb29d3754efb802e617922f1f28e5 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Thu, 14 Aug 2025 21:45:39 +0200 Subject: [PATCH 5/5] norme: Fixed little oopsie --- src/utils/inits.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/utils/inits.c b/src/utils/inits.c index d533f07..ebc3a11 100644 --- a/src/utils/inits.c +++ b/src/utils/inits.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/31 13:43:05 by kcolin #+# #+# */ -/* Updated: 2025/08/14 21:37:58 by tchampio ### ########.fr */ +/* Updated: 2025/08/14 21:45:19 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -69,8 +69,8 @@ void place_base_sprites(t_cub3d_data *data, char **map) || map[y][x] == 'J' || map[y][x] == 'D') { if (data->sprite_counter < MAX_SPRITES) - data->sprite_list[data->sprite_counter++] = - place_right_sprite(data, map[y][x], x, y); + data->sprite_list[data->sprite_counter++] + = place_right_sprite(data, map[y][x], x, y); } x++; }