aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--Makefile8
-rw-r--r--inc/cub3d.h51
-rw-r--r--src/ft_drawsquare.c3
-rw-r--r--src/ft_exit.c18
-rw-r--r--src/ft_free_words.c15
-rw-r--r--src/ft_get_res.c27
-rw-r--r--src/ft_get_tex.c77
-rw-r--r--src/ft_init_lists.c6
-rw-r--r--src/ft_key_events.c7
-rw-r--r--src/ft_map_error.c4
-rw-r--r--src/ft_parse_map.c7
-rw-r--r--src/main.c18
12 files changed, 172 insertions, 69 deletions
diff --git a/Makefile b/Makefile
index f52c084..3798958 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-default: run
+default: all
#==============================================================================#
#--------------------------------- Shell --------------------------------------#
#==============================================================================#
@@ -23,6 +23,8 @@ SRCS_NAME += ft_exit.c
SRCS_NAME += ft_drawsquare.c
SRCS_NAME += ft_parse_map.c
SRCS_NAME += ft_get_res.c
+SRCS_NAME += ft_get_tex.c
+SRCS_NAME += ft_free_words.c
SRCS_NAME += ft_map_error.c
SRCS_NAME += ft_init_winlx.c
#------------------------------------------------------------------------------#
@@ -46,7 +48,11 @@ endif
DEBUG = -g3
FSANITIZE = -fsanitize=address
#------------------------------------------------------------------------------#
+ifeq (${OS}, Darwin)
NAME = cub3d
+else
+NAME = a.out
+endif
#==============================================================================#
#--------------------------------- UNIX ---------------------------------------#
#==============================================================================#
diff --git a/inc/cub3d.h b/inc/cub3d.h
index 4f495d4..80f89bb 100644
--- a/inc/cub3d.h
+++ b/inc/cub3d.h
@@ -19,33 +19,36 @@
#define FT_ESC_KEY 53
#endif
-typedef struct s_win
+typedef struct s_cub
{
- void *wlx;
- void *winptr;
- int x_size;
- int y_size;
-} t_win;
+ char *no_tex_path;
+ char *so_tex_path;
+ char *ea_tex_path;
+ char *we_tex_path;
+ char *sprite_path;
+ int f_color;
+ int c_color;
+} t_cub;
-typedef struct s_cub
+typedef struct s_win
{
- char *north_path;
- char *south_path;
- char *west_path;
- char *east_path;
- char *sprite_path;
- int f_color;
- int c_color;
-} t_cub;
+ void *wlx;
+ void *winptr;
+ int x_size;
+ int y_size;
+ struct s_cub *clist;
+} t_win;
-t_cub *ft_init_cub(void);
-t_win *ft_init_win(void);
-void ft_parse_map(t_win *wlist, t_cub *clist, const char *map_path);
-int ft_key_event(int keycode, void *param);
-int ft_exit(uint8_t exit_code);
-void ft_drawsquare(t_win *wlist, t_cub *clist, int a, int b);
-int ft_get_res(int fd, t_win *wlist);
-int ft_map_error(unsigned int line);
-int ft_init_winlx(t_win *wlist);
+t_win *ft_init_win(void);
+t_cub *ft_init_cub(void);
+void ft_parse_map(t_win *wlist, const char *map_path);
+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);
+int ft_get_res(int fd, t_win *wlist);
+int ft_get_tex(int fd, t_win *wlist);
+void ft_free_words(char **words);
+int ft_map_error(unsigned int line, t_win *wlist);
+int ft_init_winlx(t_win *wlist);
#endif
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);
}