diff --git a/includes/cub3d_parsing.h b/includes/cub3d_parsing.h index 9e424b4..d66f95a 100644 --- a/includes/cub3d_parsing.h +++ b/includes/cub3d_parsing.h @@ -6,13 +6,14 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/11 14:41:07 by bgoulard #+# #+# */ -/* Updated: 2024/11/18 11:32:00 by bgoulard ### ########.fr */ +/* Updated: 2024/12/01 17:53:02 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CUB3D_PARSING_H # define CUB3D_PARSING_H +# include "cub3d_struct.h" # include # define C3D_PRS_PLS "SO" @@ -24,30 +25,14 @@ # define C3D_PRS_WLL '1' # define C3D_PRS_EMP '0' -enum e_tile_m -{ - EMPTY, - DOOR_OPEN, - DOOR_ANIM, - DOOR_CLOSE, - WALL_TRAIL, - WALL, - PLAYER_N, - PLAYER_S, - PLAYER_E, - PLAYER_W -}; +t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos); +bool is_identifier(const char *str, const char **id_str); -typedef struct s_map_truth -{ - enum e_tile_m tile; - char chr[2]; -} t_map_truth; - -typedef struct s_tile -{ - enum e_tile_m tile; - bool visited; -} t_tile; +void *load_file(void *data); +void *load_bgs(void *data); +void *load_map(void *data); +void *load_textures(void *data); +void *load_tiles(void *data); +void *traverse_map(void *data); #endif /* CUB3D_PARSING_H */ diff --git a/parsing/load_bgs.c b/parsing/load_bgs.c new file mode 100644 index 0000000..3bff69d --- /dev/null +++ b/parsing/load_bgs.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* load_bgs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 17:47:37 by bgoulard #+# #+# */ +/* Updated: 2024/12/01 17:52:28 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d_struct.h" +#include "cub3d_parsing.h" +#include "ft_string.h" + +bool color_from_str(const char *str, t_color *color) +{ + int col[4]; + char **split; + + split = ft_split(str, ','); + if (split == NULL) + return (false); + col[0] = ft_atoi(split[0]); + col[1] = ft_atoi(split[1]); + col[2] = ft_atoi(split[2]); + if (split[3]) + col[3] = ft_atoi(split[3]); + else + col[3] = 255; + if (col[0] < 0 || col[0] > 255 || col[1] < 0 || col[1] > 255 || col[2] < 0 + || col[2] > 255 || col[3] < 0 || col[3] > 255) + return (false); + *color = (t_color){.r = col[0], .g = col[1], .b = col[2], .a = col[3]}; + ft_free_2d((void **)split); + return (true); +} + +bool load_bg(t_info *info, const char *line, const char **id_str) +{ + t_color color; + size_t i; + + i = 0; + ft_bzero(&color, sizeof(t_color)); + while (id_str[i]) + { + if (ft_strstart_with(line, id_str[i]) && (color_from_str(line + + ft_strlen(id_str[i]), &color) == false || color.a < 255)) + return (info->last_error = ERROR_PARSE, false); + if (ft_strstart_with(id_str[i], "F ")) + info->map.bg_colors[BG_FLR] = color; + else + info->map.bg_colors[BG_CLG] = color; + i++; + } + if (color.color == 0) + return (info->last_error = ERROR_PARSE, false); + return (true); +} + +void *load_bgs(void *data) +{ + const char *id_str[] = {"F ", "C ", NULL}; + t_info *info; + size_t i; + + i = 0; + info = (t_info *)data; + while (info->map.fraw[i]) + { + if (is_identifier(info->map.fraw[i], id_str) && load_bg(info, + info->map.fraw[i], id_str) == false) + return (NULL); + i++; + } + return (info); +} diff --git a/parsing/load_file.c b/parsing/load_file.c new file mode 100644 index 0000000..5b1b056 --- /dev/null +++ b/parsing/load_file.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* load_file.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 17:51:46 by bgoulard #+# #+# */ +/* Updated: 2024/12/01 17:51:54 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_string.h" +#include "cub3d_struct.h" + +void *load_file(void *data) +{ + t_info *info; + char *file; + + file = NULL; + info = (t_info *)data; + file = ft_fd_to_buff(info->map.fd); + if (file == NULL) + return (info->errno_state = errno, info->last_error = ERROR_READ_FILE, + NULL); + info->map.fraw = ft_split(file, '\n'); + if (info->map.fraw == NULL) + return (info->errno_state = errno, info->last_error = ERROR_MALLOC, + NULL); + ft_free((void **)&file); + info->last_error = NO_ERROR; + return (info); +} diff --git a/parsing/load_textures.c b/parsing/load_textures.c new file mode 100644 index 0000000..618a868 --- /dev/null +++ b/parsing/load_textures.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* load_textures.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 17:46:52 by bgoulard #+# #+# */ +/* Updated: 2024/12/01 17:53:44 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d_struct.h" +#include "cub3d_parsing.h" + +#include "ft_string.h" + +#include "mlx_functions.h" +#include + +bool load_texture(t_info *info, const char *str, const char **id_str) +{ + size_t i; + t_texture texture; + + i = 0; + while (i < 4) + { + if (ft_strstart_with(str, id_str[i])) + { + texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); + if (texture.path == NULL) + return (info->errno_state = errno, + info->last_error = ERROR_MALLOC, false); + if (ft_strend_with(texture.path, ".xpm") == false) + return (info->last_error = ERROR_TEXTURE_FORMAT, false); + texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path, + &texture.width, &texture.height); + if (texture.img == NULL) + return (info->errno_state = errno, info->last_error = ERROR_MLX, + false); + return (info->map.texture_[i] = texture, true); + } + i++; + } + return (false); +} + +void *load_textures(void *data) +{ + const char *id_str[] = {"NO ", "SO ", "WE ", "EA ", NULL}; + t_info *info; + size_t i; + + i = 0; + info = (t_info *)data; + while (info->map.fraw[i]) + { + if (is_identifier(info->map.fraw[i], id_str) && load_texture(info, + info->map.fraw[i], id_str) == false) + return (NULL); + i++; + } + return (info); +} diff --git a/parsing/load_tiles.c b/parsing/load_tiles.c new file mode 100644 index 0000000..86cf811 --- /dev/null +++ b/parsing/load_tiles.c @@ -0,0 +1,110 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* load_tiles.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 17:47:15 by bgoulard #+# #+# */ +/* Updated: 2024/12/01 17:53:53 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d_struct.h" +#include "cub3d_parsing.h" + +#include "ft_string.h" +#include "ft_vector.h" +#include "ft_math.h" + +void *ft_strchrs(const char *str, const char *chrs) +{ + while (*str) + { + if (ft_strchr(chrs, *str)) + return ((void *)str); + str++; + } + return (NULL); +} + +void str_to_tile(const char *str, t_tile *tile, size_t size) +{ + size_t i; + + i = 0; + while (str[i]) + { + if (str[i] == '1' || str[i] == ' ') + tile[i].tile_type = WALL; + else + tile[i].tile_type = EMPTY; + i++; + } + while (i < size) + tile[i++].tile_type = WALL; +} + +t_vector *load_vector(t_map *map) +{ + const char *id_str__all[] = {"NO ", "SO ", "WE ", "EA ", "F ", "C ", NULL}; + t_vector *str_map; + size_t i; + + str_map = ft_vec_new(); + if (str_map == NULL) + return (NULL); + i = 0; + while (map->fraw[i] && is_identifier(map->fraw[i], id_str__all)) + i++; + while (map->fraw[i + map->size.y]) + { + map->size.x = ft_max(map->size.x, ft_strlen(map->fraw[i])); + ft_vec_add(&str_map, map->fraw[i + map->size.y++]); + } + return (str_map); +} + +int set_player(t_info *info, int i, t_vector *str_map) +{ + t_dpoint pos; + + ft_bzero(&pos, sizeof(t_dpoint)); + if (info->player.pos.x != 0 || info->player.pos.y != 0 || i == 0) + return (ft_vec_destroy(&str_map), info->last_error = ERROR_PARSE, \ + EXIT_FAILURE); + pos.y = i + .5; + pos.x = ft_strchrs(ft_vec_at(str_map, i), "SNWE") + - ft_vec_at(str_map, i) + .5; + info->player.pos = pos; + info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y}; + return (EXIT_SUCCESS); +} + +void *load_tiles(void *data) +{ + t_info *info; + t_vector *str_map; + size_t i; + + info = (t_info *)data; + str_map = load_vector(&info->map); + if (!str_map) + return (info->last_error = ERROR_MALLOC, NULL); + info->map.map = ft_calloc(sizeof(t_tile), (info->map.size.y + * info->map.size.x)); + if (!info->map.map) + return (ft_vec_destroy(&str_map), info->last_error = ERROR_MALLOC, + NULL); + i = 0; + while (ft_vec_at(str_map, i)) + { + str_to_tile(ft_vec_at(str_map, i), info->map.map + (i + * info->map.size.x), info->map.size.x); + if (ft_strchrs(ft_vec_at(str_map, i), "SNWE") && \ + set_player(info, i, str_map) != EXIT_SUCCESS) + return (NULL); + i++; + } + return (ft_vec_destroy(&str_map), info); +} diff --git a/parsing/map.c b/parsing/map.c index 2d3822f..5d49bd9 100644 --- a/parsing/map.c +++ b/parsing/map.c @@ -6,312 +6,22 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/12 08:31:06 by bgoulard #+# #+# */ -/* Updated: 2024/11/29 16:46:52 by bgoulard ### ########.fr */ +/* Updated: 2024/12/01 17:51:13 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ // proj #include "cub3d.h" #include "cub3d_struct.h" +#include "cub3d_parsing.h" // mlx -#include "mlx_functions.h" // libft - types -#include "ft_vector_types.h" #include "ft_optional_types.h" // libft -#include "ft_math.h" -#include "ft_string.h" -#include "ft_vector.h" #include "ft_optional.h" // sys std -#include #include -#include -#include - -void *load_file(void *data) -{ - t_info *info; - char *file; - - file = NULL; - info = (t_info *)data; - file = ft_fd_to_buff(info->map.fd); - if (file == NULL) - return (info->errno_state = errno, - info->last_error = ERROR_READ_FILE, NULL); - info->map.fraw = ft_split(file, '\n'); - if (info->map.fraw == NULL) - return (info->errno_state = errno, -info->last_error = ERROR_MALLOC, NULL); - ft_free((void **)&file); - info->last_error = NO_ERROR; - return (info); -} - -bool is_identifier(const char *str, const char **id_str) -{ - size_t i; - - i = 0; - while (id_str[i]) - { - if (ft_strstart_with(str, id_str[i])) - return (true); - i++; - } - return (false); -} - -bool load_texture(t_info *info, const char *str, const char **id_str) -{ - size_t i; - t_texture texture; - - i = 0; - while (i < 4) - { - if (ft_strstart_with(str, id_str[i])) - { - texture.path = ft_strtrim(str + ft_strlen(id_str[i]), " "); - if (texture.path == NULL) - return (info->errno_state = errno, -info->last_error = ERROR_MALLOC, false); - if (ft_strend_with(texture.path, ".xpm") == false) - return (info->last_error = ERROR_TEXTURE_FORMAT, false); - texture.img = mlx_xpm_file_to_image(info->mlx_ptr, texture.path, \ - &texture.width, &texture.height); - if (texture.img == NULL) - return (info->errno_state = errno, -info->last_error = ERROR_MLX, false); - return (info->map.texture_[i] = texture, true); - } - i++; - } - return (false); -} - -bool color_from_str(const char *str, t_color *color) -{ - int col[4]; - char **split; - - split = ft_split(str, ','); - if (split == NULL) - return (false); - col[0] = ft_atoi(split[0]); - col[1] = ft_atoi(split[1]); - col[2] = ft_atoi(split[2]); - if (split[3]) - col[3] = ft_atoi(split[3]); - else - col[3] = 255; - if (col[0] < 0 || col[0] > 255 || col[1] < 0 || col[1] > 255 || \ - col[2] < 0 || col[2] > 255 || col[3] < 0 || col[3] > 255) - return (false); - *color = (t_color){.r = col[0], .g = col[1], .b = col[2], .a = col[3]}; - ft_free_2d((void **)split); - return (true); -} - -bool load_bg(t_info *info, const char *line, const char **id_str) -{ - t_color color; - size_t i; - - i = 0; - ft_bzero(&color, sizeof(t_color)); - while (id_str[i]) - { - if (ft_strstart_with(line, id_str[i]) && \ - (color_from_str(line + ft_strlen(id_str[i]), &color) == false || - color.a < 255)) - return (info->last_error = ERROR_PARSE, false); - if (ft_strstart_with(id_str[i], "F ")) - info->map.bg_colors[BG_FLR] = color; - else - info->map.bg_colors[BG_CLG] = color; - i++; - } - if (color.color == 0) - return (info->last_error = ERROR_PARSE, false); - return (true); -} - -void *load_textures(void *data) -{ - const char *id_str[] = {"NO ", "SO ", "WE ", "EA ", NULL}; - t_info *info; - size_t i; - - i = 0; - info = (t_info *)data; - while (info->map.fraw[i]) - { - if (is_identifier(info->map.fraw[i], id_str) && \ - load_texture(info, info->map.fraw[i], id_str) == false) - return (NULL); - i++; - } - return (info); -} - -void *load_bgs(void *data) -{ - const char *id_str[] = {"F ", "C ", NULL}; - t_info *info; - size_t i; - - i = 0; - info = (t_info *)data; - while (info->map.fraw[i]) - { - if (is_identifier(info->map.fraw[i], id_str) && \ - load_bg(info, info->map.fraw[i], id_str) == false) - return (NULL); - i++; - } - return (info); -} - -void *ft_strchrs(const char *str, const char *chrs) -{ - while (*str) - { - if (ft_strchr(chrs, *str)) - return ((void *)str); - str++; - } - return (NULL); -} - -void str_to_tile(const char *str, t_tile *tile, size_t size) -{ - size_t i; - - i = 0; - while (str[i]) - { - if (str[i] == '1' || str[i] == ' ') - tile[i].tile_type = WALL; - else - tile[i].tile_type = EMPTY; - i++; - } - while (i < size) - tile[i++].tile_type = WALL; -} - -t_vector *load_vector(t_map *map) -{ - const char *id_str__all[] = {"NO ", "SO ", "WE ", "EA ", "F ", "C ", NULL}; - t_vector *str_map; - size_t i; - - str_map = ft_vec_new(); - if (str_map == NULL) - return (NULL); - i = 0; - while (map->fraw[i] && - is_identifier(map->fraw[i], id_str__all)) - i++; - while (map->fraw[i + map->size.y]) - { - map->size.x = ft_max(map->size.x, ft_strlen(map->fraw[i])); - ft_vec_add(&str_map, map->fraw[i + map->size.y++]); - } - return (str_map); -} - -void *load_tiles(void *data) -{ - t_info *info; - t_vector *str_map; - size_t i; - t_dpoint pos; - - info = (t_info *)data; - str_map = load_vector(&info->map); - if (!str_map) - return (info->last_error = ERROR_MALLOC, NULL); - info->map.map = ft_calloc(sizeof(t_tile), \ - (info->map.size.y * info->map.size.x)); - if (!info->map.map) - return (ft_vec_destroy(&str_map), \ - info->last_error = ERROR_MALLOC, NULL); - i = 0; - ft_bzero(&pos, sizeof(t_dpoint)); - while (ft_vec_at(str_map, i)) - { - str_to_tile(ft_vec_at(str_map, i), info->map.map + \ - (i * info->map.size.x), info->map.size.x); - if (ft_strchrs(ft_vec_at(str_map, i), "SNWE")) - { - if (pos.x != 0 || pos.y != 0 || i == 0) - return (ft_vec_destroy(&str_map), \ - info->last_error = ERROR_PARSE, NULL); - pos.y = i + .5; - pos.x = ft_strchrs(ft_vec_at(str_map, i), "SNWE") - ft_vec_at(str_map, i) + .5; - info->player.pos = pos; - info->player.pos_i = (t_ipoint){.x = (int)pos.x, .y = (int)pos.y}; - } - i++; - } - return (ft_vec_destroy(&str_map), info); -} - -t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos) -{ - if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x || pos.y >= dimensions.y) - return (printf("runtime error: %s:%d (pos:%d,%d on dims:%d,%d)\n", - __func__, __LINE__, pos.x, pos.y, dimensions.x, dimensions.y), - NULL); - return (map + (pos.y * dimensions.x + pos.x)); -} - -bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs) -{ - t_tile *current; - size_t i; - const t_ipoint to_check[] = { - (t_ipoint){pos.x + 1, pos.y}, - (t_ipoint){pos.x - 1, pos.y}, - (t_ipoint){pos.x, pos.y + 1}, - (t_ipoint){pos.x, pos.y - 1}, - (t_ipoint){pos.x + 1, pos.y + 1}, - (t_ipoint){pos.x - 1, pos.y + 1}, - (t_ipoint){pos.x + 1, pos.y - 1}, - (t_ipoint){pos.x - 1, pos.y - 1}, - }; - - if (pos.x < 0 || pos.y < 0 || pos.x >= maxs.x || pos.y >= maxs.y) - return (false); - current = c3_get_cell(tiles, maxs, pos); - if (current->tile_visited == true || current->tile_type == WALL) - return (true); - current->tile_visited = true; -// printf("dbg (marker %s:%d) pos (%03d : %03d), max (%03d, %03d)\n", -// __func__, __LINE__, pos.x, pos.y, maxs.x, maxs.y); - i = 0; - while (i != (sizeof(to_check) / sizeof(to_check[0]))) - if (flood_fill(tiles, to_check[i++], maxs) == false) - return (false); - return (true); -} - -void *traverse_map(void *data) -{ -// printf("dbg marker %s\n", __func__); - t_info *info; - t_ipoint pos_start; - - info = (t_info *)data; - pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y}; - if (flood_fill(info->map.map, pos_start, info->map.size) == false) - return (info->last_error = ERROR_PARSE, NULL); - return (info); -} void parse_map(t_info *info) { @@ -329,7 +39,6 @@ void parse_map(t_info *info) info->map.path = info->cli_ctx.file; if (ft_optional_chain(&opt, function_list) == false) return (c3_perror(info), (void)0); - info->player.plane = (t_dpoint){.x = 0, .y = - 2 * atan(deg2rad(FOV / 2))}; + info->player.plane = (t_dpoint){.x = 0, .y = 2 * atan(deg2rad(FOV / 2))}; info->player.dir = (t_dpoint){.x = -1, .y = 0}; } diff --git a/parsing/traverse_map.c b/parsing/traverse_map.c new file mode 100644 index 0000000..384e015 --- /dev/null +++ b/parsing/traverse_map.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* traverse_map.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 17:49:12 by bgoulard #+# #+# */ +/* Updated: 2024/12/01 17:54:03 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d_struct.h" +#include "cub3d_parsing.h" + +#include + +bool flood_fill(t_tile *tiles, t_ipoint pos, t_ipoint maxs) +{ + size_t i; + t_tile *current; + const t_ipoint to_check[] = { + (t_ipoint){pos.x + 1, pos.y}, (t_ipoint){pos.x - 1, pos.y}, + (t_ipoint){pos.x, pos.y + 1}, (t_ipoint){pos.x, pos.y - 1}, + (t_ipoint){pos.x + 1, pos.y + 1}, + (t_ipoint){pos.x - 1, pos.y + 1}, + (t_ipoint){pos.x + 1, pos.y - 1}, + (t_ipoint){pos.x - 1, pos.y - 1}, + }; + + if (pos.x < 0 || pos.y < 0 || pos.x >= maxs.x || pos.y >= maxs.y) + return (false); + current = c3_get_cell(tiles, maxs, pos); + if (current->tile_visited == true || current->tile_type == WALL) + return (true); + current->tile_visited = true; + i = 0; + while (i != (sizeof(to_check) / sizeof(to_check[0]))) + if (flood_fill(tiles, to_check[i++], maxs) == false) + return (false); + return (true); +} + +void *traverse_map(void *data) +{ + t_info *info; + t_ipoint pos_start; + + info = (t_info *)data; + pos_start = (t_ipoint){.x = info->player.pos.x, .y = info->player.pos.y}; + if (flood_fill(info->map.map, pos_start, info->map.size) == false) + return (info->last_error = ERROR_PARSE, NULL); + return (info); +} diff --git a/parsing/utils.c b/parsing/utils.c new file mode 100644 index 0000000..0d8a1a4 --- /dev/null +++ b/parsing/utils.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: bgoulard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/12/01 17:43:17 by bgoulard #+# #+# */ +/* Updated: 2024/12/01 17:45:58 by bgoulard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d_struct.h" + +#include "ft_string.h" + +#include +#include + +t_tile *c3_get_cell(t_tile *map, t_ipoint dimensions, t_ipoint pos) +{ + if (pos.x < 0 || pos.y < 0 || pos.x >= dimensions.x + || pos.y >= dimensions.y) + return (printf("runtime error: %s:%d (pos:%d,%d on dims:%d,%d)\n", + __func__, __LINE__, pos.x, pos.y, dimensions.x, dimensions.y), + NULL); + return (map + (pos.y * dimensions.x + pos.x)); +} + +bool is_identifier(const char *str, const char **id_str) +{ + size_t i; + + i = 0; + while (id_str[i]) + { + if (ft_strstart_with(str, id_str[i])) + return (true); + i++; + } + return (false); +} diff --git a/sources/cleanups.c b/sources/cleanups.c index cc755c6..898db8d 100644 --- a/sources/cleanups.c +++ b/sources/cleanups.c @@ -6,7 +6,7 @@ /* By: bgoulard +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/11/09 01:11:01 by bgoulard #+# #+# */ -/* Updated: 2024/11/29 17:09:01 by bgoulard ### ########.fr */ +/* Updated: 2024/12/01 17:24:54 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,19 +32,24 @@ static void cleanup_map(t_map *map) static void cleanup_mlx(t_info *info) { + size_t i; + + i = 0; if (!info->mlx_ptr) return ; if (info->win_ptr) mlx_destroy_window(info->mlx_ptr, info->win_ptr); - for (int i = 0; i < 4; i++) + while (i < sizeof(info->map.texture_) / sizeof(info->map.texture_[0])) { if (info->map.texture_[i].img) mlx_destroy_image(info->mlx_ptr, info->map.texture_[i].img); - if (info->map.texture_[i].path) - ft_free((void **)&info->map.texture_[i].path); - if (info->map.texture[i]) - mlx_destroy_image(info->mlx_ptr, info->map.texture[i]); + if (info->map.texture_[i++].path) + ft_free((void **)&info->map.texture_[i - 1].path); } + i = 0; + while (i < sizeof(info->map.texture) / sizeof(info->map.texture[0])) + if (info->map.texture[i++]) + mlx_destroy_image(info->mlx_ptr, info->map.texture[i - 1]); if (info->camera.screen_buff) mlx_destroy_image(info->mlx_ptr, info->camera.screen_buff); mlx_destroy_display(info->mlx_ptr); diff --git a/sources/main.c b/sources/main.c index d7fd13b..dcd7a21 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/30 16:30:03 by rparodi #+# #+# */ -/* Updated: 2024/11/29 17:08:31 by bgoulard ### ########.fr */ +/* Updated: 2024/12/01 17:27:34 by bgoulard ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,10 +20,10 @@ #include #include -void dump_map(t_tile *map, t_ipoint size) +void dump_map(t_tile *map, t_ipoint size) { - int i; - int j; + int i; + int j; i = 0; while (i < size.y) @@ -40,12 +40,12 @@ void dump_map(t_tile *map, t_ipoint size) } } -// not normed but we'll take care of this as a niceties at the last +// not normed but we'll take care of this as a niceties at the last // possible moment :) void dump_info(t_info *info) { - const char *bool_str[2] = {"False", "True"}; - size_t i; + const char *bool_str[2] = {"False", "True"}; + size_t i; i = 0; printf("t_info:\n"); @@ -54,7 +54,6 @@ void dump_info(t_info *info) printf("\t\tpos:\t(x:%lf, y:%lf)\n", info->player.pos.x, info->player.pos.y); printf("\t\tdir:\t(x:%lf, y:%lf)\n", info->player.dir.x, info->player.dir.y); printf("\t\tplane:\t(x:%lf, y:%lf)\n", info->player.plane.x, info->player.plane.y); - printf("\tcli_ctx:\n"); printf("\t\tfile:\t%s\n", info->cli_ctx.file); printf("\t\tdebug:\t%s\n", bool_str[info->cli_ctx.debug]); @@ -90,8 +89,8 @@ void check_err(t_info *info) return (info->last_error = ERROR_EXTENSION_FILE, (void)0); info->map.fd = open(info->cli_ctx.file, O_RDONLY); if (info->map.fd == -1) - return (info->errno_state = errno, - info->last_error = ERROR_OPEN_FILE, (void)0); + return (info->errno_state = errno, info->last_error = ERROR_OPEN_FILE, + (void)0); } void run_cub3d(t_info *info) @@ -103,11 +102,10 @@ void run_cub3d(t_info *info) dump_info(info); if (info->last_error != NO_ERROR) return ; - info->camera.screen_buff = \ - mlx_new_image(info->mlx_ptr, info->screen_size.x, info->screen_size.y); - info->camera.img_addr = \ - mlx_get_data_addr(info->camera.screen_buff, &info->camera.bpp, \ - &info->camera.line_len, &info->camera.endian); + info->camera.screen_buff = mlx_new_image(info->mlx_ptr, info->screen_size.x, + info->screen_size.y); + info->camera.img_addr = mlx_get_data_addr(info->camera.screen_buff, + &info->camera.bpp, &info->camera.line_len, &info->camera.endian); mlx_loop(info->mlx_ptr); }