From 6776a1f3491663c85480326e0d01f9a6c5455bbd Mon Sep 17 00:00:00 2001
From: JozanLeClerc <bousset.rudy@gmail.com>
Date: Fri, 24 Jan 2020 17:57:20 +0100
Subject: Tons of changes

---
 src/ft_drawsquare.c |  3 +--
 src/ft_exit.c       | 18 ++++++++++++-
 src/ft_free_words.c | 15 +++++++++++
 src/ft_get_res.c    | 27 +++++--------------
 src/ft_get_tex.c    | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 src/ft_init_lists.c |  6 +++++
 src/ft_key_events.c |  7 +----
 src/ft_map_error.c  |  4 +--
 src/ft_parse_map.c  |  7 ++---
 src/main.c          | 18 +++++++------
 10 files changed, 138 insertions(+), 44 deletions(-)
 create mode 100644 src/ft_free_words.c

(limited to 'src')

diff --git a/src/ft_drawsquare.c b/src/ft_drawsquare.c
index b37dc77..78ebd8c 100644
--- a/src/ft_drawsquare.c
+++ b/src/ft_drawsquare.c
@@ -2,12 +2,11 @@
 #include <cub3d.h>
 
 void
-ft_drawsquare(t_win *wlist, t_cub *clist, int a, int b)
+ft_drawsquare(t_win *wlist, int a, int b)
 {
 	int	x;
 	int	y;
 
-	(void)clist;
 	x = a;
 	y = b;
 	while (x > a - 40)
diff --git a/src/ft_exit.c b/src/ft_exit.c
index 5a4ef73..165c3d9 100644
--- a/src/ft_exit.c
+++ b/src/ft_exit.c
@@ -1,13 +1,29 @@
 #include <libft.h>
+#include <cub3d.h>
 #include <stdlib.h>
 #include <inttypes.h>
 
+static void
+ft_free_lists(t_win *wlist)
+{
+	ft_memdel(wlist->clist->no_tex_path);
+	ft_memdel(wlist->clist->so_tex_path);
+	ft_memdel(wlist->clist->ea_tex_path);
+	ft_memdel(wlist->clist->we_tex_path);
+	ft_memdel(wlist->clist->sprite_path);
+	ft_memdel(wlist->clist);
+	ft_memdel(wlist->winptr);
+	ft_memdel(wlist->wlx);
+	ft_memdel(wlist);
+}
+
 int
-ft_exit(uint8_t exit_code)
+ft_exit(uint8_t exit_code, t_win *wlist)
 {
 	ft_printf("Exiting program\n");
 	if (exit_code < 0 || exit_code > 0)
 		ft_printf("Exit code: %hhu\n", exit_code);
+	ft_free_lists(wlist);
 	exit(exit_code);
 	return (0);
 }
diff --git a/src/ft_free_words.c b/src/ft_free_words.c
new file mode 100644
index 0000000..3231791
--- /dev/null
+++ b/src/ft_free_words.c
@@ -0,0 +1,15 @@
+#include <libft.h>
+
+void
+ft_free_words(char **words)
+{
+	size_t	i;
+
+	i = 0;
+	while (words[i])
+	{
+		ft_memdel(words[i]);
+		i++;
+	}
+	ft_memdel(words);
+}
diff --git a/src/ft_get_res.c b/src/ft_get_res.c
index 92e0890..79df8a3 100644
--- a/src/ft_get_res.c
+++ b/src/ft_get_res.c
@@ -2,21 +2,7 @@
 #include <cub3d.h>
 
 static void
-ft_free_words(char **words)
-{
-	size_t	i;
-
-	i = 0;
-	while (words[i])
-	{
-		ft_memdel(words[i]);
-		i++;
-	}
-	ft_memdel(words);
-}
-
-static void
-ft_checkdigit(const char *word)
+ft_checkdigit(const char *word, t_win *wlist)
 {
 	size_t	i;
 
@@ -24,7 +10,7 @@ ft_checkdigit(const char *word)
 	while (ft_isdigit(word[i]))
 		i++;
 	if (i != ft_strlen(word))
-		ft_map_error(1);
+		ft_map_error(1, wlist);
 }
 	
 int
@@ -33,15 +19,14 @@ ft_get_res(int fd, t_win *wlist)
 	char	*line;
 	char	**words;
 
-	(void)wlist;
 	get_next_line(fd, &line);
 	if (!(words = ft_split(line, ' ')))
-		return (ft_exit(5));
+		return (ft_exit(5, wlist));
 	if (!(*words) || ft_strcmp(*words, "R") || !(*(words + 1))
 		|| !(*(words + 2)) || (*(words + 3)))
-		ft_map_error(1);
-	ft_checkdigit(words[1]);
-	ft_checkdigit(words[2]);
+		return (ft_map_error(1, wlist));
+	ft_checkdigit(words[1], wlist);
+	ft_checkdigit(words[2], wlist);
 	wlist->x_size = ft_atoi(words[1]);
 	wlist->y_size = ft_atoi(words[2]);
 	ft_free_words(words);
diff --git a/src/ft_get_tex.c b/src/ft_get_tex.c
index d38e494..5bd7a4f 100644
--- a/src/ft_get_tex.c
+++ b/src/ft_get_tex.c
@@ -1,7 +1,82 @@
 #include <libft.h>
 #include <cub3d.h>
+#include <stdlib.h>
+
+static int
+ft_get_tex_no(int fd, t_win *wlist)
+{
+	char	*line;
+	char	**words;
+	size_t	len;
+
+	get_next_line(fd, &line);
+	if (!(words = ft_split(line, ' ')))
+		return (ft_exit(5, wlist));
+	if (!(*words) || ft_strcmp(*words, "NO")
+		|| !(*(words + 1)) || (*(words + 2)))
+		return (ft_map_error(2, wlist));
+	ft_memdel(wlist->clist->no_tex_path);
+	len = ft_strlen(*(words + 1));
+	if (!(wlist->clist->no_tex_path = (char*)malloc((len + 1) * sizeof(char))))
+		return (-1);
+	ft_strlcpy(wlist->clist->no_tex_path, *(words + 1), len + 1);
+	ft_free_words(words);
+	ft_memdel(line);
+	return (0);
+}
+
+static int
+ft_get_tex_so(int fd, t_win *wlist)
+{
+	char	*line;
+	char	**words;
+	size_t	len;
+
+	get_next_line(fd, &line);
+	if (!(words = ft_split(line, ' ')))
+		return (ft_exit(5, wlist));
+	if (!(*words) || ft_strcmp(*words, "SO")
+		|| !(*(words + 1)) || (*(words + 2)))
+		return (ft_map_error(3, wlist));
+	ft_memdel(wlist->clist->so_tex_path);
+	len = ft_strlen(*(words + 1));
+	if (!(wlist->clist->so_tex_path = (char*)malloc((len + 1) * sizeof(char))))
+		return (-1);
+	ft_strlcpy(wlist->clist->so_tex_path, *(words + 1), len + 1);
+	ft_free_words(words);
+	ft_memdel(line);
+	return (0);
+}
+
+static int
+ft_get_tex_we(int fd, t_win *wlist)
+{
+	char	*line;
+	char	**words;
+	size_t	len;
+
+	get_next_line(fd, &line);
+	if (!(words = ft_split(line, ' ')))
+		return (ft_exit(5, wlist));
+	if (!(*words) || ft_strcmp(*words, "WE")
+		|| !(*(words + 1)) || (*(words + 2)))
+		return (ft_map_error(4, wlist));
+	ft_memdel(wlist->clist->we_tex_path);
+	len = ft_strlen(*(words + 1));
+	if (!(wlist->clist->we_tex_path = (char*)malloc((len + 1) * sizeof(char))))
+		return (-1);
+	ft_strlcpy(wlist->clist->we_tex_path, *(words + 1), len + 1);
+	ft_free_words(words);
+	ft_memdel(line);
+	return (0);
+}
 
 int
-ft_get_tex(int fd, t_cub *clist)
+ft_get_tex(int fd, t_win *wlist)
 {
+	if (ft_get_tex_no(fd, wlist) < 0 ||
+		ft_get_tex_so(fd, wlist) < 0 ||
+		ft_get_tex_we(fd, wlist) < 0)
+		return (-1);
+	return (0);
 }
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index 126843c..4aa2d81 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -21,5 +21,11 @@ t_cub
 
 	if (!(clist = (t_cub*)malloc(sizeof(t_cub))))
 		return (NULL);
+	if (!(clist->no_tex_path = (char*)ft_calloc(1, 1)) ||
+		!(clist->so_tex_path = (char*)ft_calloc(1, 1)) ||
+		!(clist->ea_tex_path = (char*)ft_calloc(1, 1)) ||
+		!(clist->we_tex_path = (char*)ft_calloc(1, 1)) ||
+		!(clist->sprite_path = (char*)ft_calloc(1, 1)))	
+		return (NULL);
 	return (clist);
 }
