diff --git a/Makefile b/Makefile index cc7d2cf..669aa2e 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,9 @@ SANITIZERS= #-fsanitize-address -fno-omit-frame-pointer CFLAGS=-Wall -Wextra -Werror -g $(SANITIZERS) -I mlx SOURCEFILES=src/main.c \ - src/map/map_checker.c + src/map/map_checker.c \ + src/map/checkers.c \ + src/map/setters.c OBJECTS=$(patsubst src/%.c,objects/%.o,$(SOURCEFILES)) OBJDIR=objects NAME=cub3d diff --git a/includes/maputils.h b/includes/maputils.h index b96fb8d..b7745e3 100644 --- a/includes/maputils.h +++ b/includes/maputils.h @@ -1,7 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* maputils.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/06/21 19:34:43 by tchampio #+# #+# */ +/* Updated: 2025/06/21 19:47:07 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + #ifndef MAPUTILS_H # define MAPUTILS_H # include +# include "structs.h" // TODO: mettre ca dans un autre fichier # define RESET "\033[0m" @@ -13,25 +26,22 @@ # define CYAN "\033[36m" # define BOLD "\033[1m" -typedef struct s_mapdata -{ - char *filename; - char *NO_texture; - char *SO_texture; - char *WE_texture; - char *EA_texture; - int f_color; - int c_color; - int mapheight; - char **map; - char **mapflood; - int skipped_lines; - bool isvalid; - int startx; - int starty; - char error[1024]; -} t_mapdata; -bool check_cubfile(char *filename, t_mapdata *map); +bool check_cubfile(char *filename, t_mapdata *map); +bool check_filename(t_mapdata *map, char *file); +void populate_maps(t_mapdata *map, int fd); +bool check_walls(t_mapdata *map); +bool check_bare_minimum(t_mapdata *map); +bool has_forbidden_characters(char *line); +void print_mapdata(const t_mapdata *data); +unsigned long set_color(const char *s, t_mapdata *map); +bool set_textures(char *line, t_mapdata *map); +bool add_textures(int fd, t_mapdata *map); +void free_tab_length(char **tab, int length); +int copy_old_map(t_mapdata *map, char **newmap, + char **newmapflood, int length); +void add_map_line(const char *line, t_mapdata *map); +void print_map(char **map); +bool check_cubfile(char *file, t_mapdata *map); #endif diff --git a/includes/structs.h b/includes/structs.h new file mode 100644 index 0000000..1b29521 --- /dev/null +++ b/includes/structs.h @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* structs.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/06/21 19:46:20 by tchampio #+# #+# */ +/* Updated: 2025/06/21 19:48:20 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef STRUCTS_H +# define STRUCTS_H + +typedef struct s_mlx_data +{ + void *img; + char *addr; + int bits_per_pixel; + int line_length; + int endian; +} t_mlx_data; + +typedef enum e_direction +{ + NORTH, + SOUTH, + EAST, + WEST +} t_direction; + +typedef struct s_mapdata +{ + char *filename; + char *no_texture; + char *so_texture; + char *we_texture; + char *ea_texture; + int f_color; + int c_color; + int mapheight; + char **map; + char **mapflood; + int skipped_lines; + bool isvalid; + int startx; + int starty; + t_direction startdirection; + char error[1024]; +} t_mapdata; + +#endif diff --git a/src/main.c b/src/main.c index 4513252..2bd7447 100644 --- a/src/main.c +++ b/src/main.c @@ -6,28 +6,19 @@ /* By: tchampio #include #include -typedef struct s_mlx_data -{ - void *img; - char *addr; - int bits_per_pixel; - int line_length; - int endian; -} t_mlx_data; - - void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color) { char *dst; @@ -36,6 +27,24 @@ void my_mlx_pixel_put(t_mlx_data *data, int x, int y, int color) *(unsigned int*)dst = color; } +void draw_2d_wall(unsigned int color, t_mlx_data *data, int size, int x, int y) +{ + int i; + int j; + + i = 0; + while (i < size) + { + j = 0; + while (j < size) + { + my_mlx_pixel_put(data, x + i, y + j, color); + j++; + } + i++; + } +} + void free_tab(char **tab) { int i; @@ -54,10 +63,10 @@ int destroy(t_mapdata *map) { free_tab(map->map); free_tab(map->mapflood); - free(map->EA_texture); - free(map->NO_texture); - free(map->SO_texture); - free(map->WE_texture); + free(map->ea_texture); + free(map->no_texture); + free(map->so_texture); + free(map->we_texture); free(map->filename); exit(0); return 0; @@ -70,14 +79,31 @@ int key_destroy(int keycode, t_mapdata *map) return (0); } +void draw_map(t_mapdata *map, t_mlx_data *data) +{ + int i; + int j; + + i = 0; + while (map->map[i]) + { + j = 0; + while (map->map[i][j]) + { + if (map->map[i][j] == '1') + draw_2d_wall(map->f_color, data, 10, 10 * j, 10 * i); + else if (ft_strchr("NSEW", map->map[i][j])) + draw_2d_wall(0x00FF0000, data, 10, 10 * j, 10 * i); + j++; + } + i++; + } +} + int main(int argc, char **argv) { void *mlx; void *mlx_win; - char *xpm = "./ressources/test.xpm"; - void *xpm_image; - int xpm_height; - int xpm_width; t_mlx_data data; t_mapdata map; @@ -90,11 +116,9 @@ int main(int argc, char **argv) mlx_win = mlx_new_window(mlx, 800, 600, "Cub3d"); data.img = mlx_new_image(mlx, 800, 600); data.addr = mlx_get_data_addr(data.img, &data.bits_per_pixel, &data.line_length, &data.endian); - xpm_image = mlx_xpm_file_to_image(mlx, xpm, &xpm_width, &xpm_height); - //my_mlx_pixel_put(&data, 5, 5, 0x00FF0000); mlx_hook(mlx_win, 17, 0L, destroy, &map); + draw_map(&map, &data); mlx_key_hook(mlx_win, key_destroy, &map); mlx_put_image_to_window(mlx, mlx_win, data.img, 0, 0); - mlx_put_image_to_window(mlx, mlx_win, xpm_image, 0, 0); mlx_loop(mlx); } diff --git a/src/map/checkers.c b/src/map/checkers.c new file mode 100644 index 0000000..6c4ec34 --- /dev/null +++ b/src/map/checkers.c @@ -0,0 +1,127 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* checkers.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/06/21 19:29:45 by tchampio #+# #+# */ +/* Updated: 2025/06/21 19:32:04 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/maputils.h" +#include "../../includes/libft.h" + +bool check_filename(t_mapdata *map, char *file) +{ + int filename_size; + int i; + int j; + char end[5]; + + filename_size = ft_strlen(file); + i = filename_size; + j = 4; + ft_bzero(end, 5); + while (i > (filename_size - 5)) + { + end[j] = file[i]; + i--; + j--; + } + if (ft_strncmp(end, ".cub", 5)) + return (false); + map->filename = ft_strdup(file); + return (true); +} + +void populate_maps(t_mapdata *map, int fd) +{ + char *line; + + line = get_next_line(fd); + while (line) + { + if (line[0] != '\n') + add_map_line(line, map); + free(line); + line = get_next_line(fd); + } + free(line); +} + +bool check_walls(t_mapdata *map) +{ + int i; + int j; + + i = 0; + while (i < map->mapheight) + { + j = 0; + while (map->map[i][j] == ' ') + j++; + if (map->map[i][j] != '1' + && map->map[i][ft_strlen(map->map[i]) - 1] != '1') + return (false); + if (i == 0 || i == map->mapheight - 1) + { + while (map->map[i][j] == '1' || map->map[i][j] == ' ' + || map->map[i][j] == '\n') + j++; + if (ft_strlen(map->map[i]) != (unsigned int)j) + return (false); + } + if (has_forbidden_characters(map->map[i])) + return (false); + i++; + } + return (true); +} + +bool check_bare_minimum(t_mapdata *map) +{ + int i; + int spawncount; + int j; + + i = 0; + spawncount = 0; + while (map->map[i]) + { + j = 0; + while (map->map[i][j]) + { + if (map->map[i][j] == 'N' || map->map[i][j] == 'E' + || map->map[i][j] == 'S' || map->map[i][j] == 'W') + { + spawncount++; + map->startx = j; + map->starty = i; + } + j++; + } + i++; + } + if (spawncount == 1) + return (true); + return (false); +} + +bool has_forbidden_characters(char *line) +{ + static const char *allowedchars = " 10NSEW\n"; + size_t strsize; + int i; + + strsize = ft_strlen(line); + i = 0; + while (i < (int)strsize) + { + if (!ft_strchr(allowedchars, line[i])) + return (true); + i++; + } + return (false); +} diff --git a/src/map/map_checker.c b/src/map/map_checker.c index 996a21d..8f7699e 100644 --- a/src/map/map_checker.c +++ b/src/map/map_checker.c @@ -6,7 +6,7 @@ /* By: tchampio filename); - ft_printf(BOLD "NO Texture: " RESET "%s\n", data->NO_texture); - ft_printf(BOLD "SO Texture: " RESET "%s\n", data->SO_texture); - ft_printf(BOLD "WE Texture: " RESET "%s\n", data->WE_texture); - ft_printf(BOLD "EA Texture: " RESET "%s\n", data->EA_texture); + ft_printf(BOLD "NO Texture: " RESET "%s\n", data->no_texture); + ft_printf(BOLD "SO Texture: " RESET "%s\n", data->so_texture); + ft_printf(BOLD "WE Texture: " RESET "%s\n", data->we_texture); + ft_printf(BOLD "EA Texture: " RESET "%s\n", data->ea_texture); ft_printf(BOLD "F color: " RESET "%x\n", data->f_color); ft_printf(BOLD "C color: " RESET "%x\n", data->c_color); ft_printf(BOLD "Validity: " RESET); @@ -38,105 +38,6 @@ void print_mapdata(const t_mapdata *data) ft_printf(CYAN "=================\n" RESET); } -unsigned long set_color(const char *s, t_mapdata *map) -{ - int i; - unsigned long finalcolor; - char **tab; - int rgb[100]; - - i = 0; - tab = ft_split(s, ','); - while (tab[i]) - { - if (i > 2) - ft_strlcpy(map->error, "too many colors", 16); - rgb[i] = ft_atoi(tab[i]); - free(tab[i]); - i++; - } - free(tab); - if (rgb[0] < 0 || rgb[0] > 255 - || rgb[1] < 0 || rgb[1] > 255 - || rgb[2] < 0 || rgb[2] > 255) - return (ft_strlcpy(map->error, "Invalid color", 14), 0); - finalcolor = ((rgb[0] & 0xff) << 16) - + ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff); - return (finalcolor); -} - -bool set_textures(char *line, t_mapdata *map) -{ - char **tab; - int i; - - tab = ft_split(line, ' '); - i = 0; - if (tab[0][0] == '1') - return (false); - if (tab[0] && tab[1]) - { - if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3)) - map->NO_texture = ft_strdup(tab[1]); - if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3)) - map->SO_texture = ft_strdup(tab[1]); - if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3)) - map->WE_texture = ft_strdup(tab[1]); - if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3)) - map->EA_texture = ft_strdup(tab[1]); - if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2)) - map->f_color = set_color(tab[1], map); - if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2)) - map->c_color = set_color(tab[1], map); - } - while (tab[i]) - free(tab[i++]); - return (free(tab), true); -} - -bool add_textures(int fd, t_mapdata *map) -{ - char *line; - int set_lines; - - line = get_next_line(fd); - set_lines = 0; - while (line && set_lines < 6) - { - if (!set_textures(line, map)) - return (free(line), false); - else - set_lines++; - free(line); - line = get_next_line(fd); - } - free(line); - return (true); -} - -bool check_filename(t_mapdata *map, char *file) -{ - int filename_size; - int i; - int j; - char end[5]; - - filename_size = ft_strlen(file); - i = filename_size; - j = 4; - ft_bzero(end, 5); - while (i > (filename_size - 5)) - { - end[j] = file[i]; - i--; - j--; - } - if (ft_strncmp(end, ".cub", 5)) - return (false); - map->filename = ft_strdup(file); - return (true); -} - void free_tab_length(char **tab, int length) { int i; @@ -152,148 +53,6 @@ void free_tab_length(char **tab, int length) free(tab); } -int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length) -{ - int i; - - i = 0; - if (!map->map) - return (0); - while (i < length) - { - newmap[i] = ft_strdup(map->map[i]); - newmapflood[i] = ft_strdup(map->mapflood[i]); - i++; - } - return (i); -} - -void add_map_line(const char *line, t_mapdata *map) -{ - static int length = 0; - char **newmap; - char **newmapflood; - int i; - - newmap = ft_calloc(sizeof(char *), (length + 2)); - if (!newmap) - return ; - newmapflood = ft_calloc(sizeof(char *), (length + 2)); - if (!newmapflood) - return ; - i = copy_old_map(map, newmap, newmapflood, length); - newmap[i] = ft_strdup(line); - newmapflood[i] = ft_strdup(line); - free_tab_length(map->map, length); - free_tab_length(map->mapflood, length); - map->mapheight = ++length; - map->map = newmap; - map->mapflood = newmapflood; -} - -void populate_maps(t_mapdata *map, int fd) -{ - char *line; - - line = get_next_line(fd); - while (line) - { - if (line[0] != '\n') - add_map_line(line, map); - free(line); - line = get_next_line(fd); - } - free(line); -} - -bool has_forbidden_characters(char *line) -{ - static const char *allowedchars = " 10NSEW\n"; - size_t strsize; - int i; - - strsize = ft_strlen(line); - i = 0; - while (i < (int)strsize) - { - if (!ft_strchr(allowedchars, line[i])) - return (true); - i++; - } - return (false); -} - -bool check_walls(t_mapdata *map) -{ - int i; - int j; - - i = 0; - while (i < map->mapheight) - { - j = 0; - while (map->map[i][j] == ' ') - j++; - if (map->map[i][j] != '1' - && map->map[i][ft_strlen(map->map[i]) - 1] != '1') - return (false); - if (i == 0 || i == map->mapheight - 1) - { - while (map->map[i][j] == '1' || map->map[i][j] == ' ' - || map->map[i][j] == '\n') - j++; - if (ft_strlen(map->map[i]) != (unsigned int)j) - return (false); - } - if (has_forbidden_characters(map->map[i])) - return (false); - i++; - } - return (true); -} - -void print_map(char **map) -{ - int i; - - i = 0; - while (map && map[i]) - { - ft_printf("line2: %s", map[i]); - i++; - } - ft_printf("(%p) %s", map[i], map[i]); -} - -bool check_bare_minimum(t_mapdata *map) -{ - int i; - int spawncount; - int j; - - i = 0; - spawncount = 0; - while (map->map[i]) - { - j = 0; - while (map->map[i][j]) - { - if (map->map[i][j] == 'N' || map->map[i][j] == 'E' - || map->map[i][j] == 'S' || map->map[i][j] == 'W') - { - spawncount++; - map->startx = j; - map->starty = i; - } - j++; - } - i++; - } - if (spawncount == 1) - return (true); - return (false); -} - bool check_cubfile(char *file, t_mapdata *map) { int fd; @@ -315,5 +74,6 @@ bool check_cubfile(char *file, t_mapdata *map) false); if (!check_bare_minimum(map)) return (close(fd), false); + map->isvalid = true; return (true); } diff --git a/src/map/setters.c b/src/map/setters.c new file mode 100644 index 0000000..648e4be --- /dev/null +++ b/src/map/setters.c @@ -0,0 +1,129 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* setters.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/06/21 19:35:43 by tchampio #+# #+# */ +/* Updated: 2025/06/21 19:39:32 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/maputils.h" +#include "../../includes/libft.h" + +unsigned long set_color(const char *s, t_mapdata *map) +{ + int i; + unsigned long finalcolor; + char **tab; + int rgb[100]; + + i = 0; + tab = ft_split(s, ','); + while (tab[i]) + { + if (i > 2) + ft_strlcpy(map->error, "too many colors", 16); + rgb[i] = ft_atoi(tab[i]); + free(tab[i]); + i++; + } + free(tab); + if (rgb[0] < 0 || rgb[0] > 255 + || rgb[1] < 0 || rgb[1] > 255 + || rgb[2] < 0 || rgb[2] > 255) + return (ft_strlcpy(map->error, "Invalid color", 14), 0); + finalcolor = ((rgb[0] & 0xff) << 16) + + ((rgb[1] & 0xff) << 8) + (rgb[2] & 0xff); + return (finalcolor); +} + +bool set_textures(char *line, t_mapdata *map) +{ + char **tab; + int i; + + tab = ft_split(line, ' '); + i = 0; + if (tab[0][0] == '1') + return (false); + if (tab[0] && tab[1]) + { + if (!ft_strncmp(tab[0], "NO", 3) || !ft_strncmp(tab[0], "no", 3)) + map->no_texture = ft_strdup(tab[1]); + if (!ft_strncmp(tab[0], "SO", 3) || !ft_strncmp(tab[0], "so", 3)) + map->so_texture = ft_strdup(tab[1]); + if (!ft_strncmp(tab[0], "WE", 3) || !ft_strncmp(tab[0], "we", 3)) + map->we_texture = ft_strdup(tab[1]); + if (!ft_strncmp(tab[0], "EA", 3) || !ft_strncmp(tab[0], "ea", 3)) + map->ea_texture = ft_strdup(tab[1]); + if (!ft_strncmp(tab[0], "F", 2) || !ft_strncmp(tab[0], "f", 2)) + map->f_color = set_color(tab[1], map); + if (!ft_strncmp(tab[0], "C", 2) || !ft_strncmp(tab[0], "c", 2)) + map->c_color = set_color(tab[1], map); + } + while (tab[i]) + free(tab[i++]); + return (free(tab), true); +} + +bool add_textures(int fd, t_mapdata *map) +{ + char *line; + int set_lines; + + line = get_next_line(fd); + set_lines = 0; + while (line && set_lines < 6) + { + if (!set_textures(line, map)) + return (free(line), false); + else + set_lines++; + free(line); + line = get_next_line(fd); + } + free(line); + return (true); +} + +int copy_old_map(t_mapdata *map, char **newmap, char **newmapflood, int length) +{ + int i; + + i = 0; + if (!map->map) + return (0); + while (i < length) + { + newmap[i] = ft_strdup(map->map[i]); + newmapflood[i] = ft_strdup(map->mapflood[i]); + i++; + } + return (i); +} + +void add_map_line(const char *line, t_mapdata *map) +{ + static int length = 0; + char **newmap; + char **newmapflood; + int i; + + newmap = ft_calloc(sizeof(char *), (length + 2)); + if (!newmap) + return ; + newmapflood = ft_calloc(sizeof(char *), (length + 2)); + if (!newmapflood) + return ; + i = copy_old_map(map, newmap, newmapflood, length); + newmap[i] = ft_strdup(line); + newmapflood[i] = ft_strdup(line); + free_tab_length(map->map, length); + free_tab_length(map->mapflood, length); + map->mapheight = ++length; + map->map = newmap; + map->mapflood = newmapflood; +}