diff --git a/Makefile b/Makefile index cbb2712..5080206 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ NAME = cub3d -LIBFT = libft.a +LIBFT = lib/libft.a MLX = includes/mlx.h @@ -14,12 +14,12 @@ RM = rm -rf UNAME_S := $(shell uname -s) ifeq ($(UNAME_S), Linux) - LFLAGS = -lXext -lX11 -lm -lz -Llib -lft -lmlx_Linux + LFLAGS = -lXext -lX11 -lm -lz -Llib -lft -lmlx endif ifeq ($(UNAME_S), Darwin) - LFLAGS = -framework OpenGL -framework AppKit -Llib -lft -lmlx_Mac + LFLAGS = -framework OpenGL -framework AppKit -Llib -lft -lmlx endif -CFLAGS = -g -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations +CFLAGS = -Werror -Wall -Wextra -O3 -ffast-math -funsafe-math-optimizations %.o:%.c ${CC} ${CFLAGS} -c $< -o ${<:.c=.o} @@ -29,11 +29,22 @@ all: $(NAME) $(VERBOSE).SILENT: $(LIBFT): + bash -c "if [[ ! -d "libft" ]]; then git clone \ + https://git.narnaud.net/nicolas-arnaud/Libft.git libft; fi" ${MAKE} -C libft/ - cp libft/libft.a ./lib/ + cp libft/libft.a lib/ cp libft/libft.h includes/ $(MLX): + bash -c "if [[ ! -d "mlx" ]]; then git clone \ + https://github.com/42Paris/minilibx-linux.git mlx; fi" + ${MAKE} -C mlx/ +ifeq ($(UNAME_S), Linux) + cp mlx/libmlx_Linux.a lib/libmlx.a +endif +ifeq ($(UNAME_S), Darwin) + cp mlx/libmlx.a lib/ +endif cp mlx/mlx.h includes/ $(NAME): $(LIBFT) $(MLX) $(OBJS) @@ -53,8 +64,10 @@ clean: fclean: clean ${MAKE} -C libft fclean - ${RM} libft.a + ${RM} lib/libft.a ${RM} includes/libft.h + ${RM} lib/libmlx.a + ${RM} includes/mlx.h echo Cleaning binary... ${RM} ${NAME} echo ✅ diff --git a/includes/cub3d.h b/includes/cub3d.h index a2f2439..faf545e 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -24,11 +24,7 @@ # include # include # include "libft.h" -# ifdef __linux__ -# include "mlx.h" -# elif __APPLE__ -# include "mlxmac.h" -# endif +# include "mlx.h" # include "mlx_keycode.h" typedef struct s_vector diff --git a/includes/libft.h b/includes/libft.h deleted file mode 100755 index de7b0c1..0000000 --- a/includes/libft.h +++ /dev/null @@ -1,108 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* libft.h :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ -/* Updated: 2022/05/18 16:25:27 by narnaud@stude ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#ifndef LIBFT_H -# define LIBFT_H - -# include -# include -# include - -typedef struct s_slist -{ - void *content; - struct s_slist *next; -} t_slist; - -typedef struct s_i_slist -{ - int nb; - struct s_i_slist *next; -} t_i_slist; - -typedef struct s_dlist -{ - void *content; - struct s_dlist *next; - struct s_dlist *previous; -} t_dlist; - -int ft_atoi(const char *str); -void ft_bzero(void *s, size_t n); -void *ft_calloc( size_t num, size_t size ); -int ft_isalpha(int ch); -int ft_isascii(int ch); -int ft_isdigit(int ch); -int ft_isprint(int ch); -int ft_isalnum(int ch); -int ft_isspace(int ch); -int ft_isnumber(char *str); -void *ft_memchr(const void *b, int c, size_t n); -int ft_memcmp(const void *s1, const void *s2, size_t n); -void *ft_memcpy(void *dst, const void *src, size_t n); -void *ft_memmove(void *dst, const void *src, size_t n); -void ft_free_split(char **split); - -size_t ft_ilen(int nbr); -size_t ft_ulen(unsigned int nbr); -size_t ft_longbaselen(long nbr, size_t base); -int ft_croissant(int a, int b); -int ft_decroissant(int a, int b); -int ft_max(int a, int b); -int ft_min(int a, int b); - -void *ft_memset(void *b, int c, size_t len); -char *ft_strchr(const char *s, int c); -char *ft_strdup(const char *src); -size_t ft_strlcat(char *dst, const char *src, size_t dstsize); -size_t ft_strlcpy(char *dst, const char *src, size_t dstsize); -size_t ft_strlen(const char *s); -size_t ft_strlen_to(const char *str, char ch); -int ft_strncmp(const char *s1, const char *s2, size_t n); -char *ft_strnstr(const char *haystack, const char *needle, size_t len); -char *ft_strrchr(const char *s, int c); -void ft_strrev(char **str, unsigned int neg); -int ft_tolower(int c); -int ft_toupper(int c); -char *ft_substr(char const *s, unsigned int start, size_t len); -char *ft_strjoin(const char *s1, const char *s2); -char *ft_strjoin_with(char *s1, char *s2, char *inter); -char *ft_strtrim(char const *s1, char const *set); -char **ft_split(char const *str, char ch); -char *ft_itoa(int n); -char *ft_itox(unsigned long n, const char *base); -char *ft_utoa(unsigned int n); -char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); -void ft_striteri(char *s, void (*f)(unsigned int, char*)); -void ft_putchar_fd(char c, int fd); -void ft_putstr_fd(char *s, int fd); -void ft_putendl_fd(char *s, int fd); -void ft_putnbr_fd(int n, int fd); - -t_slist *ft_slst_new(void *content); -void ft_slst_add_front(t_slist **alst, t_slist *new); -int ft_slst_size(t_slist *lst); -t_slist *ft_slst_last(t_slist *lst); -void ft_slst_add_back(t_slist **alst, t_slist *new); -void ft_slst_delone(t_slist *lst, void (*del)(void *)); -void ft_slst_clear(t_slist **lst, void (*del)(void *)); -void ft_slst_iter(t_slist *lst, void (*f)(void *)); -t_slist *ft_slst_map(t_slist *lst, void *(*f)(void *), void (*del)(void *)); - -size_t ft_ilst_first(t_i_slist *lst, int (*fct)(int a, int b)); -void ft_ilst_free(t_i_slist *list); -int ft_ilst_is_in(int value, t_i_slist lst); - -t_dlist *ft_dlst_add(t_dlist *prev, void *content); -t_dlist *ft_dlst_n(t_dlist *lst, size_t n); -char **ft_dlst_to_arr(t_dlist *ptr); -#endif diff --git a/includes/mlx.h b/includes/mlx.h deleted file mode 100644 index b323412..0000000 --- a/includes/mlx.h +++ /dev/null @@ -1,139 +0,0 @@ -/* -** mlx.h for MinilibX in -** -** Made by Charlie Root -** Login -** -** Started on Mon Jul 31 16:37:50 2000 Charlie Root -** Last update Tue May 15 16:23:28 2007 Olivier Crouzet -*/ - -/* -** MinilibX - Please report bugs -*/ - - -/* -** FR msg - FR msg - FR msg -** -** La MinilibX utilise 2 librairies supplementaires qu'il -** est necessaire de rajouter a la compilation : -** -lmlx -lXext -lX11 -** -** La MinilibX permet le chargement des images de type Xpm. -** Notez que cette implementation est incomplete. -** Merci de communiquer tout probleme de chargement d'image -** de ce type. -*/ - - -#ifndef MLX_H - -#define MLX_H - - -void *mlx_init(); -/* -** needed before everything else. -** return (void *)0 if failed -*/ - - -/* -** Basic actions -*/ - -void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title); -/* -** return void *0 if failed -*/ -int mlx_clear_window(void *mlx_ptr, void *win_ptr); -int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color); -/* -** origin for x & y is top left corner of the window -** y down is positive -** color is 0x00RRGGBB -*/ - - -/* -** Image stuff -*/ - -void *mlx_new_image(void *mlx_ptr,int width,int height); -/* -** return void *0 if failed -** obsolete : image2 data is stored using bit planes -** void *mlx_new_image2(void *mlx_ptr,int width,int height); -*/ -char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel, - int *size_line, int *endian); -/* -** endian : 0 = sever X is little endian, 1 = big endian -** for mlx_new_image2, 2nd arg of mlx_get_data_addr is number_of_planes -*/ -int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr, - int x, int y); -int mlx_get_color_value(void *mlx_ptr, int color); - - -/* -** dealing with Events -*/ - -int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param); -int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param); -int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param); - -int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param); -int mlx_loop (void *mlx_ptr); -int mlx_loop_end (void *mlx_ptr); - -/* -** hook funct are called as follow : -** -** expose_hook(void *param); -** key_hook(int keycode, void *param); -** mouse_hook(int button, int x,int y, void *param); -** loop_hook(void *param); -** -*/ - - -/* -** Usually asked... -*/ - -int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color, - char *string); -void mlx_set_font(void *mlx_ptr, void *win_ptr, char *name); -void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data, - int *width, int *height); -void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename, - int *width, int *height); -int mlx_destroy_window(void *mlx_ptr, void *win_ptr); - -int mlx_destroy_image(void *mlx_ptr, void *img_ptr); - -int mlx_destroy_display(void *mlx_ptr); - -/* -** generic hook system for all events, and minilibX functions that -** can be hooked. Some macro and defines from X11/X.h are needed here. -*/ - -int mlx_hook(void *win_ptr, int x_event, int x_mask, - int (*funct)(), void *param); - -int mlx_do_key_autorepeatoff(void *mlx_ptr); -int mlx_do_key_autorepeaton(void *mlx_ptr); -int mlx_do_sync(void *mlx_ptr); - -int mlx_mouse_get_pos(void *mlx_ptr, void *win_ptr, int *x, int *y); -int mlx_mouse_move(void *mlx_ptr, void *win_ptr, int x, int y); -int mlx_mouse_hide(void *mlx_ptr, void *win_ptr); -int mlx_mouse_show(void *mlx_ptr, void *win_ptr); - -int mlx_get_screen_size(void *mlx_ptr, int *sizex, int *sizey); - -#endif /* MLX_H */ diff --git a/includes/mlxmac.h b/includes/mlxmac.h deleted file mode 100644 index e75b4af..0000000 --- a/includes/mlxmac.h +++ /dev/null @@ -1,149 +0,0 @@ -/* -** mlx.h for MinilibX in -** -** Made by Charlie Root -** Login -** -** Started on Mon Jul 31 16:37:50 2000 Charlie Root -** Last update Tue Oct 01 16:23:28 2014 Olivier Crouzet -*/ - -/* -** MinilibX - Please report bugs -*/ - - -/* -** FR msg - FR msg - FR msg -** -** MacOSX -** La MinilibX utilise 2 frameworks Mac : OpenGL et AppKit -** qu'il faut ajouter a la compilation : -** -framework OpenGL -framework AppKit -** -** UNIX / Linux -** La MinilibX utilise 2 librairies supplementaires qu'il -** est necessaire de rajouter a la compilation : -** -lmlx -lXext -lX11 -** -** La MinilibX permet le chargement des images de type Xpm. -** Notez que cette implementation est incomplete. -** -** Il y a des differences entre X11 et MacOS. -** les numeros des touches ne sont pas les memes, -** les numeros des boutons souris ne sont pas les memes. -** Egalement l'expose est gere differemment, et sous MacOS -** il est preferable d'entrer le plus tot possible dans mlx_loop, -** il est normal que les fenetres n'apparaissent pas avant mlx_loop -** (ou bien forcez avec mlx_do_sync mais c'est pas genial). -** Sous MacOS, l'octet Alpha est pris en compte dans toutes les -** images, et represente la transparence et non l'opacite comme -** c'est normalement le cas. -*/ - - -#ifndef MLX_H - -#define MLX_H - - -void *mlx_init(); -/* -** needed before everything else. -** return (void *)0 if failed -*/ - - -/* -** Basic actions -*/ - -void *mlx_new_window(void *mlx_ptr, int size_x, int size_y, char *title); -/* -** return void *0 if failed -*/ -int mlx_clear_window(void *mlx_ptr, void *win_ptr); -int mlx_pixel_put(void *mlx_ptr, void *win_ptr, int x, int y, int color); -/* -** origin for x & y is top left corner of the window -** y down is positive -** color is 0x00RRGGBB -*/ - - -/* -** Image stuff -*/ - -void *mlx_new_image(void *mlx_ptr,int width,int height); -/* -** return void *0 if failed -*/ -char *mlx_get_data_addr(void *img_ptr, int *bits_per_pixel, - int *size_line, int *endian); -/* -** endian : 0 = sever X is little endian, 1 = big endian -** endian : useless on macos, client and graphical framework have the same endian -*/ -int mlx_put_image_to_window(void *mlx_ptr, void *win_ptr, void *img_ptr, - int x, int y); -unsigned int mlx_get_color_value(void *mlx_ptr, int color); - - -/* -** dealing with Events -*/ - -int mlx_mouse_hook (void *win_ptr, int (*funct_ptr)(), void *param); -int mlx_key_hook (void *win_ptr, int (*funct_ptr)(), void *param); -int mlx_expose_hook (void *win_ptr, int (*funct_ptr)(), void *param); - -int mlx_loop_hook (void *mlx_ptr, int (*funct_ptr)(), void *param); -int mlx_loop (void *mlx_ptr); - - -/* -** hook funct are called as follow : -** -** expose_hook(void *param); -** key_hook(int keycode, void *param); -** mouse_hook(int button, int x,int y, void *param); -** loop_hook(void *param); -** -*/ - - -/* -** Usually asked... -*/ - -int mlx_string_put(void *mlx_ptr, void *win_ptr, int x, int y, int color, - char *string); -void *mlx_xpm_to_image(void *mlx_ptr, char **xpm_data, - int *width, int *height); -void *mlx_xpm_file_to_image(void *mlx_ptr, char *filename, - int *width, int *height); -void *mlx_png_file_to_image(void *mlx_ptr, char *file, int *width, int *height); - -int mlx_destroy_window(void *mlx_ptr, void *win_ptr); - -int mlx_destroy_image(void *mlx_ptr, void *img_ptr); - -/* -** generic hook system for all events, and minilibX functions that -** can be hooked. Some macro and defines from X11/X.h are needed here. -*/ - -int mlx_hook(void *win_ptr, int x_event, int x_mask, - int (*funct)(), void *param); - -int mlx_mouse_hide(); -int mlx_mouse_show(); -int mlx_mouse_move(void *win_ptr, int x, int y); -int mlx_mouse_get_pos(void *win_ptr, int *x, int *y); - -int mlx_do_key_autorepeatoff(void *mlx_ptr); -int mlx_do_key_autorepeaton(void *mlx_ptr); -int mlx_do_sync(void *mlx_ptr); - -#endif /* MLX_H */ diff --git a/lib/libft.a b/lib/libft.a deleted file mode 100644 index 8630f89..0000000 Binary files a/lib/libft.a and /dev/null differ diff --git a/lib/libmlx_Linux.a b/lib/libmlx_Linux.a deleted file mode 100644 index b794174..0000000 Binary files a/lib/libmlx_Linux.a and /dev/null differ diff --git a/lib/libmlx_Mac.a b/lib/libmlx_Mac.a deleted file mode 100644 index b105c73..0000000 Binary files a/lib/libmlx_Mac.a and /dev/null differ diff --git a/src/main.c b/src/main.c index 48ed8e5..030fdd7 100644 --- a/src/main.c +++ b/src/main.c @@ -22,9 +22,9 @@ int main(int argc, char **argv) if (!env) return (EXIT_FAILURE); init_window(env); - if (init_game(env) == EXIT_FAILURE) - exit(EXIT_FAILURE); - render(env); + if (init_game(env) == EXIT_FAILURE) + exit(EXIT_FAILURE); + render(env); mlx_loop_hook(env->mlx, update_hook, env); mlx_loop(env->mlx); mlx_destroy_image(env->mlx, env->img); diff --git a/src/mlx/mlx_1.c b/src/mlx/mlx_1.c index f37a369..0da814e 100644 --- a/src/mlx/mlx_1.c +++ b/src/mlx/mlx_1.c @@ -2,7 +2,8 @@ /* */ /* ::: :::::::: */ /* mlx_1.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/08/25 14:14:46 by narnaud #+# #+# */ /* Updated: 2022/11/14 16:28:51 by narnaud ### ########.fr */ @@ -75,8 +76,8 @@ int init_game(t_env *env) { env->tex[y].img = mlx_xpm_file_to_image(env->mlx, env->tex[y].file, &env->tex[y].width, &env->tex[y].height); - if (env->tex[y].img == NULL) - return (printf("Error:\nCan't open a texture\n"), EXIT_FAILURE); + if (env->tex[y].img == NULL) + return (printf("Error:\nCan't open a texture\n"), EXIT_FAILURE); env->tex[y].buffer = (int *)mlx_get_data_addr(env->tex[y].img, \ &env->tex[y].pixel_bits, &env->tex[y].line_bytes, @@ -91,5 +92,5 @@ int init_game(t_env *env) mlx_hook(env->win, 2, 1L << 0, key_press_hook, env); mlx_hook(env->win, 3, 1L << 1, key_release_hook, env); mlx_hook(env->win, 6, 0L, mouse_move_hook, env); - return (EXIT_SUCCESS); + return (EXIT_SUCCESS); } diff --git a/src/parsing/getline.c b/src/parsing/getline.c index 27bd7e9..264e870 100644 --- a/src/parsing/getline.c +++ b/src/parsing/getline.c @@ -29,39 +29,29 @@ char *save_buffer(char *buffer, int *i) } part2 = save_buffer(buffer, i); ret = ft_strjoin(part1, part2); - free(part1); - free(part2); - return (ret); + return (free(part1), free(part2), ret); } -/* TODO: - * check leaks - */ -char *get_next_line(int fd) +char *get_next_line(const int fd) { static char buffer[BUFFER_SIZE + 1]; static int i; - ssize_t read_size; char *line; char *ret; - char *next; + char *next; if (!*buffer) - { - read_size = read(fd, buffer, BUFFER_SIZE); - buffer[read_size] = 0; - } + buffer[read(fd, buffer, BUFFER_SIZE)] = 0; ret = ft_calloc(1, sizeof(char)); while (buffer[i]) { - next = save_buffer(buffer, &i); + next = save_buffer(buffer, &i); line = ft_strjoin(ret, next); ret = (free(ret), line); - free(next); + free(next); if (buffer[i - 1] == '\n') break ; - read_size = read(fd, buffer, BUFFER_SIZE); - buffer[read_size] = 0; + buffer[read(fd, buffer, BUFFER_SIZE)] = 0; i = 0; } if (*ret) diff --git a/src/parsing/map.c b/src/parsing/map.c index 6628a05..70018e4 100644 --- a/src/parsing/map.c +++ b/src/parsing/map.c @@ -92,7 +92,7 @@ char **create_map_array(t_slist *e_lst, int wide, int deep) { end_line = ft_calloc(wide - ft_strlen(e_lst->content) + 1, sizeof(char)); - end_line = memset(end_line, + end_line = memset(end_line, '0', wide - ft_strlen(e_lst->content) * sizeof(char)); ret[i] = ft_strjoin(e_lst->content, end_line); free(end_line); diff --git a/src/parsing/utils.c b/src/parsing/utils.c index 121fa40..47e1ff0 100644 --- a/src/parsing/utils.c +++ b/src/parsing/utils.c @@ -12,6 +12,18 @@ #include "../../includes/cub3d.h" +char **next_split(int *i, char **colors, char ***rgb) +{ + if (!colors[*i]) + { + ft_free_split(colors); + if (++(*rgb)) + colors = ft_split(**rgb, ','); + *i = 0; + } + return (colors); +} + int rgb_to_int(char **rgb) { int ret; @@ -26,14 +38,7 @@ int rgb_to_int(char **rgb) colors = ft_split(*rgb, ','); while (c < 3 && rgb && colors) { - if (!colors[i]) - { - i = 0; - ft_free_split(colors); - if (++rgb) - colors = ft_split(*rgb, ','); - continue ; - } + colors = next_split(&i, colors, &rgb); color = ft_atoi(colors[i++]); if (color >= 0 && color < 256) ret |= color << (8 * (2 - c++));