memorybook functionnal, with add
This commit is contained in:
4
Makefile
4
Makefile
@@ -19,7 +19,8 @@ D_SRCS = srcs \
|
||||
|
||||
SRCS = cube3d.c \
|
||||
\
|
||||
g_alloc_free_exit.c \
|
||||
memorybook.c \
|
||||
memorybook_utils.c \
|
||||
\
|
||||
init_struct.c \
|
||||
init_parsing.c \
|
||||
@@ -36,6 +37,7 @@ HEADERS = cube3d.h \
|
||||
cube3d_proto.h \
|
||||
cube3d_macro.h \
|
||||
cube3d_struct.h \
|
||||
memorybook.h \
|
||||
colors.h
|
||||
|
||||
INCLUDES = -I$(D_HEADERS) -I$(D_LFT) -I$(D_LMLX)
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
# include <stdio.h> // for printf()
|
||||
|
||||
# include "colors.h"
|
||||
# include "memorybook.h"
|
||||
|
||||
# include "cube3d_macro.h"
|
||||
# include "cube3d_struct.h"
|
||||
|
||||
@@ -45,14 +45,4 @@ int is_go_backward(int *k_hook);
|
||||
// draw.c
|
||||
void draw(t_game *game);
|
||||
|
||||
// -------------------------------
|
||||
// SRC/MEM
|
||||
// -------------------------------
|
||||
// g_alloc_free_exit.c
|
||||
void gexit_init(void(*f)(void*), void *param);
|
||||
void *gmalloc(size_t size);
|
||||
void gfree(void *addr);
|
||||
void gexit(char *str);
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
18
headers/memorybook.h
Normal file
18
headers/memorybook.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#ifndef MEMORYBOOK_H
|
||||
# define MEMORYBOOK_H
|
||||
|
||||
typedef void(*t_mb_func_exit)(void*);
|
||||
|
||||
typedef struct s_exit
|
||||
{
|
||||
t_mb_func_exit f;
|
||||
void *param;
|
||||
} t_exit;
|
||||
|
||||
void mb_init(void(*f)(void*), void *param);
|
||||
void *mb_alloc(size_t size);
|
||||
void mb_add(void *addr);
|
||||
void mb_free(void *addr);
|
||||
void mb_exit(char *str);
|
||||
|
||||
#endif
|
||||
@@ -8,12 +8,11 @@ void destroy_mlx(void *param)
|
||||
mlx_destroy_image(game->mlx_ptr, game->img_ptr);
|
||||
mlx_destroy_window(game->mlx_ptr, game->win_ptr);
|
||||
mlx_destroy_display(game->mlx_ptr);
|
||||
free(game->mlx_ptr);
|
||||
}
|
||||
|
||||
int shut_down()
|
||||
{
|
||||
gexit(B_RED"close windows"RESET"\n");
|
||||
mb_exit(B_RED"close windows"RESET"\n");
|
||||
return (0);
|
||||
}
|
||||
|
||||
@@ -23,7 +22,7 @@ int main(int ac, char **av)
|
||||
|
||||
game = init_game();
|
||||
init_parsing(ac, av, game);
|
||||
gexit_init(destroy_mlx, game);
|
||||
mb_init(destroy_mlx, game);
|
||||
|
||||
// receive a keypress event
|
||||
mlx_hook(game->win_ptr, 2, 1L << 0, keypress, game);
|
||||
@@ -35,4 +34,3 @@ int main(int ac, char **av)
|
||||
mlx_loop(game->mlx_ptr);
|
||||
return (0);
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ t_game *init_game(void)
|
||||
{
|
||||
t_game *game;
|
||||
|
||||
game = gmalloc(sizeof(t_game));
|
||||
game = mb_alloc(sizeof(t_game));
|
||||
// player first position
|
||||
game->plr_x = 16;
|
||||
game->plr_y = 16;
|
||||
@@ -13,6 +13,7 @@ t_game *init_game(void)
|
||||
game->win_size_y = 500;
|
||||
// init connexion to server
|
||||
game->mlx_ptr = mlx_init();
|
||||
mb_add(game->mlx_ptr);
|
||||
// create the window
|
||||
game->win_ptr = mlx_new_window(game->mlx_ptr, game->win_size_x,
|
||||
game->win_size_y, "test");
|
||||
|
||||
@@ -1,94 +0,0 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
/*
|
||||
* gexit_init
|
||||
*
|
||||
*/
|
||||
|
||||
//-------------------------------
|
||||
// gexit function
|
||||
//-------------------------------
|
||||
typedef void(*func_to_gexit)(void*);
|
||||
|
||||
typedef struct s_exit
|
||||
{
|
||||
func_to_gexit f;
|
||||
void *param;
|
||||
} t_exit;
|
||||
|
||||
|
||||
t_exit **exit_func()
|
||||
{
|
||||
static t_exit *texit = NULL;
|
||||
|
||||
if (!texit)
|
||||
texit = gmalloc(sizeof(t_exit));
|
||||
return (&texit);
|
||||
}
|
||||
|
||||
void gexit_init(void(*f)(void*), void *param)
|
||||
{
|
||||
t_exit **texit;
|
||||
|
||||
texit = exit_func();
|
||||
(*texit)->param = param;
|
||||
(*texit)->f = f;
|
||||
}
|
||||
|
||||
//-------------------------------
|
||||
// allocation list
|
||||
//-------------------------------
|
||||
t_list **alloc_lst()
|
||||
{
|
||||
static t_list *lst = NULL;
|
||||
|
||||
return (&lst);
|
||||
}
|
||||
|
||||
//-------------------------------
|
||||
// alloc, free, exit functions
|
||||
//-------------------------------
|
||||
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);
|
||||
}
|
||||
|
||||
static int comp_addr(void *to_find, void *to_compare)
|
||||
{
|
||||
return (to_find == to_compare);
|
||||
}
|
||||
|
||||
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;
|
||||
t_exit **texit;
|
||||
|
||||
lst = alloc_lst();
|
||||
texit = exit_func();
|
||||
if ((*texit)->f)
|
||||
(*texit)->f((*texit)->param);
|
||||
ft_putstr_fd(str, 2);
|
||||
ft_lstfree((*lst), free);
|
||||
exit(0);
|
||||
}
|
||||
64
srcs/mem/memorybook.c
Normal file
64
srcs/mem/memorybook.c
Normal file
@@ -0,0 +1,64 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
t_exit **mb_exit_func();
|
||||
t_list **mb_lst();
|
||||
int mb_comp_addr(void *to_find, void *to_compare);
|
||||
|
||||
void mb_init(void(*f)(void*), void *param)
|
||||
{
|
||||
t_exit **texit;
|
||||
|
||||
texit = mb_exit_func();
|
||||
(*texit)->param = param;
|
||||
(*texit)->f = f;
|
||||
}
|
||||
|
||||
void *mb_alloc(size_t size)
|
||||
{
|
||||
void *tmp;
|
||||
t_list **lst;
|
||||
|
||||
lst = mb_lst();
|
||||
tmp = ft_memalloc(size);
|
||||
if (!tmp)
|
||||
mb_exit(B_RED"failed create new allocation"RESET"\n");
|
||||
if (!ft_lstpush_back(lst, ft_lstcreate(tmp)))
|
||||
mb_exit(B_RED"failed add new element to list"RESET"\n");
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
void mb_add(void *addr)
|
||||
{
|
||||
t_list **lst;
|
||||
|
||||
lst = mb_lst();
|
||||
if (!ft_lstpush_back(lst, ft_lstcreate(addr)))
|
||||
mb_exit(B_RED"failed add new element to list"RESET"\n");
|
||||
}
|
||||
|
||||
void mb_free(void *addr)
|
||||
{
|
||||
t_list **lst;
|
||||
t_list *tmp;
|
||||
|
||||
lst = mb_lst();
|
||||
tmp = ft_lstfind((*lst), addr, mb_comp_addr);
|
||||
if (!tmp)
|
||||
ft_putstr_fd(B_RED"failed free element"RESET"\n", 2);
|
||||
ft_lsterase(tmp, free);
|
||||
}
|
||||
|
||||
void mb_exit(char *str)
|
||||
{
|
||||
t_list **lst;
|
||||
t_exit **texit;
|
||||
|
||||
lst = mb_lst();
|
||||
texit = mb_exit_func();
|
||||
if ((*texit)->f)
|
||||
(*texit)->f((*texit)->param);
|
||||
ft_putstr_fd(str, 2);
|
||||
ft_lstfree((*lst), free);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
23
srcs/mem/memorybook_utils.c
Normal file
23
srcs/mem/memorybook_utils.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
t_exit **mb_exit_func()
|
||||
{
|
||||
static t_exit *texit = NULL;
|
||||
|
||||
if (!texit)
|
||||
texit = mb_alloc(sizeof(t_exit));
|
||||
return (&texit);
|
||||
}
|
||||
|
||||
t_list **mb_lst()
|
||||
{
|
||||
static t_list *lst = NULL;
|
||||
|
||||
return (&lst);
|
||||
}
|
||||
|
||||
int mb_comp_addr(void *to_find, void *to_compare)
|
||||
{
|
||||
return (to_find == to_compare);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user