aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJozanLeClerc <bousset.rudy@gmail.com>2020-01-24 18:19:20 +0100
committerJozanLeClerc <bousset.rudy@gmail.com>2020-01-24 18:19:20 +0100
commit4286b43e9e35242b03f5a38af1099f77901c0b3e (patch)
tree6ee93930d83e24f047cbd72dbfbcbd8ebfa14dfc
parentAdded a second map for error management (diff)
download42-cub3d-4286b43e9e35242b03f5a38af1099f77901c0b3e.tar.gz
42-cub3d-4286b43e9e35242b03f5a38af1099f77901c0b3e.tar.bz2
42-cub3d-4286b43e9e35242b03f5a38af1099f77901c0b3e.tar.xz
42-cub3d-4286b43e9e35242b03f5a38af1099f77901c0b3e.tar.zst
42-cub3d-4286b43e9e35242b03f5a38af1099f77901c0b3e.zip
Much cleaner
-rw-r--r--inc/cub3d.h2
-rw-r--r--src/ft_get_tex.c3
-rw-r--r--src/ft_init_lists.c3
-rw-r--r--src/ft_init_winlx.c2
-rw-r--r--src/ft_parse_map.c29
-rw-r--r--src/main.c2
6 files changed, 38 insertions, 3 deletions
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 80f89bb..909dcf8 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -41,7 +41,7 @@ typedef struct s_win
t_win *ft_init_win(void);
t_cub *ft_init_cub(void);
-void ft_parse_map(t_win *wlist, const char *map_path);
+void ft_parse_map(const char *map_path, t_win *wlist);
int ft_key_event(int keycode, void *param);
int ft_exit(uint8_t exit_code, t_win *wlist);
void ft_drawsquare(t_win *wlist, int a, int b);
diff --git a/src/ft_get_tex.c b/src/ft_get_tex.c
index 65c8441..6bd2b7c 100644
--- a/src/ft_get_tex.c
+++ b/src/ft_get_tex.c
@@ -14,7 +14,10 @@ ft_get_tex_no(int fd, t_win *wlist)
return (ft_exit(5, wlist));
if (!(*words) || ft_strcmp(*words, "NO")
|| !(*(words + 1)) || (*(words + 2)))
+ {
+ ft_free_words(words);
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))))
diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c
index 4aa2d81..799204b 100644
--- a/src/ft_init_lists.c
+++ b/src/ft_init_lists.c
@@ -11,6 +11,9 @@ t_win
if (!(wlist = (t_win*)malloc(sizeof(t_win))))
return (NULL);
+ if (!(wlist->wlx = ft_calloc(1, 1)) ||
+ !(wlist->winptr = ft_calloc(1, 1)))
+ return (NULL);
return (wlist);
}
diff --git a/src/ft_init_winlx.c b/src/ft_init_winlx.c
index 0e272fd..a4b7208 100644
--- a/src/ft_init_winlx.c
+++ b/src/ft_init_winlx.c
@@ -6,8 +6,10 @@
int
ft_init_winlx(t_win *wlist)
{
+ ft_memdel(wlist->wlx);
if (!(wlist->wlx = mlx_init()))
return (-1);
+ ft_memdel(wlist->winptr);
if (!(wlist->winptr = (void*)malloc(wlist->x_size * wlist->y_size)))
return (-1);
if (!(wlist->winptr = mlx_new_window(wlist->wlx, wlist->x_size,\
diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c
index f4eb4f0..0a4aa45 100644
--- a/src/ft_parse_map.c
+++ b/src/ft_parse_map.c
@@ -4,15 +4,42 @@
#include <fcntl.h>
#include <unistd.h>
+static void
+ft_check_cub(const char *map_path, t_win *wlist)
+{
+ char **words;
+ size_t i;
+
+ if (!(words = ft_split(map_path, '.')))
+ {
+ ft_dprintf(STDERR_FILENO, "Error\n");
+ ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n");
+ ft_free_words(words);
+ ft_exit(2, wlist);
+ }
+ i = 0;
+ while (words[i])
+ i++;
+ if (ft_strcmp(words[i - 1], "cub"))
+ {
+ ft_dprintf(STDERR_FILENO, "Error\n");
+ ft_dprintf(STDERR_FILENO, "\033[31;1mMap is not a .cub\033[0m\n");
+ ft_free_words(words);
+ ft_exit(2, wlist);
+ }
+ ft_free_words(words);
+}
+
/*
** I can't close fd
*/
void
-ft_parse_map(t_win *wlist, const char *map_path)
+ft_parse_map(const char *map_path, t_win *wlist)
{
int fd;
+ ft_check_cub(map_path, wlist);
fd = open(map_path, O_RDONLY);
if (fd < 0)
{
diff --git a/src/main.c b/src/main.c
index b47b36f..0f2c93c 100644
--- a/src/main.c
+++ b/src/main.c
@@ -15,7 +15,7 @@ int
ft_memdel(wlist);
return (1);
}
- ft_parse_map(wlist, "map/map_one.cub");
+ ft_parse_map("map/map_two.cub", wlist);
if (ft_init_winlx(wlist) < 0)
return (ft_exit(3, wlist));
mlx_key_hook(wlist->winptr, ft_key_event, wlist);