derversion

This commit is contained in:
Ladebeze66 2024-01-30 14:02:57 +01:00
parent 7d67dfd452
commit bb9c6a0f59
8 changed files with 135 additions and 67 deletions

View File

@ -6,7 +6,7 @@
# By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ # # By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/01/14 16:54:48 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_find_player_utils.c \
./parsing/ft_map_check_utils.c \ ./parsing/ft_map_check_utils.c \
./parsing/ft_text_and_col_utils.c \ ./parsing/ft_text_and_col_utils.c \
./utils/ft_utils_mem.c \
OBJ = $(SRC:.c=.o) OBJ = $(SRC:.c=.o)

14
cub3d.h
View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 16:56:52 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 <assert.h> # include <assert.h>
# include "include/mlx.h" # include "include/mlx.h"
# ifndef BUFFER_SIZE
# define BUFFER_SIZE 100000 # define BUFFER_SIZE 100000
# endif
# define PI 3.14159265359 # define PI 3.14159265359
# define P2 1.57079632679 # define P2 1.57079632679
# define P3 4.71238898038 # define P3 4.71238898038
@ -362,7 +359,7 @@ typedef struct s_draw_params
int texturewidth; int texturewidth;
int textureheight; int textureheight;
int texturex; int texturex;
int textureY; int texture_y;
int a; int a;
int b; int b;
int c; int c;
@ -394,7 +391,7 @@ typedef struct s_point
typedef struct s_map_check typedef struct s_map_check
{ {
char *map; char *map;
int maxWidth; int max_width;
int height; int height;
} t_map_check; } 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); bool check_for_player(char current_char, bool *found_player);
void update_player_info(t_structure_map *map_info, void update_player_info(t_structure_map *map_info,
int line_number, int column_number, char player_direction); 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); bool check_for_multiple_players(bool found_player);
int check_boundaries(t_point p, t_map_check *map_check); 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); 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 #endif

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 17:00:21 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) while (y < rparams->line_off + rparams->line_h)
{ {
perspectivefactor = (float)(y - rparams->line_off) / rparams->line_h; perspectivefactor = (float)(y - rparams->line_off) / rparams->line_h;
dparams->textureY = perspectivefactor * dparams->textureheight; dparams->texture_y = perspectivefactor * dparams->textureheight;
if (dparams->textureY >= dparams->textureheight) if (dparams->texture_y >= dparams->textureheight)
dparams->textureY = dparams->textureheight - 1; dparams->texture_y = dparams->textureheight - 1;
set_texture_coords(tparams, dparams); set_texture_coords(tparams, dparams);
color = get_texture_color(tparams->w, color = get_texture_color(tparams->w,
tparams->wall_dir, dparams->texturex, y); tparams->wall_dir, dparams->texturex, y);

View File

@ -6,22 +6,22 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 17:22:58 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 #ifndef MLX_H
# define 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); 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_clear_window(void *mlx_ptr, void *win_ptr);
int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color); 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, 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 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_get_color_value(void *mlx_ptr, int color);
int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param); int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param);
int mlx_key_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 (void *mlx_ptr);
int mlx_loop_end (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, 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_set_font(void *mlx_ptr, void *win_ptr, char *name);
void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data, 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, void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename,
int *width, int *height); int *width, int *height);
int mlx_destroy_window(void *mlx_ptr, void *win_ptr); int mlx_destroy_window(void *mlx_ptr, void *win_ptr);
int mlx_destroy_image(void *mlx_ptr, void *img_ptr); int mlx_destroy_image(void *mlx_ptr, void *img_ptr);
int mlx_destroy_display(void *mlx_ptr); int mlx_destroy_display(void *mlx_ptr);
int mlx_hook(void *win_ptr, int x_event, int x_mask, 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_autorepeatoff(void *mlx_ptr);
int mlx_do_key_autorepeaton(void *mlx_ptr); int mlx_do_key_autorepeaton(void *mlx_ptr);
int mlx_do_sync(void *mlx_ptr); int mlx_do_sync(void *mlx_ptr);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/11 14:49:45 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, static int flood_fill_and_check(t_map_check *map_check,
char *visitedMap, t_point p, char replacement) 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) || p.y < 0 || p.y >= map_check->height)
return (0); return (0);
if (visitedMap[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->maxWidth + p.x] == '1') || map_check->map[p.y * map_check->max_width + p.x] == '1')
return (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)) && !is_space_surrounded_by_walls(map_check, p))
return (0); return (0);
visitedMap[p.y * map_check->maxWidth + p.x] = 1; visitedMap[p.y * map_check->max_width + p.x] = 1;
map_check->map[p.y * map_check->maxWidth + p.x] = replacement; map_check->map[p.y * map_check->max_width + p.x] = replacement;
return (flood_fill_and_check(map_check, visitedMap, return (flood_fill_and_check(map_check, visitedMap,
(t_point){p.x + 1, p.y}, replacement) (t_point){p.x + 1, p.y}, replacement)
&& flood_fill_and_check(map_check, visitedMap, && 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) while (y < map_check->height)
{ {
x = 0; x = 0;
while (x < map_check->maxWidth) while (x < map_check->max_width)
{ {
if ((map_check->map[y * map_check->maxWidth + x] == '0' if ((map_check->map[y * map_check->max_width + x] == '0'
|| map_check->map[y * map_check->maxWidth + x] == '2') || map_check->map[y * map_check->max_width + x] == '2')
&& !is_space_surrounded_by_walls(map_check, (t_point){x, y})) && !is_space_surrounded_by_walls(map_check, (t_point){x, y}))
{ {
startPoint->x = x; startPoint->x = x;
@ -66,7 +66,7 @@ static int initialize_maps(char **copy_map, char **visited_map,
int maxWidth, int height) int maxWidth, int height)
{ {
*copy_map = malloc(maxWidth * 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)) if (!(*copy_map) || !(*visited_map))
{ {
free(*copy_map); free(*copy_map);
@ -92,7 +92,7 @@ int is_map_closed(char *map, int maxWidth, int height)
t_point start_point; t_point start_point;
int is_closed; int is_closed;
map_check.maxWidth = maxWidth; map_check.max_width = maxWidth;
map_check.height = height; map_check.height = height;
if (!initialize_maps(&copy_map, &visited_map, maxWidth, height)) if (!initialize_maps(&copy_map, &visited_map, maxWidth, height))
return (0); return (0);

View File

@ -6,7 +6,7 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/29 19:44:27 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) 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); && 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)) if (!check_boundaries(p, map_check))
return (0); return (0);
return (map_check->map[p.y * map_check->maxWidth + (p.x - 1)] == '1' return (map_check->map[p.y * map_check->max_width + (p.x - 1)] == '1'
&& map_check->map[p.y * map_check->maxWidth + (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->maxWidth + p.x] == '1' && map_check->map[(p.y - 1) * map_check->max_width + p.x] == '1'
&& map_check->map[(p.y + 1) * map_check->maxWidth + p.x] == '1'); && map_check->map[(p.y + 1) * map_check->max_width + p.x] == '1');
} }

View File

@ -6,52 +6,56 @@
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */ /* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/16 16:19:13 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" #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) bool handle_map(int fd, char **map_buffer, int *map_length)
{ {
char *line; char buffer[1024];
size_t len; ssize_t bytes_read;
ssize_t read;
bool start_copying; bool start_copying;
FILE *stream;
stream = fdopen(fd, "r");
line = NULL;
len = 0;
start_copying = false; start_copying = false;
*map_length = 0; *map_length = 0;
*map_buffer = NULL; *map_buffer = NULL;
if (!stream) while (read_map_line(fd, buffer, &bytes_read))
{ {
perror("Error converting file descriptor to FILE *"); if (!start_copying && (ft_strchr(buffer, '1')
return (false); || ft_strchr(buffer, '0')))
}
while ((read = getline(&line, &len, stream)) != -1)
{
if (!start_copying && (ft_strchr(line, '1') || ft_strchr(line, '0')))
start_copying = true; start_copying = true;
if (start_copying) if (start_copying)
{ if (!process_line(map_buffer, map_length, buffer))
*map_buffer = realloc(*map_buffer, *map_length + read + 1);
if (!*map_buffer)
{
perror("Error reallocating memory for map buffer");
free(line);
fclose(stream);
return (false); 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); return (*map_buffer != NULL);
} }

63
utils/ft_utils_mem.c Normal file
View File

@ -0,0 +1,63 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utils_mem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgras-ca <fgras-ca@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}