diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ft_get_res.c | 46 | ||||
-rw-r--r-- | src/ft_init_lists.c | 13 | ||||
-rw-r--r-- | src/ft_init_winlx.c | 18 | ||||
-rw-r--r-- | src/ft_key_events.c | 2 | ||||
-rw-r--r-- | src/ft_map_error.c | 4 | ||||
-rw-r--r-- | src/ft_parse_map.c | 4 | ||||
-rw-r--r-- | src/main.c | 7 |
7 files changed, 72 insertions, 22 deletions
diff --git a/src/ft_get_res.c b/src/ft_get_res.c index c610fc1..92e0890 100644 --- a/src/ft_get_res.c +++ b/src/ft_get_res.c @@ -1,22 +1,50 @@ #include <libft.h> #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) +{ + size_t i; + + i = 0; + while (ft_isdigit(word[i])) + i++; + if (i != ft_strlen(word)) + ft_map_error(1); +} + int ft_get_res(int fd, t_win *wlist) { char *line; char **words; - int i; (void)wlist; get_next_line(fd, &line); - words = ft_split(line, ' '); - i = 0; - while (words) - { - ft_printf("[%s] ", words[i]); - i++; - } - ft_printf("\n"); + if (!(words = ft_split(line, ' '))) + return (ft_exit(5)); + if (!(*words) || ft_strcmp(*words, "R") || !(*(words + 1)) + || !(*(words + 2)) || (*(words + 3))) + ft_map_error(1); + ft_checkdigit(words[1]); + ft_checkdigit(words[2]); + wlist->x_size = ft_atoi(words[1]); + wlist->y_size = ft_atoi(words[2]); + ft_free_words(words); + ft_memdel(line); return (0); } diff --git a/src/ft_init_lists.c b/src/ft_init_lists.c index 7a6eb79..126843c 100644 --- a/src/ft_init_lists.c +++ b/src/ft_init_lists.c @@ -1,6 +1,7 @@ #include <libft.h> #include <mlx.h> #include <cub3d.h> +#include <stddef.h> #include <stdlib.h> t_win @@ -8,13 +9,8 @@ t_win { t_win *wlist; - wlist = (t_win*)malloc(sizeof(t_win)); - wlist->x_size = 800; - wlist->y_size = 600; - wlist->wlx = mlx_init(); - wlist->winptr = (void*)malloc(wlist->x_size * wlist->y_size); - wlist->winptr = mlx_new_window(wlist->wlx, wlist->x_size, wlist->y_size, "Cub3D"); - ft_printf("Created window of size %dx%d\n", wlist->x_size, wlist->y_size); + if (!(wlist = (t_win*)malloc(sizeof(t_win)))) + return (NULL); return (wlist); } @@ -23,6 +19,7 @@ t_cub { t_cub *clist; - clist = (t_cub*)malloc(sizeof(t_cub)); + if (!(clist = (t_cub*)malloc(sizeof(t_cub)))) + return (NULL); return (clist); } diff --git a/src/ft_init_winlx.c b/src/ft_init_winlx.c new file mode 100644 index 0000000..0e272fd --- /dev/null +++ b/src/ft_init_winlx.c @@ -0,0 +1,18 @@ +#include <libft.h> +#include <mlx.h> +#include <cub3d.h> +#include <stdlib.h> + +int +ft_init_winlx(t_win *wlist) +{ + if (!(wlist->wlx = mlx_init())) + return (-1); + if (!(wlist->winptr = (void*)malloc(wlist->x_size * wlist->y_size))) + return (-1); + if (!(wlist->winptr = mlx_new_window(wlist->wlx, wlist->x_size,\ + wlist->y_size, "Cub3D"))) + return (-1); + ft_printf("Created window of size %dx%d\n", wlist->x_size, wlist->y_size); + return (0); +} diff --git a/src/ft_key_events.c b/src/ft_key_events.c index 6702988..088df54 100644 --- a/src/ft_key_events.c +++ b/src/ft_key_events.c @@ -44,8 +44,8 @@ ft_key_event(int keycode, void *param) (keycode <= 3) ? ((*fun_ptr[keycode])()) : 0; if (keycode == 53) { - ft_memdel(((t_win*)param)->wlx); ft_memdel(((t_win*)param)->winptr); + ft_memdel(((t_win*)param)->wlx); ft_memdel((t_win*)param); ft_exit(0); } diff --git a/src/ft_map_error.c b/src/ft_map_error.c index bffb6d1..dae08e9 100644 --- a/src/ft_map_error.c +++ b/src/ft_map_error.c @@ -1,9 +1,11 @@ #include <libft.h> #include <cub3d.h> +#include <unistd.h> int ft_map_error(unsigned int line) { - ft_printf("\033[1;31mMap error: line %d\033[0m\n", line); + ft_dprintf(STDERR_FILENO, "Error\n"); + ft_dprintf(STDERR_FILENO, "\033[1;31mMap error: line %d\033[0m\n", line); return (ft_exit(1)); } diff --git a/src/ft_parse_map.c b/src/ft_parse_map.c index d3d1f53..2fa5741 100644 --- a/src/ft_parse_map.c +++ b/src/ft_parse_map.c @@ -2,6 +2,7 @@ #include <cub3d.h> #include <stdlib.h> #include <fcntl.h> +#include <unistd.h> /* ** I can't close @@ -16,7 +17,8 @@ ft_parse_map(t_win *wlist, t_cub *clist, const char *map_path) fd = open(map_path, O_RDONLY); if (fd < 0) { - ft_printf("\033[31;1mNo map\033[0m\n"); + ft_dprintf(STDERR_FILENO, "Error\n"); + ft_dprintf(STDERR_FILENO, "\033[31;1mNo map\033[0m\n"); ft_exit(2); } ft_get_res(fd, wlist); @@ -9,9 +9,12 @@ int t_win *wlist; t_cub *clist; - wlist = ft_init_win(); - clist = ft_init_cub(); + if (!(wlist = ft_init_win()) + || !(clist = ft_init_cub())) + return (ft_exit(4)); ft_parse_map(wlist, clist, "map/map_one.cub"); + if (ft_init_winlx(wlist) < 0) + return (ft_exit(3)); mlx_key_hook(wlist->winptr, ft_key_event, wlist); ft_drawsquare(wlist, clist, 80, 80); mlx_loop(wlist->wlx); |