improve gexit with init function and add basic mlx image gestion

This commit is contained in:
Hugo LAMY
2022-03-25 18:49:06 +01:00
parent 3a1036ccdc
commit 02b5d88ea5
45 changed files with 139 additions and 7042 deletions

View File

@@ -1,18 +1,36 @@
#include "cube3d.h"
void destroy_mlx(void *param)
{
t_game *game;
game = 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");
return (0);
}
int main(int ac, char **av)
{
t_game *game;
game = init_game();
init_parsing(ac, av, game);
gexit_init(destroy_mlx, game);
// receive a keypress event
mlx_hook(game->win_ptr, 2, 1L << 0, keypress, game);
// receive a keyprelease event
mlx_hook(game->win_ptr, 3, 1L << 1, keyrelease, game);
// receive event when clicking the red button to close the window
mlx_hook(game->win_ptr, 17, 1L << 17, shut_down, game);
mlx_hook(game->win_ptr, 17, 1L << 17, shut_down, NULL);
// infinite loop that waits for events to occurs
mlx_loop(game->mlx_ptr);
return (0);

15
srcs/draw/draw.c Normal file
View File

@@ -0,0 +1,15 @@
#include "cube3d.h"
static void draw_pixel(t_game *game, int x, int y, int color)
{
unsigned int position;
position = y * game->sizel + x * (game->bpp / 8);
*(unsigned int*)(game->img_data + position) = color;
}
void draw(t_game *game)
{
draw_pixel(game, game->plr_x, game->plr_y, 0x00FF0000);
mlx_put_image_to_window(game->mlx_ptr, game->win_ptr, game->img_ptr, 0, 0);
}

View File

@@ -1,13 +1,5 @@
#include "cube3d.h"
int shut_down(t_game *game)
{
mlx_destroy_window(game->mlx_ptr, game->win_ptr);
gexit(B_RED"close windows"RESET"\n");
exit(0);
return (0);
}
// temp, to test keypress hook
void keypress_do_action(t_game *game)
{
@@ -21,7 +13,7 @@ void keypress_do_action(t_game *game)
(game->plr_y) -= 5;
if (is_go_backward(game->k_hook))
(game->plr_y) += 5;
mlx_pixel_put(game->mlx_ptr, game->win_ptr, game->plr_x, game->plr_y, 0x74db74);
draw(game);
}

View File

@@ -14,10 +14,15 @@ t_game *init_game(void)
// init connexion to server
game->mlx_ptr = mlx_init();
// create the window
game->win_ptr = mlx_new_window(game->mlx_ptr, game->win_size_x, game->win_size_y, "test");
// fill k_hook with zeros (key_hook, the array containing the values of key press)
game->win_ptr = mlx_new_window(game->mlx_ptr, game->win_size_x,
game->win_size_y, "test");
// k(ey)_hook is the array containing the values of key press events
ft_bzero(&game->k_hook, sizeof(game->k_hook));
// create image and get its data address
game->img_ptr = mlx_new_image(game->mlx_ptr, game->win_size_x,
game->win_size_y);
game->img_data = mlx_get_data_addr(game->img_ptr, &(game->bpp),
&(game->sizel), &(game->endian));
return (game);
}

View File

@@ -1,10 +1,43 @@
#include "cube3d.h"
static int comp_addr(void *to_find, void *to_compare)
/*
* gexit_init
*
*/
//-------------------------------
// gexit function
//-------------------------------
typedef void(*func_to_gexit)(void*);
typedef struct s_exit
{
return (to_find == to_compare);
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;
@@ -12,6 +45,9 @@ t_list **alloc_lst()
return (&lst);
}
//-------------------------------
// alloc, free, exit functions
//-------------------------------
void *gmalloc(size_t size)
{
void *tmp;
@@ -26,6 +62,11 @@ void *gmalloc(size_t size)
return (tmp);
}
static int comp_addr(void *to_find, void *to_compare)
{
return (to_find == to_compare);
}
void gfree(void *addr)
{
t_list **lst;
@@ -41,8 +82,12 @@ void gfree(void *addr)
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);