diff options
-rw-r--r-- | Makefile | 20 | ||||
-rw-r--r-- | inc/cub3d.h | 1 | ||||
-rw-r--r-- | libft/src/ft_strcmp.c | 2 | ||||
-rw-r--r-- | map/map_one.cub | 17 | ||||
-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 |
11 files changed, 105 insertions, 29 deletions
@@ -24,6 +24,7 @@ SRCS_NAME += ft_drawsquare.c SRCS_NAME += ft_parse_map.c SRCS_NAME += ft_get_res.c SRCS_NAME += ft_map_error.c +SRCS_NAME += ft_init_winlx.c #------------------------------------------------------------------------------# SRCS = $(addprefix ${SRCS_DIR},${SRCS_NAME}) #------------------------------------------------------------------------------# @@ -42,7 +43,7 @@ ifdef ASAN CFLAGS += ${FSANITIZE} endif #------------------------------------------------------------------------------# -DEBUG = -glldb +DEBUG = -g3 FSANITIZE = -fsanitize=address #------------------------------------------------------------------------------# NAME = cub3d @@ -51,20 +52,30 @@ NAME = cub3d #==============================================================================# MKDIR = mkdir -p RM = rm -rf +OS = $(shell uname) #==============================================================================# #--------------------------------- Rules --------------------------------------# #==============================================================================# ${OBJS_DIR}%.o: ${SRCS_DIR}%.c ${INCS_DIR}${INCS} @${MKDIR} ${OBJS_DIR} - ${CC} -c ${CFLAGS} -I${LFT_DIR}${INCS_DIR} -I${MLX_DIR} -I${INCS_DIR} \ --o $@ $< +ifeq (${OS}, Darwin) + ${CC} -c ${CFLAGS} -I${LFT_DIR}${INCS_DIR} -I${MLX_DIR} -I${INCS_DIR} -o $@ $< +else + ${CC} -c ${CFLAGS} -I${LFT_DIR}${INCS_DIR} -I${INCS_DIR} -o $@ $< +endif #------------------------------------------------------------------------------# ${NAME}: ${OBJS} +ifeq (${OS}, Darwin) ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -L${MLX_DIR} -lft -lmlx -lm \ -framework OpenGL -framework AppKit +else + ${CC} ${CFLAGS} -o $@ ${OBJS} -L${LFT_DIR} -lft -lX11 -lXext -lmlx +endif #------------------------------------------------------------------------------# all: +ifeq (${OS}, Darwin) @$(MAKE) --no-print-directory -C ${MLX_DIR} all +endif ifdef ASAN @$(MAKE) --no-print-directory -C ${LFT_DIR} all ASAN=1 else @@ -73,12 +84,13 @@ endif @$(MAKE) ${NAME} #------------------------------------------------------------------------------# clean: +ifeq (${OS}, Darwin) @$(MAKE) --no-print-directory -C ${MLX_DIR} clean +endif @$(MAKE) --no-print-directory -C ${LFT_DIR} clean ${RM} ${OBJS_DIR} #------------------------------------------------------------------------------# fclean: clean - @$(MAKE) --no-print-directory -C ${MLX_DIR} clean @$(MAKE) --no-print-directory -C ${LFT_DIR} fclean ${RM} ${NAME} ${RM} ${NAME}.dSYM diff --git a/inc/cub3d.h b/inc/cub3d.h index d5ef69e..0ca7128 100644 --- a/inc/cub3d.h +++ b/inc/cub3d.h @@ -30,5 +30,6 @@ 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); #endif diff --git a/libft/src/ft_strcmp.c b/libft/src/ft_strcmp.c index ca5cf60..595f349 100644 --- a/libft/src/ft_strcmp.c +++ b/libft/src/ft_strcmp.c @@ -17,7 +17,7 @@ int ft_strcmp(const char *s1, const char *s2) { - size_t i; + size_t i; i = 0; while (s1[i] == s2[i] && i < ft_strlen(s1) - 1) diff --git a/map/map_one.cub b/map/map_one.cub index e52aee0..df60d8f 100644 --- a/map/map_one.cub +++ b/map/map_one.cub @@ -1,4 +1,4 @@ -R 1920 1080 +R 800 600 NO ./path_to_the_north_texture SO ./path_to_the_south_texture WE ./path_to_the_west_texture @@ -8,4 +8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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 +1 0 1 1 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 1 1 0 1 1 1 0 1 0 1 0 0 1 0 0 0 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 0 0 0 0 0 0 1 0 0 0 1 +1 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 0 0 0 1 +1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 1 0 0 0 1 +1 1 0 0 0 0 0 0 1 1 0 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 N 0 1 +1 1 1 1 0 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 0 1 1 1 1 0 0 0 1 +1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
\ No newline at end of file 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); |