start project with header, struct, mlx functionnal, and basic key event action

This commit is contained in:
hugogogo
2022-03-23 14:27:36 +01:00
parent 1959a1a9a3
commit 95bd304708
11 changed files with 228 additions and 1 deletions

14
headers/cube3d.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef CUBE3D_H
# define CUBE3D_H
#include "../libs/libft/includes/libft.h"
#include <mlx.h>
#include <unistd.h> // for sleep()
#include <stdlib.h> // for atoi()
#include <stdio.h> // for printf()
#include "cube3d_macro.h"
#include "cube3d_struct.h"
#include "cube3d_proto.h"
#endif

21
headers/cube3d_macro.h Normal file
View File

@@ -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

40
headers/cube3d_proto.h Normal file
View File

@@ -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

15
headers/cube3d_struct.h Normal file
View File

@@ -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

18
srcs/cube3d.c Normal file
View 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);
}

View 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);
}

View 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
View 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
View 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
View 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);
}