From 1b3cf4e3ce43e556ef47c669511620815b747221 Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Tue, 28 Jan 2020 16:01:25 +0100
Subject: I understood

---
 Makefile            | 10 ++++----
 inc/cub3d.h         | 13 ++++++-----
 src/ft_exit.c       |  8 ++++---
 src/ft_free_words.c |  3 +--
 src/ft_get_res.c    | 20 +++++-----------
 src/ft_init_lists.c |  5 +++-
 src/ft_init_winlx.c |  1 +
 src/ft_map_error.c  |  5 ++--
 src/ft_parse_map.c  | 67 +++++++++++++++++++++++++++--------------------------
 src/ft_print_list.c |  2 +-
 src/ft_select_get.c | 26 +++++++++++++++++++--
 11 files changed, 91 insertions(+), 69 deletions(-)

diff --git a/Makefile b/Makefile
index 59dd069..1321e35 100644
--- a/Makefile
+++ b/Makefile
@@ -24,11 +24,11 @@ SRCS_NAME	+= ft_drawsquare.c
 SRCS_NAME	+= ft_parse_map.c
 SRCS_NAME	+= ft_select_get.c
 SRCS_NAME	+= ft_get_res.c
-SRCS_NAME	+= ft_get_tex.c
-SRCS_NAME	+= ft_get_sprite_tex.c
-SRCS_NAME	+= ft_get_colors.c
-SRCS_NAME	+= ft_get_map.c
-SRCS_NAME	+= ft_check_empty_line.c
+# SRCS_NAME	+= ft_get_tex.c
+# SRCS_NAME	+= ft_get_sprite_tex.c
+# SRCS_NAME	+= ft_get_colors.c
+# SRCS_NAME	+= ft_get_map.c
+# SRCS_NAME	+= ft_check_empty_line.c
 SRCS_NAME	+= ft_free_words.c
 SRCS_NAME	+= ft_map_error.c
 SRCS_NAME	+= ft_init_winlx.c
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 5a56df1..3a18cd8 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -1,7 +1,7 @@
 #ifndef CUB3D_H
 #define CUB3D_H
 
-#include <inttypes.h>
+#include <stdint.h>
 #include <stddef.h>
 
 #ifndef FT_W_KEY
@@ -24,8 +24,9 @@ typedef struct		s_win
 {
 	void			*wlx;
 	void			*winptr;
-	int				x_size;
-	int				y_size;
+	uint8_t			inited;
+	uint16_t		x_size;
+	uint16_t		y_size;
 }					t_win;
 
 typedef struct		s_cub
@@ -50,7 +51,7 @@ int					ft_exit(uint8_t exit_code, t_cub *clist);
 void				ft_drawsquare(int a, int b, int rgb, t_cub *clist);
 void				ft_parse_map(const char *map_path, t_cub *clist);
 int					ft_select_get(char **words, t_cub *clist);
-int					ft_get_res(int fd, t_cub *clist);
+int					ft_get_res(char **words, t_cub *clist);
 int					ft_get_tex(int fd, t_cub *clist);
 int					ft_get_sprite_tex(int fd, t_cub *clist);
 int					ft_get_colors(int fd, t_cub *clist);
@@ -58,8 +59,8 @@ int					ft_get_map(int fd, t_cub *clist);
 void				ft_check_empty_line(int fd,
 										unsigned int linum,
 										t_cub *clist);
-void				ft_free_words(char **words, char *line);
-int					ft_map_error(unsigned int linum, t_cub *clist);
+void				ft_free_words(char **words);
+int					ft_map_error(t_cub *clist);
 int					ft_init_winlx(t_cub *clist);
 void				ft_drawmap(t_cub *clist);
 void				ft_print_list(t_cub *clist);
diff --git a/src/ft_exit.c b/src/ft_exit.c
index 010c0d1..a3ec72d 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -12,8 +12,9 @@ ft_free_lists(t_cub *clist)
 	ft_memdel(clist->ea_tex_path);
 	ft_memdel(clist->we_tex_path);
 	ft_memdel(clist->sprite_path);
-	ft_free_words(clist->map, NULL);
-	/* ft_memdel(clist->wlist->winptr); */
+	ft_free_words(clist->map);
+	if (!clist->wlist->inited)
+		ft_memdel(clist->wlist->winptr);
 	ft_memdel(clist->wlist->wlx);
 	ft_memdel(clist->wlist);
 	ft_memdel(clist);