diff --git a/src/ft_key_events.c b/src/ft_key_events.c
index 74d6346..8c8167a 100644
--- a/src/ft_key_events.c
+++ b/src/ft_key_events.c
@@ -46,11 +46,6 @@ ft_key_event(int keycode, void *param)
 	(keycode == FT_D_KEY) ? (keycode = 3) : 0;
 	(keycode <= 3) ? ((*fun_ptr[keycode])()) : 0;
 	if (keycode == FT_ESC_KEY)
-	{
-		ft_memdel(((t_win*)param)->winptr);
-		ft_memdel(((t_win*)param)->wlx);
-		ft_memdel((t_win*)param);
-		ft_exit(0);
-	}
+		ft_exit(0, ((t_win*)param));
 	return (0);
 }
diff --git a/src/ft_map_error.c b/src/ft_map_error.c
index dae08e9..6af5438 100644
--- a/src/ft_map_error.c
+++ b/src/ft_map_error.c
@@ -3,9 +3,9 @@
 #include <unistd.h>
 
 int
-ft_map_error(unsigned int line)
+ft_map_error(unsigned int line, t_win *wlist)
 {
 	ft_dprintf(STDERR_FILENO, "Error\n");
 	ft_dprintf(STDERR_FILENO, "\033[1;31mMap error: line %d\033[0m\n", line);
-	return (ft_exit(1));
+	return (ft_exit(1, wlist));
 }
diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c
index 60726a2..f4eb4f0 100644
--- a/src/ft_parse_map.c
+++ b/src/ft_parse_map.c
@@ -9,17 +9,18 @@
 */
 
 void
-ft_parse_map(t_win *wlist, t_cub *clist, const char *map_path)
+ft_parse_map(t_win *wlist, const char *map_path)
 {
 	int		fd;
 
-	(void)clist;
 	fd = open(map_path, O_RDONLY);
 	if (fd < 0)
 	{
 		ft_dprintf(STDERR_FILENO, "Error\n");
 		ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n");
-		ft_exit(2);
+		ft_exit(2, wlist);
 	}
 	ft_get_res(fd, wlist);
+	if (ft_get_tex(fd, wlist) < 0)
+		return ;
 }
diff --git a/src/main.c b/src/main.c
index c252bca..b47b36f 100644
--- a/src/main.c
+++ b/src/main.c
@@ -7,17 +7,19 @@ int
 	main(void)
 {
 	t_win	*wlist;
-	t_cub	*clist;
 
-	if (!(wlist = ft_init_win())
-		|| !(clist = ft_init_cub()))
-		return (ft_exit(4));
-	ft_parse_map(wlist, clist, "map/map_one.cub");
+	if (!(wlist = ft_init_win()))
+		return (1);
+	if (!(wlist->clist = ft_init_cub()))
+	{
+		ft_memdel(wlist);
+		return (1);
+	}
+	ft_parse_map(wlist, "map/map_one.cub");
 	if (ft_init_winlx(wlist) < 0)
-		return (ft_exit(3));
+		return (ft_exit(3, wlist));
 	mlx_key_hook(wlist->winptr, ft_key_event, wlist);
-	ft_drawsquare(wlist, clist, 80, 80);
+	ft_drawsquare(wlist, 80, 80);
 	mlx_loop(wlist->wlx);
-	ft_memdel(clist);
 	return (0);
 }
-- 
cgit v1.2.3