diff --git a/headers/cube3d.h b/headers/cube3d.h new file mode 100644 index 0000000..a95cff0 --- /dev/null +++ b/headers/cube3d.h @@ -0,0 +1,14 @@ +#ifndef CUBE3D_H +# define CUBE3D_H + +#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" + +#endif diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h new file mode 100644 index 0000000..d848f36 --- /dev/null +++ b/headers/cube3d_macro.h @@ -0,0 +1,21 @@ +#ifndef CUBE3D_MACRO_H +# define CUBE3D_MACRO_H + +# define MAX_NB_KEY 3 + +# define ARROW_LEFT 65361 +# define ARROW_UP 65362 +# define ARROW_RIGHT 65363 +# define ARROW_DOWN 65364 +# define KEY_A 97 // left +# define KEY_Q 113 // left (azerty) +# define KEY_D 100 // right +# define KEY_S 115 // backward +# define KEY_W 119 // forward +# define KEY_Z 122 // forward (azerty) +# define KEY_ESC 65307 +# define KEY_SHIFT_LEFT 65505 +# define KEY_SHIFT_RIGHT 65506 +# define KEY_SPACE 32 + +#endif diff --git a/headers/cube3d_proto.h b/headers/cube3d_proto.h new file mode 100644 index 0000000..c216223 --- /dev/null +++ b/headers/cube3d_proto.h @@ -0,0 +1,40 @@ +#ifndef CUBE3D_PROTO_H +# define CUBE3D_PROTO_H + +// ------------------------------- +// SRC + + +// ------------------------------- +// SRC/INIT + +// init_struct.c +t_game *init_game(void); + +// init_parsing.c +void init_parsing(int ac, char **av, t_game *game); + + +// ------------------------------- +// SRC/PARSING + + +// ------------------------------- +// SRC/HOOK + +// key_hook.c +int keypress(int keycode, t_game *game); + +// key_do_action.c +int shut_down(t_game *game); + + +// ------------------------------- +// SRC/DRAW + + +// ------------------------------- +// SRC/FREE + + +#endif diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h new file mode 100644 index 0000000..99984a3 --- /dev/null +++ b/headers/cube3d_struct.h @@ -0,0 +1,15 @@ +#ifndef CUBE3D_STRUCT_H +# define CUBE3D_STRUCT_H + +typedef struct s_game +{ + void *mlx_ptr; + void *win_ptr; + int plr_x; + int plr_y; + int win_size_x; + int win_size_y; + int k_hook[MAX_NB_KEY]; +} t_game; + +#endif diff --git a/libs/libft b/libs/libft index d622367..39b3f2b 160000 --- a/libs/libft +++ b/libs/libft @@ -1 +1 @@ -Subproject commit d622367cfdc547b2d6cc3a7bfaaa7ed7a1301648 +Subproject commit 39b3f2bc4ea6aa99ef7df5eb3239055bfe13ecd5 diff --git a/srcs/cube3d.c b/srcs/cube3d.c new file mode 100644 index 0000000..5985ea4 --- /dev/null +++ b/srcs/cube3d.c @@ -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); +} + diff --git a/srcs/hook/key_do_action.c b/srcs/hook/key_do_action.c new file mode 100644 index 0000000..67dafd2 --- /dev/null +++ b/srcs/hook/key_do_action.c @@ -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); +} diff --git a/srcs/hook/key_is_action_1.c b/srcs/hook/key_is_action_1.c new file mode 100644 index 0000000..860a656 --- /dev/null +++ b/srcs/hook/key_is_action_1.c @@ -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; +} + diff --git a/srcs/hook/keyhook.c b/srcs/hook/keyhook.c new file mode 100644 index 0000000..f75b3df --- /dev/null +++ b/srcs/hook/keyhook.c @@ -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); +} diff --git a/srcs/init/init_parsing.c b/srcs/init/init_parsing.c new file mode 100644 index 0000000..b9d81d9 --- /dev/null +++ b/srcs/init/init_parsing.c @@ -0,0 +1,8 @@ +#include "cube3d.h" + +void init_parsing(int ac, char **av, t_game *game) +{ + (void)*game; + (void)av; + (void)ac; +} diff --git a/srcs/init/init_struct.c b/srcs/init/init_struct.c new file mode 100644 index 0000000..2c25a4b --- /dev/null +++ b/srcs/init/init_struct.c @@ -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); +} +