diff --git a/Makefile b/Makefile index 9884aab..ad1e59c 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ D_SRCS = srcs \ srcs/init \ srcs/hook \ srcs/draw \ - srcs/free + srcs/mem SRCS = cube3d.c \ \ @@ -23,14 +23,17 @@ SRCS = cube3d.c \ \ keyhook.c \ key_do_action.c \ - key_is_action_1.c + key_is_action_1.c \ + \ + free_exit.c # headers D_HEADERS = headers HEADERS = cube3d.h \ cube3d_proto.h \ cube3d_macro.h \ - cube3d_struct.h + cube3d_struct.h \ + colors.h INCLUDES = -I$(D_HEADERS) -I$(D_LFT) -I$(D_LMLX) diff --git a/headers/colors.h b/headers/colors.h new file mode 100644 index 0000000..0374e42 --- /dev/null +++ b/headers/colors.h @@ -0,0 +1,25 @@ +#ifndef COLORS_H +# define COLORS_H + +# define GRAY "\e[0;30m" +# define RED "\e[0;31m" +# define GREEN "\e[0;32m" +# define YELLOW "\e[0;33m" +# define BLUE "\e[0;34m" +# define PURPLE "\e[0;35m" +# define CYAN "\e[0;36m" +# define WHITE "\e[0;37m" + +# define B_GRAY "\e[1;30m" +# define B_RED "\e[1;31m" +# define B_GREEN "\e[1;32m" +# define B_YELLOW "\e[1;33m" +# define B_BLUE "\e[1;34m" +# define B_PURPLE "\e[1;35m" +# define B_CYAN "\e[1;36m" +# define B_WHITE "\e[1;37m" + +# define RESET "\e[0m" + +#endif + diff --git a/headers/cube3d.h b/headers/cube3d.h index a95cff0..a321709 100644 --- a/headers/cube3d.h +++ b/headers/cube3d.h @@ -1,14 +1,16 @@ #ifndef CUBE3D_H # define CUBE3D_H -#include "../libs/libft/includes/libft.h" -#include -#include // for sleep() -#include // for atoi() -#include // for printf() +# include "../libs/libft/includes/libft.h" +# include +# include // for sleep() +# include // for atoi() +# include // for printf() -#include "cube3d_macro.h" -#include "cube3d_struct.h" -#include "cube3d_proto.h" +# include "colors.h" + +# include "cube3d_macro.h" +# include "cube3d_struct.h" +# include "cube3d_proto.h" #endif diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h index 603cca5..96b5f74 100644 --- a/headers/cube3d_proto.h +++ b/headers/cube3d_proto.h @@ -44,6 +44,9 @@ int is_go_backward(int *k_hook); // ------------------------------- // SRC/FREE +void *gmalloc(size_t size); +void gfree(void *addr); +void gexit(char *str); #endif diff --git a/libs/libft b/libs/libft index 39b3f2b..2767ff1 160000 --- a/libs/libft +++ b/libs/libft @@ -1 +1 @@ -Subproject commit 39b3f2bc4ea6aa99ef7df5eb3239055bfe13ecd5 +Subproject commit 2767ff165f48330340f35276d6dbf193f91ea8c2 diff --git a/srcs/hook/key_do_action.c b/srcs/hook/key_do_action.c index 6612f36..7393c30 100644 --- a/srcs/hook/key_do_action.c +++ b/srcs/hook/key_do_action.c @@ -1,10 +1,10 @@ #include "cube3d.h" -int shut_down(t_game *game) +int shut_down(t_game *game) { mlx_destroy_window(game->mlx_ptr, game->win_ptr); + gexit(B_RED"close windows"RESET"\n"); exit(0); - free(game); return (0); } diff --git a/srcs/hook/keyhook.c b/srcs/hook/keyhook.c index 706cb32..4ea8526 100644 --- a/srcs/hook/keyhook.c +++ b/srcs/hook/keyhook.c @@ -3,7 +3,8 @@ // temp, to map all the keys on linux and mac static int print_keycode(int keycode) { - ft_putnbrendl(keycode); + ft_putnbr_fd(keycode, 1); + ft_putchar_fd('\n', 1); return(0); } // temp end diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c index 2c25a4b..732c9fb 100644 --- a/srcs/init/init_struct.c +++ b/srcs/init/init_struct.c @@ -4,7 +4,7 @@ t_game *init_game(void) { t_game *game; - game = malloc(sizeof(t_game)); + game = gmalloc(sizeof(t_game)); // player first position game->plr_x = 16; game->plr_y = 16; diff --git a/srcs/mem/free_exit.c b/srcs/mem/free_exit.c new file mode 100644 index 0000000..899b14d --- /dev/null +++ b/srcs/mem/free_exit.c @@ -0,0 +1,49 @@ +#include "cube3d.h" + +static int comp_addr(void *to_find, void *to_compare) +{ + return (to_find == to_compare); +} + +t_list **alloc_lst() +{ + static t_list *lst = NULL; + + return (&lst); +} + +void *gmalloc(size_t size) +{ + void *tmp; + t_list **lst; + + lst = alloc_lst(); + tmp = ft_memalloc(size); + if (!tmp) + gexit(B_RED"failed allocation element"RESET"\n"); + if (!ft_lstpush_back(lst, ft_lstcreate(tmp))) + gexit(B_RED"failed allocation list of alocated address"RESET"\n"); + return (tmp); +} + +void gfree(void *addr) +{ + t_list **lst; + t_list *tmp; + + lst = alloc_lst(); + tmp = ft_lstfind((*lst), addr, comp_addr); + if (!tmp) + ft_putstr_fd(B_RED"failed free element"RESET"\n", 2); + ft_lsterase(tmp, free); +} + +void gexit(char *str) +{ + t_list **lst; + + lst = alloc_lst(); + ft_putstr_fd(str, 2); + ft_lstfree((*lst), free); + exit(0); +} diff --git a/test_lst.c b/test_lst.c new file mode 100644 index 0000000..e92f8fb --- /dev/null +++ b/test_lst.c @@ -0,0 +1,162 @@ +#include "libs/libft/includes/libft.h" +#include + +void lst_print(void *element) +{ + printf("lst->element : %s - %p\n",(char *)element, element); +} + +void lst_delete(void *element) +{ + free(element); +} + +int lst_comp_ptr(void *to_find, void *to_compare) +{ + return (to_find == to_compare); +} + +int lst_comp(void *to_find, void *to_compare) +{ + return (!ft_strcmp((char *)to_find, (char *)to_compare)); +} + +void *lst_copy(void *to_copy) +{ + char *copy; + + copy = ft_memalloc(sizeof(char) * (ft_strlen(to_copy) + 1)); + copy = ft_strcpy(copy, to_copy); + return (copy); +} + +// +// ft_lstnew +// ft_lstadd_back +// ft_lstadd_front +// ft_lstclear +// ft_lstdelone +// ft_lstiter +// ft_lstlast +// ft_lstmap +// ft_lstsize +// +// ft_lstfind +// ft_lstremove +// ft_lstremove_next +// +// ft_lstcreate <-- +// ft_lstpush_back <-- +// ft_lstpush_front <-- +// ft_lstloop <-- +// ft_lstloop_back <-- +// ft_lstbegin <-- +// ft_lstend <-- +// ft_lstfind <-- +// ft_lstinsert <-- +// ft_lsterase <-- +// ft_lstfree <-- +// ft_lstlen <-- +// ft_lstcopy <-- +// + +int main(void) +{ + t_list *toto = NULL; + t_list *tmp; + char *str; + char *str2; + int c = 'f'; + +/////////////////////////////////////////////////////////// + printf("\npush back ------------\n"); + for (int i = 0; i < 5; i++) + { + str = ft_memalloc(2 * sizeof(char)); + str[1] = '\0'; + str[0] = c++; + ft_lstpush_back(&toto, ft_lstcreate(str)); + } + printf("print forward --------\n"); + ft_lstloop(toto, lst_print); + printf("print backward -------\n"); + ft_lstloop_back(ft_lstend(toto), lst_print); + printf("len ------------------\n"); + printf("%i\n", ft_lstlen(toto)); + +/////////////////////////////////////////////////////////// + printf("\npush front -----------\n"); + c = 'e'; + for (int i = 0; i < 5; i++) + { + str = ft_memalloc(2 * sizeof(char)); + str[1] = '\0'; + str[0] = c--; + ft_lstpush_front(&toto, ft_lstcreate(str)); + } + printf("print forward --------\n"); + ft_lstloop(toto, lst_print); + printf("print backward -------\n"); + ft_lstloop_back(ft_lstend(toto), lst_print); + printf("len ------------------\n"); + printf("%i\n", ft_lstlen(toto)); + +/////////////////////////////////////////////////////////// + printf("\nfind -----------------\n"); + tmp = ft_lstfind(toto, "e", lst_comp); + if (tmp) + lst_print(tmp->content); + +/////////////////////////////////////////////////////////// + printf("\ninsert ---------------\n"); + str = ft_memalloc(2 * sizeof(char)); + str[1] = '\0'; + str[0] = 'E'; + ft_lstinsert(tmp, ft_lstcreate(str)); + printf("print from insert ----\n"); + ft_lstloop(tmp, lst_print); + printf("print from begin -----\n"); + ft_lstloop(toto, lst_print); + printf("print backward -------\n"); + ft_lstloop_back(ft_lstend(toto), lst_print); + printf("len ------------------\n"); + printf("%i\n", ft_lstlen(toto)); + +/////////////////////////////////////////////////////////// + printf("\nerase ----------------\n"); + ft_lsterase(ft_lstfind(toto, "E", lst_comp), lst_delete); + printf("print forward --------\n"); + ft_lstloop(toto, lst_print); + printf("print backward -------\n"); + ft_lstloop_back(ft_lstend(toto), lst_print); + printf("len ------------------\n"); + printf("%i\n", ft_lstlen(toto)); + +/////////////////////////////////////////////////////////// + printf("\nfree -----------------\n"); + tmp = ft_lstfind(toto, "e", lst_comp); + ft_lstfree(tmp, lst_delete); + printf("print forward --------\n"); + ft_lstloop(toto, lst_print); + printf("print backward -------\n"); + ft_lstloop_back(ft_lstend(toto), lst_print); + printf("len ------------------\n"); + printf("%i\n", ft_lstlen(toto)); + +/////////////////////////////////////////////////////////// + printf("\ncopy -----------------\n"); + tmp = ft_lstcopy(toto, lst_copy); + printf("print forward toto ---\n"); + ft_lstloop(toto, lst_print); + printf("print forward tmp ----\n"); + ft_lstloop(tmp, lst_print); + printf("len tmp --------------\n"); + printf("%i\n", ft_lstlen(tmp)); + ft_lstfree(tmp, lst_delete); + +/////////////////////////////////////////////////////////// + ft_lstfree(toto, lst_delete); + + return (0); +} +