@@ -22,7 +23,8 @@ ft_free_lists(t_cub *clist)
 int
 ft_exit(uint8_t exit_code, t_cub *clist)
 {
-	mlx_destroy_window(clist->wlist->wlx, clist->wlist->winptr);
+	if (clist->wlist->inited)
+		mlx_destroy_window(clist->wlist->wlx, clist->wlist->winptr);
 	ft_printf("Exiting program\n");
 	if (exit_code < 0 || exit_code > 0)
 		ft_printf("Exit code: %hhu\n", exit_code);
diff --git a/src/ft_free_words.c b/src/ft_free_words.c
index b09b891..3231791 100644
--- a/src/ft_free_words.c
+++ b/src/ft_free_words.c
@@ -1,11 +1,10 @@
 #include <libft.h>
 
 void
-ft_free_words(char **words, char *line)
+ft_free_words(char **words)
 {
 	size_t	i;
 
-	ft_memdel(line);
 	i = 0;
 	while (words[i])
 	{
diff --git a/src/ft_get_res.c b/src/ft_get_res.c
index 66eb1df..42b3f19 100644
--- a/src/ft_get_res.c
+++ b/src/ft_get_res.c
@@ -10,30 +10,22 @@ ft_checkdigit(const char *word, t_cub *clist)
 	while (ft_isdigit(word[i]))
 		i++;
 	if (i != ft_strlen(word))
-		ft_map_error(1, clist);
+		ft_map_error(clist);
 }
 	
 int
-ft_get_res(int fd, t_cub *clist)
+ft_get_res(char **words, t_cub *clist)
 {
-	char	*line;
-	char	**words;
-
-	if (get_next_line(fd, &line) <= 0 || !(words = ft_split(line, ' ')))
-	{
-		ft_memdel(line);
-		return (ft_map_error(1, clist));
-	}
-	if (!(*words) || ft_strcmp(*words, "R") || !(*(words + 1))
+	if (!(*words + 0) || !(*(words + 1))
 		|| !(*(words + 2)) || (*(words + 3)))
 	{
-		ft_free_words(words, line);
-		return (ft_map_error(1, clist));
+		ft_free_words(words);
+		return (ft_map_error(clist));
 	}
 	ft_checkdigit(words[1], clist);
 	ft_checkdigit(words[2], clist);
 	clist->wlist->x_size = ft_atoi(words[1]);
 	clist->wlist->y_size = ft_atoi(words[2]);
-	ft_free_words(words, line);
+	ft_free_words(words);
 	return (0);
 }
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index 1b4ab59..f9c5c6c 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -14,6 +14,9 @@ t_win
 	if (!(wlist->wlx = ft_calloc(1, 1)) ||
 		!(wlist->winptr = ft_calloc(1, 1)))
 		return (NULL);
+	wlist->inited = 0;
+	wlist->x_size = 0;
+	wlist->y_size = 0;
 	return (wlist);
 }
 
@@ -38,6 +41,6 @@ t_cub
 		return (NULL);
 	clist->map[1] = 0;
 	clist->map_w = 0;
-	clist->line_chk = 1;
+	clist->line_chk = 0;
 	return (clist);
 }
diff --git a/src/ft_init_winlx.c b/src/ft_init_winlx.c
index 931e19b..ff42e69 100644
--- a/src/ft_init_winlx.c
+++ b/src/ft_init_winlx.c
@@ -16,6 +16,7 @@ ft_init_winlx(t_cub *clist)
 	if (!(clist->wlist->winptr = mlx_new_window(clist->wlist->wlx,
 				clist->wlist->x_size, clist->wlist->y_size, "Cub3D")))
 		return (-1);
+	clist->wlist->inited = 1;
 	ft_printf("Created window of size %dx%d\n",
 			  clist->wlist->x_size, clist->wlist->y_size);
 	return (0);
diff --git a/src/ft_map_error.c b/src/ft_map_error.c
index df7a2fc..0970936 100644
--- a/src/ft_map_error.c
+++ b/src/ft_map_error.c
@@ -3,9 +3,10 @@
 #include <unistd.h>
 
 int
-ft_map_error(unsigned int linum, t_cub *clist)
+ft_map_error(t_cub *clist)
 {
 	ft_dprintf(STDERR_FILENO, "Error\n");
-	ft_dprintf(STDERR_FILENO, "\033[1;31mMap error: line %d\033[0m\n", linum);
+	ft_dprintf(STDERR_FILENO,
+		"\033[1;31mMap error: line %zu\033[0m\n", clist->line_chk);
 	return (ft_exit(1, clist));
 }
diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c
index c94a91b..49d41ba 100644
--- a/src/ft_parse_map.c
+++ b/src/ft_parse_map.c
@@ -14,7 +14,7 @@ ft_check_cub(const char *map_path, t_cub *clist)
 	{
 		ft_dprintf(STDERR_FILENO, "Error\n");
 		ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n");
-		ft_free_words(words, NULL);
+		ft_free_words(words);
 		ft_exit(2, clist);
 	}
 	i = 0;
@@ -24,29 +24,29 @@ ft_check_cub(const char *map_path, t_cub *clist)
 	{
 		ft_dprintf(STDERR_FILENO, "Error\n");
 		ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n");
-		ft_free_words(words, NULL);
+		ft_free_words(words);
 		ft_exit(2, clist);
 	}
-	ft_free_words(words, NULL);
+	ft_free_words(words);
 }
 
