diff --git a/Makefile b/Makefile index a90e203..ac51997 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: fgras-ca +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/01/14 16:54:48 by fgras-ca #+# #+# # -# Updated: 2024/01/29 20:12:12 by fgras-ca ### ########.fr # +# Updated: 2024/01/30 12:40:15 by fgras-ca ### ########.fr # # # # **************************************************************************** # @@ -58,6 +58,7 @@ SRC = main.c \ ./parsing/ft_find_player_utils.c \ ./parsing/ft_map_check_utils.c \ ./parsing/ft_text_and_col_utils.c \ + ./utils/ft_utils_mem.c \ OBJ = $(SRC:.c=.o) diff --git a/cub3d.h b/cub3d.h index 574e70f..50fed06 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/14 16:56:52 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 20:09:43 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 13:57:20 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,10 +23,7 @@ # include # include "include/mlx.h" -# ifndef BUFFER_SIZE # define BUFFER_SIZE 100000 -# endif - # define PI 3.14159265359 # define P2 1.57079632679 # define P3 4.71238898038 @@ -362,7 +359,7 @@ typedef struct s_draw_params int texturewidth; int textureheight; int texturex; - int textureY; + int texture_y; int a; int b; int c; @@ -394,7 +391,7 @@ typedef struct s_point typedef struct s_map_check { char *map; - int maxWidth; + int max_width; int height; } t_map_check; @@ -469,9 +466,12 @@ void initialize_variables(int *i, int *line_number, bool check_for_player(char current_char, bool *found_player); void update_player_info(t_structure_map *map_info, int line_number, int column_number, char player_direction); -void update_position(char current_char, int *line_number, int *column_number); +void update_position(char current_char, + int *line_number, int *column_number); bool check_for_multiple_players(bool found_player); int check_boundaries(t_point p, t_map_check *map_check); int is_space_surrounded_by_walls(t_map_check *map_check, t_point p); +void *ft_calloc(size_t nmemb, size_t size); +void *ft_realloc(void *ptr, size_t size); #endif diff --git a/draw/ft_3d_view_utils.c b/draw/ft_3d_view_utils.c index 0b7a378..1467a5b 100644 --- a/draw/ft_3d_view_utils.c +++ b/draw/ft_3d_view_utils.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 17:00:21 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 17:05:55 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 13:49:33 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,9 +44,9 @@ static void draw_texture_line(t_ray_params *rparams, while (y < rparams->line_off + rparams->line_h) { perspectivefactor = (float)(y - rparams->line_off) / rparams->line_h; - dparams->textureY = perspectivefactor * dparams->textureheight; - if (dparams->textureY >= dparams->textureheight) - dparams->textureY = dparams->textureheight - 1; + dparams->texture_y = perspectivefactor * dparams->textureheight; + if (dparams->texture_y >= dparams->textureheight) + dparams->texture_y = dparams->textureheight - 1; set_texture_coords(tparams, dparams); color = get_texture_color(tparams->w, tparams->wall_dir, dparams->texturex, y); diff --git a/include/mlx.h b/include/mlx.h index fc751cd..8733e0d 100644 --- a/include/mlx.h +++ b/include/mlx.h @@ -6,22 +6,22 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 17:22:58 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 17:28:02 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 14:00:04 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef MLX_H # define MLX_H -void *mlx_init(); +void *mlx_init(void); void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title); int mlx_clear_window(void *mlx_ptr, void *win_ptr); int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color); -void *mlx_new_image(void *mlx_ptr,int width,int height); +void *mlx_new_image(void *mlx_ptr, int width, int height); char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel, - int *size_line, int *endian); + int *size_line, int *endian); int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr, - int x, int y); + int x, int y); int mlx_get_color_value(void *mlx_ptr, int color); int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param); int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param); @@ -30,17 +30,17 @@ int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param); int mlx_loop (void *mlx_ptr); int mlx_loop_end (void *mlx_ptr); int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color, - char *string); + char *string); void mlx_set_font(void *mlx_ptr, void *win_ptr, char *name); void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data, - int *width, int *height); + int *width, int *height); void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename, int *width, int *height); int mlx_destroy_window(void *mlx_ptr, void *win_ptr); int mlx_destroy_image(void *mlx_ptr, void *img_ptr); int mlx_destroy_display(void *mlx_ptr); int mlx_hook(void *win_ptr, int x_event, int x_mask, - int (*funct)(), void *param); + int (*funct)(), void *param); int mlx_do_key_autorepeatoff(void *mlx_ptr); int mlx_do_key_autorepeaton(void *mlx_ptr); int mlx_do_sync(void *mlx_ptr); diff --git a/parsing/ft_map_check.c b/parsing/ft_map_check.c index d4b428d..926f602 100644 --- a/parsing/ft_map_check.c +++ b/parsing/ft_map_check.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 14:49:45 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 19:46:50 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 13:57:02 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,17 +15,17 @@ static int flood_fill_and_check(t_map_check *map_check, char *visitedMap, t_point p, char replacement) { - if (p.x < 0 || p.x >= map_check->maxWidth + if (p.x < 0 || p.x >= map_check->max_width || p.y < 0 || p.y >= map_check->height) return (0); - if (visitedMap[p.y * map_check->maxWidth + p.x] == 1 - || map_check->map[p.y * map_check->maxWidth + p.x] == '1') + if (visitedMap[p.y * map_check->max_width + p.x] == 1 + || map_check->map[p.y * map_check->max_width + p.x] == '1') return (1); - if (map_check->map[p.y * map_check->maxWidth + p.x] == ' ' + if (map_check->map[p.y * map_check->max_width + p.x] == ' ' && !is_space_surrounded_by_walls(map_check, p)) return (0); - visitedMap[p.y * map_check->maxWidth + p.x] = 1; - map_check->map[p.y * map_check->maxWidth + p.x] = replacement; + visitedMap[p.y * map_check->max_width + p.x] = 1; + map_check->map[p.y * map_check->max_width + p.x] = replacement; return (flood_fill_and_check(map_check, visitedMap, (t_point){p.x + 1, p.y}, replacement) && flood_fill_and_check(map_check, visitedMap, @@ -45,10 +45,10 @@ static int find_start_point(t_map_check *map_check, t_point *startPoint) while (y < map_check->height) { x = 0; - while (x < map_check->maxWidth) + while (x < map_check->max_width) { - if ((map_check->map[y * map_check->maxWidth + x] == '0' - || map_check->map[y * map_check->maxWidth + x] == '2') + if ((map_check->map[y * map_check->max_width + x] == '0' + || map_check->map[y * map_check->max_width + x] == '2') && !is_space_surrounded_by_walls(map_check, (t_point){x, y})) { startPoint->x = x; @@ -66,7 +66,7 @@ static int initialize_maps(char **copy_map, char **visited_map, int maxWidth, int height) { *copy_map = malloc(maxWidth * height); - *visited_map = calloc(maxWidth * height, sizeof(char)); + *visited_map = ft_calloc(maxWidth * height, sizeof(char)); if (!(*copy_map) || !(*visited_map)) { free(*copy_map); @@ -92,7 +92,7 @@ int is_map_closed(char *map, int maxWidth, int height) t_point start_point; int is_closed; - map_check.maxWidth = maxWidth; + map_check.max_width = maxWidth; map_check.height = height; if (!initialize_maps(©_map, &visited_map, maxWidth, height)) return (0); diff --git a/parsing/ft_map_check_utils.c b/parsing/ft_map_check_utils.c index 20e34ad..0fe1e7a 100644 --- a/parsing/ft_map_check_utils.c +++ b/parsing/ft_map_check_utils.c @@ -6,7 +6,7 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/29 19:44:27 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 19:48:01 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 13:57:02 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int check_boundaries(t_point p, t_map_check *map_check) { - return (p.x > 0 && p.x < map_check->maxWidth - 1 + return (p.x > 0 && p.x < map_check->max_width - 1 && p.y > 0 && p.y < map_check->height - 1); } @@ -22,8 +22,8 @@ int is_space_surrounded_by_walls(t_map_check *map_check, t_point p) { if (!check_boundaries(p, map_check)) return (0); - return (map_check->map[p.y * map_check->maxWidth + (p.x - 1)] == '1' - && map_check->map[p.y * map_check->maxWidth + (p.x + 1)] == '1' - && map_check->map[(p.y - 1) * map_check->maxWidth + p.x] == '1' - && map_check->map[(p.y + 1) * map_check->maxWidth + p.x] == '1'); + return (map_check->map[p.y * map_check->max_width + (p.x - 1)] == '1' + && map_check->map[p.y * map_check->max_width + (p.x + 1)] == '1' + && map_check->map[(p.y - 1) * map_check->max_width + p.x] == '1' + && map_check->map[(p.y + 1) * map_check->max_width + p.x] == '1'); } diff --git a/parsing/ft_textures_and_colors.c b/parsing/ft_textures_and_colors.c index 3ad8a07..4caad8e 100644 --- a/parsing/ft_textures_and_colors.c +++ b/parsing/ft_textures_and_colors.c @@ -6,52 +6,56 @@ /* By: fgras-ca +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/16 16:19:13 by fgras-ca #+# #+# */ -/* Updated: 2024/01/29 20:17:57 by fgras-ca ### ########.fr */ +/* Updated: 2024/01/30 13:55:56 by fgras-ca ### ########.fr */ /* */ /* ************************************************************************** */ #include "../cub3d.h" +bool read_map_line(int fd, char *buffer, ssize_t *bytes_read) +{ + *bytes_read = read(fd, buffer, 1023); + if (*bytes_read <= 0) + return (false); + buffer[*bytes_read] = '\0'; + return (true); +} + +bool process_line(char **map_buffer, int *map_length, char *line) +{ + ssize_t bytes_read; + + bytes_read = ft_strlen(line); + *map_buffer = ft_realloc(*map_buffer, *map_length + bytes_read + 1); + if (!*map_buffer) + { + perror("Error reallocating memory for map buffer"); + return (false); + } + ft_memcpy(*map_buffer + *map_length, line, bytes_read); + *map_length += bytes_read; + (*map_buffer)[*map_length] = '\0'; + return (true); +} + bool handle_map(int fd, char **map_buffer, int *map_length) { - char *line; - size_t len; - ssize_t read; + char buffer[1024]; + ssize_t bytes_read; bool start_copying; - FILE *stream; - stream = fdopen(fd, "r"); - line = NULL; - len = 0; start_copying = false; *map_length = 0; *map_buffer = NULL; - if (!stream) + while (read_map_line(fd, buffer, &bytes_read)) { - perror("Error converting file descriptor to FILE *"); - return (false); - } - while ((read = getline(&line, &len, stream)) != -1) - { - if (!start_copying && (ft_strchr(line, '1') || ft_strchr(line, '0'))) + if (!start_copying && (ft_strchr(buffer, '1') + || ft_strchr(buffer, '0'))) start_copying = true; if (start_copying) - { - *map_buffer = realloc(*map_buffer, *map_length + read + 1); - if (!*map_buffer) - { - perror("Error reallocating memory for map buffer"); - free(line); - fclose(stream); + if (!process_line(map_buffer, map_length, buffer)) return (false); - } - ft_memcpy(*map_buffer + *map_length, line, read); - *map_length += read; - (*map_buffer)[*map_length] = '\0'; - } } - free(line); - fclose(stream); return (*map_buffer != NULL); } diff --git a/utils/ft_utils_mem.c b/utils/ft_utils_mem.c new file mode 100644 index 0000000..9dcacf4 --- /dev/null +++ b/utils/ft_utils_mem.c @@ -0,0 +1,63 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_utils_mem.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: fgras-ca +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/01/30 12:33:22 by fgras-ca #+# #+# */ +/* Updated: 2024/01/30 13:58:28 by fgras-ca ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d.h" + +void ft_bzero(void *ptr, size_t len) +{ + size_t i; + + i = 0; + while (i < len) + { + *(char *)(ptr + i) = 0; + i++; + } +} + +void *ft_calloc(size_t nmemb, size_t size) +{ + void *ptr; + + if (!nmemb || !size) + return (malloc(0)); + if ((nmemb * size) / size != nmemb) + return (NULL); + ptr = malloc(size * nmemb); + if (!ptr) + return (0); + ft_bzero(ptr, size * nmemb); + return (ptr); +} + +void *ft_realloc(void *ptr, size_t size) +{ + void *new_ptr; + + if (ptr == NULL) + { + return (malloc(size)); + } + if (size == 0) + { + free(ptr); + return (NULL); + } + new_ptr = malloc(size); + if (new_ptr == NULL) + { + return (NULL); + } + ft_memcpy(new_ptr, ptr, size); + free(ptr); + return (new_ptr); +}