diff --git a/config.c b/config.c index 1cf13a9..f95c17f 100644 --- a/config.c +++ b/config.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/14 01:41:17 by yantoine #+# #+# */ -/* Updated: 2025/03/07 16:10:31 by yantoine ### ########.fr */ +/* Updated: 2025/03/17 16:03:35 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -34,39 +34,58 @@ static inline t_scene parsing_line(char *line, t_scene scene) return (scene); } +static t_calc init_calc(void) +{ + t_calc init; + + init.count = 0; + init.line = NULL; + init.data = ft_calloc(1, 1); + return (init); +} + static char **get_all_file(int fd) { - char *join; - char *line; - char *data; - char **splited; - int count; + t_calc calc; - count = 0; - line = NULL; - data = ft_calloc(1, 1); + calc = init_calc(); while (1) { - if (count > MAX_SPHERES + MAX_PLANES + MAX_CYLINDERS + MAX_LIGHTS + MAX_AMBIENT + MAX_CAMERA) + if (calc.count > MAX_SPHERES + MAX_PLANES \ + + MAX_CYLINDERS + MAX_LIGHTS + MAX_AMBIENT + MAX_CAMERA) { - free(join); + free(calc.join); ft_putstr_fd("Erreur:\n max element reached\n", 2); close(fd); exit(1); } - if (count != 0) - data = join; - line = get_next_line(fd); - if (!line) + if (calc.count != 0) + calc.data = calc.join; + calc.line = get_next_line(fd); + if (!calc.line) break ; - join = ft_strjoin(data, line); - free(line); - free(data); - count++; + calc.join = ft_strjoin(calc.data, calc.line); + free(calc.line); + free(calc.data); + calc.count++; } - splited = ft_split(join, '\n'); - free(data); - return (splited); + calc.splited = ft_split(calc.join, '\n'); + return (free(calc.data), calc.splited); +} + +static t_scene initialisation(int fd, const char *filename) +{ + t_scene scene; + + scene = create_scene(); + if (fd <= 0 || !have_extension(filename)) + { + printf("Erreur : impossible d'ouvrir %s\n", filename); + scene.no = 1; + return (scene); + } + scene.fd_if_exit = fd; + return (scene); } // ----- Parsing du fichier de configuration ----- @@ -77,14 +96,10 @@ t_scene load_config(const char *filename) char *line; t_scene scene; - scene = create_scene(); fd = open(filename, O_RDONLY); - if (fd <= 0 || !have_extension(filename)) - { - printf("Erreur : impossible d'ouvrir %s\n", filename); + scene = initialisation(fd, filename); + if (scene.no == 1) return (scene); - } - scene.fd_if_exit = fd; scene.all_file = get_all_file(fd); i = -1; while (scene.all_file[++i]) @@ -95,11 +110,10 @@ t_scene load_config(const char *filename) if (!check_nb_element(scene)) { ft_free_array(scene.all_file); - ft_putstr_fd("Error\n Bad number element\n", 2); + ft_putstr_fd("Error\n Bad number element\n", 1); exit(1); } scene = parsing_line(line, scene); } - ft_free_array(scene.all_file); - return (scene); + return (ft_free_array(scene.all_file), scene); } diff --git a/main.c b/main.c index 775addc..fc18a27 100644 --- a/main.c +++ b/main.c @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/17 19:54:03 by yantoine #+# #+# */ -/* Updated: 2025/03/05 14:14:09 by yantoine ### ########.fr */ +/* Updated: 2025/03/17 16:53:05 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,7 +71,10 @@ int main(int argc, char **argv) if (!check_number(app.scene) || \ app.scene.num_camera == 0 \ || init_mlx_and_image(&app)) + { + ft_putstr_fd("Erreur: \n", 2); return (1); + } update_camera(&app); render_scene(&app); mlx_hook(app.win, 2, 1L << 0, key_press, &app); diff --git a/miniRT.h b/miniRT.h index 13f40c9..17df3dc 100644 --- a/miniRT.h +++ b/miniRT.h @@ -6,7 +6,7 @@ /* By: yantoine +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/13 20:02:36 by yantoine #+# #+# */ -/* Updated: 2025/03/05 16:05:28 by yantoine ### ########.fr */ +/* Updated: 2025/03/17 16:04:57 by yantoine ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,6 +125,7 @@ typedef struct s_scene t_camera camera; t_ray ray; int problem; + int no; int num_spheres; int num_planes; int num_cylinders; @@ -164,6 +165,10 @@ typedef struct s_app // ----- Calcul typedef struct s_calc { + char *join; + char *line; + char *data; + char **splited; t_vec3 oc; float a; float b; @@ -206,6 +211,7 @@ typedef struct s_calc t_vec3 view_dir; t_vec3 half_dir; float spec; + int count; } t_calc;