-static void
-ft_check_map_last_line(t_cub *clist)
-{
-	size_t	i;
-	size_t	j;
+/* static void */
+/* ft_check_map_last_line(t_cub *clist) */
+/* { */
+/* 	size_t	i; */
+/* 	size_t	j; */
 
-	i = 0;
-	while (clist->map[i])
-		i++;
-	j = 0;
-	while (clist->map[i - 1][j])
-	{
-		if (clist->map[i - 1][j] != '1' && clist->map[i - 1][j] != '\0')
-			ft_map_error(11 + i - 1, clist);
-		j++;
-	}
-}
+/* 	i = 0; */
+/* 	while (clist->map[i]) */
+/* 		i++; */
+/* 	j = 0; */
+/* 	while (clist->map[i - 1][j]) */
+/* 	{ */
+/* 		if (clist->map[i - 1][j] != '1' && clist->map[i - 1][j] != '\0') */
+/* 			ft_map_error(clist); */
+/* 		j++; */
+/* 	} */
+/* } */
 
 static int
 ft_parse_it(int fd, t_cub *clist)
@@ -54,10 +54,11 @@ ft_parse_it(int fd, t_cub *clist)
 	char	*line;
 	char	**words;
 
+	clist->line_chk += 1;
 	if (get_next_line(fd, &line) <= 0)
 	{
 		ft_memdel(line);
-		return (ft_map_error(clist->line_chk, clist));
+		return (ft_map_error(clist));
 	}
 	if (!line[0])
 	{
@@ -67,7 +68,7 @@ ft_parse_it(int fd, t_cub *clist)
 	if (!(words = ft_split(line, ' ')))
 	{
 		ft_memdel(line);
-		return (ft_map_error(clist->line_chk, clist));
+		return (ft_map_error(clist));
 	}
 	ft_memdel(line);
 	return (ft_select_get(words, clist));
@@ -86,17 +87,17 @@ ft_parse_map(const char *map_path, t_cub *clist)
 		ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n");
 		ft_exit(2, clist);
 	}
-	ft_get_res(fd, clist);
-	if (ft_get_tex(fd, clist) < 0)
-		return ;
-	ft_check_empty_line(fd, 6, clist);
-	if (ft_get_sprite_tex(fd, clist) < 0)
-		return ;
-	ft_get_colors(fd, clist);
-	ft_check_empty_line(fd, 10, clist);
-	if (ft_get_map(fd, clist) < 0)
-		ft_map_error(11, clist);
-	ft_check_map_last_line(clist);
-	ft_print_list(clist);
+	ft_parse_it(fd, clist);
+	/* if (ft_get_tex(fd, clist) < 0) */
+	/* 	return ; */
+	/* ft_check_empty_line(fd, 6, clist); */
+	/* if (ft_get_sprite_tex(fd, clist) < 0) */
+	/* 	return ; */
+	/* ft_get_colors(fd, clist); */
+	/* ft_check_empty_line(fd, 10, clist); */
+	/* if (ft_get_map(fd, clist) < 0) */
+	/* 	ft_map_error(11, clist); */
+	/* ft_check_map_last_line(clist); */
+	/* ft_print_list(clist); */
 	close(fd);
 }
diff --git a/src/ft_print_list.c b/src/ft_print_list.c
index 4f67a88..495f9e4 100644
--- a/src/ft_print_list.c
+++ b/src/ft_print_list.c
@@ -22,5 +22,5 @@ ft_print_list(t_cub *clist)
 		ft_printf("%2zu [%s]\n", i + 1, clist->map[i]);
 		i++;
 	}
-	ft_printf("Map width [%d]\n", clist->map_w);
+	ft_printf("Map width [%zu]\n", clist->map_w);
 }
diff --git a/src/ft_select_get.c b/src/ft_select_get.c
index 0ddc80a..e6cdfd8 100644
--- a/src/ft_select_get.c
+++ b/src/ft_select_get.c
@@ -1,7 +1,29 @@
+#include <libft.h>
+#include <cub3d.h>
+#include <stdint.h>
+
+static uint8_t
+ft_get_id(char **words)
+{
+	if (!ft_strcmp(words[0], "R"))
+		return (0);
+	if (!ft_strcmp(words[0], "NO"))
+		return (1);
+	return (12);
+}
+
 int
 ft_select_get(char **words, t_cub *clist)
 {
-	(void)words;
-	(void)clist;
+	int		(*fun_ptr[4])(char**, t_cub*);
+	uint8_t	id;
+
+	fun_ptr[0] = ft_get_res;
+	fun_ptr[1] = 
+	if ((*fun_ptr[ft_get_id(words)])(words, clist) < 0)
+	{
+		ft_free_words(words);
+		return (ft_map_error(clist));
+	}
 	return (0);
 }
-- 
cgit v1.2.3