start project with header, struct, mlx functionnal, and basic key event action
This commit is contained in:
18
srcs/cube3d.c
Normal file
18
srcs/cube3d.c
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_game *game;
|
||||
|
||||
game = init_game();
|
||||
init_parsing(ac, av, game);
|
||||
|
||||
// receive a keypress event
|
||||
mlx_hook(game->win_ptr, 2, 1L << 0, keypress, game);
|
||||
// receive event when clicking the red button to close the window
|
||||
mlx_hook(game->win_ptr, 17, 1L << 17, shut_down, game);
|
||||
// infinite loop that waits for events to occurs
|
||||
mlx_loop(game->mlx_ptr);
|
||||
return (0);
|
||||
}
|
||||
|
||||
9
srcs/hook/key_do_action.c
Normal file
9
srcs/hook/key_do_action.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
int shut_down(t_game *game)
|
||||
{
|
||||
mlx_destroy_window(game->mlx_ptr, game->win_ptr);
|
||||
exit(0);
|
||||
free(game);
|
||||
return (0);
|
||||
}
|
||||
41
srcs/hook/key_is_action_1.c
Normal file
41
srcs/hook/key_is_action_1.c
Normal file
@@ -0,0 +1,41 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
int is_esc(int *k_hook)
|
||||
{
|
||||
if (!ft_arrintchr(k_hook, KEY_ESC, 3))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_go_left(int *k_hook)
|
||||
{
|
||||
if (!ft_arrintchr(k_hook, KEY_A, 3))
|
||||
return 1;
|
||||
if (!ft_arrintchr(k_hook, KEY_Q, 3))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_go_right(int *k_hook)
|
||||
{
|
||||
if (!ft_arrintchr(k_hook, KEY_D, 3))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_go_forward(int *k_hook)
|
||||
{
|
||||
if (!ft_arrintchr(k_hook, KEY_W, 3))
|
||||
return 1;
|
||||
if (!ft_arrintchr(k_hook, KEY_Z, 3))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int is_go_backward(int *k_hook)
|
||||
{
|
||||
if (!ft_arrintchr(k_hook, KEY_S, 3))
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
38
srcs/hook/keyhook.c
Normal file
38
srcs/hook/keyhook.c
Normal file
@@ -0,0 +1,38 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
// temp, to map all the keys on linux and mac
|
||||
static int print_keycode(int keycode)
|
||||
{
|
||||
ft_putnbrendl(keycode);
|
||||
return(0);
|
||||
}
|
||||
// temp end
|
||||
|
||||
static void keypress_action(int keycode, t_game *game)
|
||||
{
|
||||
// escape
|
||||
if (keycode == 65307)
|
||||
shut_down(game);
|
||||
// left
|
||||
if (keycode == 65361)
|
||||
(game->plr_x) -= 5;
|
||||
// right
|
||||
if (keycode == 65363)
|
||||
(game->plr_x) += 5;
|
||||
// up
|
||||
if (keycode == 65362)
|
||||
(game->plr_y) -= 5;
|
||||
// down
|
||||
if (keycode == 65364)
|
||||
(game->plr_y) += 5;
|
||||
mlx_pixel_put(game->mlx_ptr, game->win_ptr, game->plr_x, game->plr_y, 0x74db74);
|
||||
}
|
||||
|
||||
int keypress(int keycode, t_game *game)
|
||||
{
|
||||
// temp
|
||||
print_keycode(keycode);
|
||||
|
||||
keypress_action(keycode, game);
|
||||
return (0);
|
||||
}
|
||||
8
srcs/init/init_parsing.c
Normal file
8
srcs/init/init_parsing.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
void init_parsing(int ac, char **av, t_game *game)
|
||||
{
|
||||
(void)*game;
|
||||
(void)av;
|
||||
(void)ac;
|
||||
}
|
||||
23
srcs/init/init_struct.c
Normal file
23
srcs/init/init_struct.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include "cube3d.h"
|
||||
|
||||
t_game *init_game(void)
|
||||
{
|
||||
t_game *game;
|
||||
|
||||
game = malloc(sizeof(t_game));
|
||||
// player first position
|
||||
game->plr_x = 16;
|
||||
game->plr_y = 16;
|
||||
// size window
|
||||
game->win_size_x = 500;
|
||||
game->win_size_y = 500;
|
||||
// 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)
|
||||
ft_bzero(&game->k_hook, sizeof(game->k_hook));
|
||||
|
||||
return (game);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user