diff --git a/Makefile b/Makefile index 78d9f28..9225428 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ GNL := ./lib/gnl GNL_A := ./lib/gnl/get_next_line.a HEADERS := -I ./include -I $(LIBMLX)/include -I $(LIBFT) -I $(FTPRINTF) -I $(GNL) -LIBS := $(LIBMLX)/build/libmlx42.a $(LIBFT_A) $(FT_PRINTF_A) $(GNL_A) -ldl -lglfw -pthread -lm +LIBS := $(LIBMLX)/build/libmlx42.a $(GNL_A) $(LIBFT_A) $(FT_PRINTF_A) -ldl -lglfw -pthread -lm SRCS := $(shell find ./src -iname "*.c") OBJS := ${SRCS:.c=.o} diff --git a/assets/maps/map.cub b/assets/maps/map.cub new file mode 100644 index 0000000..9ecbd48 --- /dev/null +++ b/assets/maps/map.cub @@ -0,0 +1,22 @@ +NO assets/textures/backRoom.png +SO assets/textures/backRoom.png +WE assets/textures/backRoom2.png +EA assets/textures/backRoom2.png + +C 164,157,79 +F 136,123,55 + +11111111111111111111111111111 +10000000001100000000000000001 +10110000011100000010000010001 +10010000000000000000000010001 +10110000011100000010000010001 +10000000001100000111011110001 +11110111111111011100000010001 +11110111111111011101010010001 +11000000110101011100000010001 +10000000000000001100000010001 +10000000000000001101010010001 +11000000110101011111011110N01 +11110111111101011111000000001 +11111111111111111111111111111 diff --git a/assets/textures/backRoom.png b/assets/textures/backRoom.png new file mode 100644 index 0000000..8fdc5f7 Binary files /dev/null and b/assets/textures/backRoom.png differ diff --git a/assets/textures/backRoom2.png b/assets/textures/backRoom2.png new file mode 100644 index 0000000..5070d44 Binary files /dev/null and b/assets/textures/backRoom2.png differ diff --git a/assets/textures/img_ea.png b/assets/textures/img_ea.png new file mode 100644 index 0000000..254ebfd Binary files /dev/null and b/assets/textures/img_ea.png differ diff --git a/assets/textures/img_no.png b/assets/textures/img_no.png new file mode 100644 index 0000000..5034e5f Binary files /dev/null and b/assets/textures/img_no.png differ diff --git a/assets/textures/img_so.png b/assets/textures/img_so.png new file mode 100644 index 0000000..bfa538a Binary files /dev/null and b/assets/textures/img_so.png differ diff --git a/assets/textures/img_we.png b/assets/textures/img_we.png new file mode 100644 index 0000000..cb3686f Binary files /dev/null and b/assets/textures/img_we.png differ diff --git a/includes/cub3d.h b/includes/cub3d.h index c120927..4b074d0 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: aortigos # include +# include # include +# include +# include # include "../lib/mlx/include/MLX42/MLX42.h" # include "../lib/libft/libft.h" # include "../lib/ft_printf/ft_printf.h" # include "../lib/gnl/get_next_line.h" -# define S_W 800 -# define S_H 600 -# define FOV 60 -# define PLAYER_SPEED 3 +/*═══════════════════════════ [ MACROS ] ═══════════════════════════════════*/ + +// MLX - GAME + +# define S_W 1900 // screen width +# define S_H 1000 // screen height +# define WALL_SIZE 30 // wall size +# define FOV 60 // field of view +# define ROTATION_SPEED 0.045 // rotation speed +# define PLAYER_SPEED 4 // player speed + +// ERROR + +# define ERR_INV_COP "Error: invalid argument\n" +# define ERR_INV_FILE "Error: invalid file\n" +# define ERR_EMPTY_FILE "Error: empty file\n" + +# define ERR_MAP_INV "Error: invalid map element\n" +# define ERR_MAP_EMPTY "Error: empty ligne in the map\n" +# define ERR_MAP_DUP "Error: duplicate map element\n" +# define ERR_MAP_RGB "Error: invalid color map [RGB]\n" + +/*══════════════════════════ [ STRUCTS ] ═══════════════════════════════════*/ + +typedef struct s_tex +{ + mlx_texture_t *no; + mlx_texture_t *so; + mlx_texture_t *we; + mlx_texture_t *ea; +} t_tex; + +typedef struct s_turelist +{ + char *name; + char *value; + struct s_turelist *next; +} t_turelist; + +typedef struct s_player +{ + int plyr_x; // player x position in pixels + int plyr_y; // player y position in pixels + double angle; // player angle + float fov_rd; // field of view in radians + int rot; // rotation flag + int l_r; // left right flag + int u_d; // up down flag +} t_player; + +typedef struct s_ray +{ + int index; + double ray_ngl; + double horiz_x; + double horiz_y; + double vert_x; + double vert_y; + double distance; + int flag; +} t_ray; + +typedef struct s_data +{ + int p_x; // player x position in the map + int p_y; // player y position in the map + int w_map; // map width + int h_map; // map height + int fd; + char *line; + char *ture; + char **ture2d; + char *map; + char **map2d; + char **sq_map; + char **cc; + char **ff; + t_turelist *t_list; +} t_data; + +typedef struct s_mlx +{ + mlx_image_t *img; // the image + mlx_t *mlx_ptr; // the mlx pointer + t_ray *ray; // the ray structure + t_data *dt; // the data structure + t_player *ply; // the player structure + t_tex *tex; + t_turelist *l_ture; +} t_mlx; // Functions // parsing.c -int read_map(char *av, t_data *map, int *count); -void get_x_y_player(t_data *data); -int check_extension_map(char *file); -int parsing(int ac, char **av, t_data *data); +int read_map(char *av, t_data *map, int *count); +void get_x_y_player(t_data *data); +int check_extension_map(char *file); +int parsing(int ac, char **av, t_data *data); // frees.c -void freetl(char *ture, char *line, int fd); -void free_map(t_data *data); -void free_m(t_mlx *mlx); -void freelist(t_turelist **txture); +void freetl(char *ture, char *line, int fd); +void free_map(t_data *data); +void free_m(t_mlx *mlx); +void freelist(t_turelist **txture); // lst_textures.c -int get_index(char *line, int i); -t_turelist *new_texture(char *line); -void lst_back_ture(t_turelist **l_ture, t_turelist *new); -int lst_ture(t_data *m, t_turelist **l_ture); +int get_index(char *line, int i); +t_turelist *new_texture(char *line); +void lst_back_ture(t_turelist **l_ture, t_turelist *new); +int lst_ture(t_data *m, t_turelist **l_ture); // read_map.c -int is_surrounded(char *line); -int is_validmap(char *line, int *flag); -char *getmap(t_data *map); -int read_map_(t_data *map, int count); -void process_map(t_data *map, int *count); +int is_surrounded(char *line); +int is_validmap(char *line, int *flag); +char *getmap(t_data *map); +int read_map_(t_data *map, int count); +void process_map(t_data *map, int *count); // read_map_utils.c -int check_tures_space_tab(char **ture2d, int count); -int parse_rgb(char **ture2d); -int check_dup(t_data *m); -int check_first_last_line(char **map); -int surrounded_by_one(char **map); +int check_tures_space_tab(char **ture2d, int count); +int parse_rgb(char **ture2d); +int check_dup(t_data *m); +int check_first_last_line(char **map); +int surrounded_by_one(char **map); // textures.c -int check_color_values(char **rgb); -void ft_process_rgb_color(t_turelist *tmp, t_data *m); -int color_ture(t_data *m, t_turelist *l_ture); -int check_color_textures(char *line); -int check_count_textures(t_data *m, int count); +int check_color_values(char **rgb); +void ft_process_rgb_color(t_turelist *tmp, t_data *m); +int color_ture(t_data *m, t_turelist *l_ture); +int check_color_textures(char *line); +int check_count_textures(t_data *m, int count); // textures_utils.c -int is_valid_texture(char *line); -int count_comma(char *rgb); -int check_pos_cf(char *l); -int line_around_one(char *line); -char *getlastline(char **map); +int is_valid_texture(char *line); +int count_comma(char *rgb); +int check_pos_cf(char *l); +int line_around_one(char *line); +char *getlastline(char **map); //valid_map.c -int h_map(char **map); -int v_map(char **map); -char *fixline(char *line, int maxlen); -int getsize_line(char **map); -int valid_map(t_data *m); +int h_map(char **map); +int v_map(char **map); +char *fixline(char *line, int maxlen); +int getsize_line(char **map); +int valid_map(t_data *m); // frees.c -void ft_delete_texture(t_texture *texture); -void ft_exit(t_mlx *mlx); +void ft_delete_texture(t_tex *texture); +void ft_exit(t_mlx *mlx); // movement.c -void rotate_player(t_mlx *mlx, int i); -void move_player(t_mlx *mlx, double move_x, double move_y); -void cub_hook(t_mlx *mlx, double move_x, double move_y); -void ft_reset_move(mlx_key_data_t keydata, t_mlx *mlx); -void key_press(mlx_key_data_t keydata, void *ml); +void rotate_player(t_mlx *mlx, int i); +void move_player(t_mlx *mlx, double move_x, double move_y); +void cub_hook(t_mlx *mlx, double move_x, double move_y); +void ft_reset_move(mlx_key_data_t keydata, t_mlx *mlx); +void key_press(mlx_key_data_t keydata, void *ml); + +// raycasting.c +int inter_check(float angle, float *inter, float *step, int is_horizon); +int wall_hit(float x, float y, t_mlx *mlx); +float get_h_inter(t_mlx *mlx, float angl); +float get_v_inter(t_mlx *mlx, float angl); +void cast_rays(t_mlx *mlx); + +// render.c +void draw_floor_ceiling(t_mlx *mlx, int ray, int t_pix, int b_pix); +mlx_texture_t *get_texture(t_mlx *mlx, int flag); +double get_x_o(mlx_texture_t *texture, t_mlx *mlx); +void draw_wall(t_mlx *mlx, int t_pix, int b_pix, double wall_h); +void render_wall(t_mlx *mlx, int ray); + +// render2.c +int get_rgba(int r, int g, int b, int a); +int reverse_bytes(int c); +void my_mlx_pixel_put(t_mlx *mlx, int x, int y, int color); +float nor_angle(float angle); +int unit_circle(float angle, char c); + +// execution.c +void drow_map_pixel(void *mlxl); +int check_load_ture(t_turelist *list); +int load_texture(t_tex *tex, t_turelist *l_ture); +void get_angle(t_mlx *mlx); +int execution(t_data *dt); + #endif diff --git a/lib/gnl/get_next_line.c b/lib/gnl/get_next_line.c index 968efaa..fa15662 100644 --- a/lib/gnl/get_next_line.c +++ b/lib/gnl/get_next_line.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* get_next_line.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: aortigos +#+ +:+ +#+ */ +/* By: aortigos +#+ +:+ +#+ */ +/* By: aortigos mlx_ptr, mlx->img); + mlx->img = mlx_new_image(mlx->mlx_ptr, S_W, S_H); + cub_hook(mlx, 0, 0); + cast_rays(mlx); + mlx_image_to_window(mlx->mlx_ptr, mlx->img, 0, 0); +} + +int check_load_ture(t_turelist *list) +{ + t_turelist *tmp; + mlx_texture_t *texture; + + tmp = list; + while (tmp) + { + if (tmp->name && ( + !ft_strncmp(tmp->name, "NO", 2) + || !ft_strncmp(tmp->name, "SO", 2) + || !ft_strncmp(tmp->name, "WE", 2) + || !ft_strncmp(tmp->name, "EA", 2))) + { + texture = mlx_load_png(tmp->value); + if (!texture) + return (0); + mlx_delete_texture(texture); + } + tmp = tmp->next; + } + return (1); +} + +int load_texture(t_tex *tex, t_turelist *l_ture) +{ + t_turelist *tmp; + + if (!l_ture) + return (0); + tmp = l_ture; + if (!check_load_ture(l_ture)) + return (0); + while (tmp) + { + if (!tmp->name || !tmp->value) + tmp = tmp->next; + if (!ft_strncmp(tmp->name, "NO", 2)) + tex->no = mlx_load_png(tmp->value); + else if (!ft_strncmp(tmp->name, "SO", 2)) + tex->so = mlx_load_png(tmp->value); + else if (!ft_strncmp(tmp->name, "WE", 2)) + tex->we = mlx_load_png(tmp->value); + else if (!ft_strncmp(tmp->name, "EA", 2)) + tex->ea = mlx_load_png(tmp->value); + tmp = tmp->next; + } + return (1); +} + +void get_angle(t_mlx *mlx) +{ + char c; + + c = mlx->dt->sq_map[mlx->dt->p_y][mlx->dt->p_x]; + if (c == 'N') + mlx->ply->angle = 3 * M_PI / 2; + if (c == 'S') + mlx->ply->angle = M_PI / 2; + if (c == 'E') + mlx->ply->angle = 0; + if (c == 'W') + mlx->ply->angle = M_PI; + mlx->ply->plyr_x = (mlx->dt->p_x * WALL_SIZE) + WALL_SIZE / 2; + mlx->ply->plyr_y = (mlx->dt->p_y * WALL_SIZE) + WALL_SIZE / 2; + mlx->ply->fov_rd = (FOV * M_PI / 180); +} + +int execution(t_data *dt) +{ + t_mlx mlx; + + if (S_H > 1440 || S_W > 2560 || FOV >= 180 || FOV <= 0) + return (freelist(&dt->t_list), free_map(dt), 0); + mlx.ply = (t_player *)ft_calloc(sizeof(t_player), 1); + mlx.ray = (t_ray *)ft_calloc(sizeof(t_ray), 1); + mlx.tex = (t_tex *)ft_calloc(sizeof(t_tex), 1); + mlx.dt = dt; + mlx.mlx_ptr = mlx_init(S_W, S_H, "cub3D", false); + if (!mlx.mlx_ptr) + return (ft_exit(&mlx), 0); + if (!load_texture(mlx.tex, dt->t_list)) + return (ft_exit(&mlx), 0); + get_angle(&mlx); + mlx_key_hook(mlx.mlx_ptr, &key_press, &mlx); + mlx_loop_hook(mlx.mlx_ptr, &drow_map_pixel, &mlx); + mlx_loop(mlx.mlx_ptr); + ft_exit(&mlx); + return (0); +} diff --git a/src/execution/frees.c b/src/execution/frees.c index eb3c063..d36c587 100644 --- a/src/execution/frees.c +++ b/src/execution/frees.c @@ -6,22 +6,22 @@ /* By: aortigos no) - mlx_delete_texture(texture->no); - if (texture->so) - mlx_delete_texture(texture->so); - if (texture->we) - mlx_delete_texture(texture->we); - if (texture->ea) - mlx_delete_texture(texture->ea); + if (tex->no) + mlx_delete_texture(tex->no); + if (tex->so) + mlx_delete_texture(tex->so); + if (tex->we) + mlx_delete_texture(tex->we); + if (tex->ea) + mlx_delete_texture(tex->ea); } void ft_exit(t_mlx *mlx) @@ -37,4 +37,4 @@ void ft_exit(t_mlx *mlx) mlx_terminate(mlx->mlx_ptr); ft_putstr_fd("END GAME\n", 1); exit(0); -} \ No newline at end of file +} diff --git a/src/execution/raycasting.c b/src/execution/raycasting.c new file mode 100644 index 0000000..dad1090 --- /dev/null +++ b/src/execution/raycasting.c @@ -0,0 +1,133 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* raycasting.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos 0 && angle < M_PI) + { + *inter += WALL_SIZE; + return (-1); + } + *step *= -1; + } + else + { + if (!(angle > M_PI / 2 && angle < 3 * M_PI / 2)) + { + *inter += WALL_SIZE; + return (-1); + } + *step *= -1; + } + return (1); +} + +int wall_hit(float x, float y, t_mlx *mlx) +{ + int x_m; + int y_m; + + if (x < 0 || y < 0) + return (0); + x_m = floor(x / WALL_SIZE); + y_m = floor(y / WALL_SIZE); + if ((y_m >= mlx->dt->h_map || x_m >= mlx->dt->w_map)) + return (0); + if (mlx->dt->map2d[y_m] && x_m <= (int)ft_strlen(mlx->dt->map2d[y_m])) + if (mlx->dt->map2d[y_m][x_m] == '1') + return (0); + return (1); +} + +float get_h_inter(t_mlx *mlx, float angl) +{ + float h_x; + float h_y; + float x_step; + float y_step; + int pixel; + + y_step = WALL_SIZE; + x_step = WALL_SIZE / tan(angl); + h_y = floor(mlx->ply->plyr_y / WALL_SIZE) * WALL_SIZE; + pixel = inter_check(angl, &h_y, &y_step, 1); + h_x = mlx->ply->plyr_x + (h_y - mlx->ply->plyr_y) / tan(angl); + if ((unit_circle(angl, 'y') && x_step > 0) || (!unit_circle(angl, 'y') + && x_step < 0)) + x_step *= -1; + while (wall_hit(h_x, h_y - pixel, mlx)) + { + h_x += x_step; + h_y += y_step; + } + mlx->ray->horiz_x = h_x; + mlx->ray->horiz_y = h_y; + return (sqrt(pow(h_x - mlx->ply->plyr_x, 2) + pow(h_y - mlx->ply->plyr_y, + 2))); +} + +float get_v_inter(t_mlx *mlx, float angl) +{ + float v_x; + float v_y; + float x_step; + float y_step; + int pixel; + + x_step = WALL_SIZE; + y_step = WALL_SIZE * tan(angl); + v_x = floor(mlx->ply->plyr_x / WALL_SIZE) * WALL_SIZE; + pixel = inter_check(angl, &v_x, &x_step, 0); + v_y = mlx->ply->plyr_y + (v_x - mlx->ply->plyr_x) * tan(angl); + if ((unit_circle(angl, 'x') && y_step < 0) || (!unit_circle(angl, 'x') + && y_step > 0)) + y_step *= -1; + while (wall_hit(v_x - pixel, v_y, mlx)) + { + v_x += x_step; + v_y += y_step; + } + mlx->ray->vert_x = v_x; + mlx->ray->vert_y = v_y; + return (sqrt(pow(v_x - mlx->ply->plyr_x, 2) + pow(v_y - mlx->ply->plyr_y, + 2))); +} + +void cast_rays(t_mlx *mlx) +{ + double h_inter; + double v_inter; + int ray; + + ray = 0; + mlx->ray->ray_ngl = mlx->ply->angle - (mlx->ply->fov_rd / 2); + while (ray < S_W) + { + mlx->ray->flag = 0; + h_inter = get_h_inter(mlx, nor_angle(mlx->ray->ray_ngl)); + v_inter = get_v_inter(mlx, nor_angle(mlx->ray->ray_ngl)); + if (v_inter <= h_inter) + mlx->ray->distance = v_inter; + else + { + mlx->ray->distance = h_inter; + mlx->ray->flag = 1; + } + render_wall(mlx, ray); + ray++; + mlx->ray->ray_ngl += (mlx->ply->fov_rd / S_W); + } +} diff --git a/src/execution/render.c b/src/execution/render.c new file mode 100644 index 0000000..8714d8b --- /dev/null +++ b/src/execution/render.c @@ -0,0 +1,106 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos dt->ff[0]), ft_atoi(mlx->dt->ff[1]), + ft_atoi(mlx->dt->ff[2]), 255); + while (i < S_H) + my_mlx_pixel_put(mlx, ray, i++, c); + c = get_rgba(ft_atoi(mlx->dt->cc[0]), ft_atoi(mlx->dt->cc[1]), + ft_atoi(mlx->dt->cc[2]), 255); + i = 0; + while (i < t_pix) + my_mlx_pixel_put(mlx, ray, i++, c); +} + +mlx_texture_t *get_texture(t_mlx *mlx, int flag) +{ + mlx->ray->ray_ngl = nor_angle(mlx->ray->ray_ngl); + if (flag == 0) + { + if (mlx->ray->ray_ngl > M_PI / 2 && mlx->ray->ray_ngl < 3 * (M_PI / 2)) + return (mlx->tex->ea); + else + return (mlx->tex->we); + } + else + { + if (mlx->ray->ray_ngl > 0 && mlx->ray->ray_ngl < M_PI) + return (mlx->tex->so); + else + return (mlx->tex->no); + } +} + +double get_x_o(mlx_texture_t *texture, t_mlx *mlx) +{ + double x_o; + + if (mlx->ray->flag == 1) + x_o = (int)fmodf((mlx->ray->horiz_x * (texture->width / WALL_SIZE)), + texture->width); + else + x_o = (int)fmodf((mlx->ray->vert_y * (texture->width / WALL_SIZE)), + texture->width); + return (x_o); +} + +void draw_wall(t_mlx *mlx, int t_pix, int b_pix, double wall_h) +{ + double x_o; + double y_o; + mlx_texture_t *texture; + uint32_t *arr; + double factor; + + texture = get_texture(mlx, mlx->ray->flag); + arr = (uint32_t *)texture->pixels; + factor = (double)texture->height / wall_h; + x_o = get_x_o(texture, mlx); + y_o = (t_pix - (S_H / 2) + (wall_h / 2)) * factor; + if (y_o < 0) + y_o = 0; + while (t_pix < b_pix) + { + my_mlx_pixel_put(mlx, mlx->ray->index, t_pix, reverse_bytes(arr[(int)y_o + * texture->width + (int)x_o])); + y_o += factor; + t_pix++; + } +} + +void render_wall(t_mlx *mlx, int ray) +{ + double wall_h; + double b_pix; + double t_pix; + + mlx->ray->distance *= cos(nor_angle(mlx->ray->ray_ngl - mlx->ply->angle)); + wall_h = (WALL_SIZE / mlx->ray->distance) * ((S_W / 2) + / tan(mlx->ply->fov_rd / 2)); + b_pix = (S_H / 2) + (wall_h / 2); + t_pix = (S_H / 2) - (wall_h / 2); + if (b_pix > S_H) + b_pix = S_H; + if (t_pix < 0) + t_pix = 0; + mlx->ray->index = ray; + draw_wall(mlx, t_pix, b_pix, wall_h); + draw_floor_ceiling(mlx, ray, t_pix, b_pix); +} diff --git a/src/execution/render2.c b/src/execution/render2.c new file mode 100644 index 0000000..9f8c66c --- /dev/null +++ b/src/execution/render2.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render2.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: aortigos > 8; + b |= (c & 0xFF000000) >> 24; + return (b); +} + +void my_mlx_pixel_put(t_mlx *mlx, int x, int y, int color) +{ + if (x < 0) + return ; + else if (x >= S_W) + return ; + if (y < 0) + return ; + else if (y >= S_H) + return ; + mlx_put_pixel(mlx->img, x, y, color); +} + +float nor_angle(float angle) +{ + if (angle < 0) + angle += (2 * M_PI); + if (angle > (2 * M_PI)) + angle -= (2 * M_PI); + return (angle); +} + +int unit_circle(float angle, char c) +{ + if (c == 'x') + { + if (angle > 0 && angle < M_PI) + return (1); + } + else if (c == 'y') + { + if (angle > (M_PI / 2) && angle < (3 * M_PI) / 2) + return (1); + } + return (0); +} diff --git a/src/main.c b/src/main.c index 0caca49..e94d252 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: aortigos = 0) + close(fd); +} + +void free_map(t_data *data) +{ + free2d(data->sq_map); + free2d(data->map2d); + free2d(data->ture2d); + if (data->ff) + free2d(data->ff); + if (data->cc) + free2d(data->cc); +} + +void free_m(t_mlx *mlx) +{ + if (mlx->dt->sq_map) + free2d(mlx->dt->sq_map); + if (mlx->dt->map2d) + free2d(mlx->dt->map2d); + if (mlx->dt->ture2d) + free2d(mlx->dt->ture2d); + if (mlx->dt->ff) + free2d(mlx->dt->ff); + if (mlx->dt->cc) + free2d(mlx->dt->cc); +} + +void freelist(t_turelist **txture) +{ + t_turelist *tmp; + + tmp = *txture; + while (tmp) { - if (ture) - ft_memfree(ture); - if (line) - ft_memfree(line); - if (fd >= 0) - close(fd); - } - - void free_map(t_data *data) - { - free2d(data->sq_map); - free2d(data->map2d); - free2d(data->ture2d); - if (data->ff) - free2d(data->ff); - if (data->cc) - free2d(data->cc); - } - - void free_m(t_mlx *mlx) - { - if (mlx->dt->sq_map) - free2d(mlx->dt->sq_map); - if(mlx->dt->map2d) - free2d(mlx->dt->map2d); - if (mlx->dt->ture2d) - free2d(mlx->dt->ture2d); - if (mlx->dt->ff) - free2d(mlx->dt->ff); - if (mlx->dt->cc) - free2d(mlx->dt->cc); - } - - void freelist(t_turelist **txture) - { - t_turelist *tmp; - + *txture = tmp->next; + ft_memfree(tmp->name); + ft_memfree(tmp->value); + ft_memfree(tmp); tmp = *txture; - while (tmp) - { - *txture = tmp->next; - ft_memfree(tmp->name); - ft_memfree(tmp->value); - ft_memfree(tmp); - tmp = *txture; - } - ft_memfree(*txture); } + ft_memfree(*txture); +} diff --git a/src/parsing/lst_textures.c b/src/parsing/lst_textures.c index 570e78a..d9377bf 100644 --- a/src/parsing/lst_textures.c +++ b/src/parsing/lst_textures.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* textures.c :+: :+: :+: */ +/* lst_textures.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: aortigos name = ft_substr(line, 0, 2); list->value = ft_substr(line, get_index(line, 2), ft_strlen(line)); - } + } else if ((!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1))) { list->name = ft_substr(line, 0, 1); diff --git a/src/parsing/parsing.c b/src/parsing/parsing.c index e40d1e5..a5ee7c3 100644 --- a/src/parsing/parsing.c +++ b/src/parsing/parsing.c @@ -6,7 +6,7 @@ /* By: aortigos fd = open(av, O_RDONLY); - if (map->fd == -1) - return (ft_putstr_fd(ERR_INV_FILE, 2), 0); - map->line = get_next_line(map->fd); - if (map->line == NULL) - return (ft_putstr_fd(ERR_EMPTY_FILE, 2), 0); - map->ture = ft_strdup(""); - process_map(map, count); - if (!check_count_textures(map, *count)) - return (freetl(map->ture, map->line, map->fd), 0); - map->ture2d = ft_split(map->ture, '\n'); - if (!map->ture2d) - return (freetl(map->ture, map->line, map->fd), 0); - if (!read_map_(map, *count)) - return (freetl(map->ture, map->line, map->fd), free2d(map->ture2d), 0); - return (freetl(map->ture, map->line, map->fd), 1); + map->fd = open(av, O_RDONLY); + if (map->fd == -1) + return (ft_putstr_fd(ERR_INV_FILE, 2), 0); + map->line = get_next_line(map->fd); + if (map->line == NULL) + return (ft_putstr_fd(ERR_EMPTY_FILE, 2), 0); + map->ture = ft_strdup(""); + process_map(map, count); + if (!check_count_textures(map, *count)) + return (freetl(map->ture, map->line, map->fd), 0); + map->ture2d = ft_split(map->ture, '\n'); + if (!map->ture2d) + return (freetl(map->ture, map->line, map->fd), 0); + if (!read_map_(map, *count)) + return (freetl(map->ture, map->line, map->fd), free2d(map->ture2d), 0); + return (freetl(map->ture, map->line, map->fd), 1); } void get_x_y_player(t_data *data) @@ -82,4 +82,4 @@ int parsing(int ac, char **av, t_data *data) return (free_map(data), freelist(&data->t_list), 0); get_x_y_player(data); return (1); -} \ No newline at end of file +} diff --git a/src/parsing/read_map.c b/src/parsing/read_map.c index 53968fa..3e4b26a 100644 --- a/src/parsing/read_map.c +++ b/src/parsing/read_map.c @@ -6,7 +6,7 @@ /* By: aortigos line) { if (map->line[0] == '\n') - return (ft_putstr_fd(ERR_MAP_EMPTY, 2), freetl(map->map, map->line, - -1), NULL); + return (ft_putstr_fd(ERR_MAP_EMPTY, 2), + freetl(map->map, map->line, -1), NULL); tmp = ft_strjoin(map->map, map->line); ft_memfree(map->map); ft_memfree(map->line); diff --git a/src/parsing/read_map_utils.c b/src/parsing/read_map_utils.c index c5a9f9c..f00b9c9 100644 --- a/src/parsing/read_map_utils.c +++ b/src/parsing/read_map_utils.c @@ -6,7 +6,7 @@ /* By: aortigos 1) + if (!is_surrounded(map[i]) || !is_validmap(map[i], &flag) || flag > 1) return (ft_putstr_fd("one X\n", 2), 0); if (flag == 0) return (ft_putstr_fd("no position map\n", 2), 0); return (1); -} \ No newline at end of file +} diff --git a/src/parsing/textures.c b/src/parsing/textures.c index 50cec17..8094b41 100644 --- a/src/parsing/textures.c +++ b/src/parsing/textures.c @@ -6,7 +6,7 @@ /* By: aortigos 255 || ft_atoi(rgb[i]) < 0) + if (ft_atoi(rgb[i]) > 255 || ft_atoi(rgb[i]) < 0) return (free2d(rgb), 0); return (free2d(rgb), 1); } void ft_process_rgb_color(t_turelist *tmp, t_data *m) { - if (!f_strncmp(tmp->name, "F", 2)) + if (!ft_strncmp(tmp->name, "F", 2)) m->ff = ft_split(tmp->value, ','); - else if(!ft_strncmp(tmp->name, "C", 2)) + else if (!ft_strncmp(tmp->name, "C", 2)) m->cc = ft_split(tmp->value, ','); return ; } @@ -35,15 +35,18 @@ void ft_process_rgb_color(t_turelist *tmp, t_data *m) int color_ture(t_data *m, t_turelist *l_ture) { t_turelist *tmp; + char **colors; - m->cc = NULL; + m->cc = NULL; m->ff = NULL; tmp = l_ture; while (tmp) { - if (!ft_strncmp(tmp->name, "F", 1) || !ft_strncmp(tmp->name, "C", 1)) + if (tmp->name && (!ft_strncmp(tmp->name, "F", 1) + || !ft_strncmp(tmp->name, "C", 1))) { - if (!check_color_values(ft_split(tmp->value, ','))) + colors = ft_split(tmp->value, ','); + if (!check_color_values(colors)) return (ft_putstr_fd(ERR_MAP_RGB, 2), 0); ft_process_rgb_color(tmp, m); } @@ -56,15 +59,15 @@ int check_color_textures(char *line) { while (ft_isspace(*line)) line++; - return ((!ft_strncmp(line, "EA", 2) || !ft_strncmp(line, "NO", 2) + return ((!ft_strncmp(line, "EA", 2) || !ft_strncmp(line, "NO", 2) || !ft_strncmp(line, "SO", 2) || !ft_strncmp(line, "WE", 2)) - || (!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1))); + || (!ft_strncmp(line, "F", 1) || !ft_strncmp(line, "C", 1))); } int check_count_textures(t_data *m, int count) { (void)m; - if (count != 6) + if (count != 6) return (ft_putstr_fd(ERR_MAP_INV, 2), 0); return (1); } diff --git a/src/parsing/textures_utils.c b/src/parsing/textures_utils.c index 691ce99..ffb63eb 100644 --- a/src/parsing/textures_utils.c +++ b/src/parsing/textures_utils.c @@ -6,11 +6,11 @@ /* By: aortigos map2d); - m->sq_map = ft_calloc(sizeof(char *), (ft_arraylen(m->map2d) + 1)); + maxlen = getsize_line(m->map2d); + m->sq_map = ft_calloc(sizeof(char *), (ft_arraylen(m->map2d) + 1)); if (!m->sq_map) return (0); i = -1; while (m->map2d[++i]) { - if (maxlen == (int)ft_strlen(m->map2d[i])) + if (maxlen == (int)ft_strlen(m->map2d[i])) m->sq_map[i] = ft_strdup(m->map2d[i]); else m->sq_map[i] = fixline(m->map2d[i], maxlen); } m->h_map = ft_arraylen(m->sq_map); m->w_map = ft_strlen(m->sq_map[0]); - if (!h_map(m->sq_map) || !v_map(m->sq_map)) + if (!h_map(m->sq_map) || !v_map(m->sq_map)) return (free2d(m->sq_map), free2d(m->map2d), free2d(m->ture2d), 0); return (1); -} \ No newline at end of file +}