diff --git a/README.md b/README.md index d003282..2afcec4 100644 --- a/README.md +++ b/README.md @@ -2,4 +2,4 @@ This is a 3D mini game, using raycasting, in imitation of Wolfenstein3D or Doom. -![]() +![aller retour](assets/cube3d_aller_retour.gif) diff --git a/assets/cube3d_aller_retour.gif b/assets/cube3d_aller_retour.gif new file mode 100644 index 0000000..4dbc6c3 Binary files /dev/null and b/assets/cube3d_aller_retour.gif differ diff --git a/assets/cube3d_tour_2.gif b/assets/cube3d_tour_2.gif new file mode 100644 index 0000000..0d8d5f1 Binary files /dev/null and b/assets/cube3d_tour_2.gif differ diff --git a/headers/cube3d.h b/headers/cube3d.h index 793fac3..dd35752 100644 --- a/headers/cube3d.h +++ b/headers/cube3d.h @@ -30,4 +30,7 @@ # include "cube3d_proto.h" # include "get_next_line.h" +# include // for time +# include // for gettimeofday + #endif diff --git a/headers/cube3d_macro.h b/headers/cube3d_macro.h index b201c99..a25bcca 100644 --- a/headers/cube3d_macro.h +++ b/headers/cube3d_macro.h @@ -18,7 +18,7 @@ */ /* nbr pixel player move */ -# define PLR_MV 4 +# define PLR_MV 10 /* degree of rotation of the player per press */ # define PLR_ROT 1 /* nbr key you can press at the same time */ @@ -33,10 +33,9 @@ # define SCREEN_FOCAL 70 /* size of a cell on the map */ # define CELL 100 +// minimum time in milliseconds between two keypress +# define DEBOUNCE_TIME 500 -/* - * keys macro - */ /* * keys macro diff --git a/headers/cube3d_struct.h b/headers/cube3d_struct.h index 83b60f5..96b02dd 100644 --- a/headers/cube3d_struct.h +++ b/headers/cube3d_struct.h @@ -162,15 +162,16 @@ typedef struct s_plr typedef struct s_game { - void *mlx_ptr; - t_win win; - t_img img; - t_plr plr; - t_rcast rcast; - t_txt txt; - t_map map; - int fd; - int k_hook[MAX_NB_KEY]; + void *mlx_ptr; + t_win win; + t_img img; + t_plr plr; + t_rcast rcast; + t_txt txt; + t_map map; + int fd; + int k_hook[MAX_NB_KEY]; + struct timeval last_keypress_time; } t_game; #endif diff --git a/srcs/hook/keyhook.c b/srcs/hook/keyhook.c index 5457b88..8fb5911 100644 --- a/srcs/hook/keyhook.c +++ b/srcs/hook/keyhook.c @@ -21,9 +21,31 @@ static int print_keycode(int keycode, t_plr *plr) return (0); } + +int should_ignore_keypress(const struct timeval *current_time, t_game *game) +{ + int is_less; + unsigned long current_milliseconds; + unsigned long last_milliseconds; + + current_milliseconds = (current_time->tv_sec % 1000) * 1000 + current_time->tv_usec / 1000; + last_milliseconds = (game->last_keypress_time.tv_sec % 1000) * 1000 + game->last_keypress_time.tv_usec / 1000; + is_less = (current_milliseconds - last_milliseconds) < DEBOUNCE_TIME; + + if (!is_less) + game->last_keypress_time = *current_time; + return is_less; +} + + int hook_action(t_game *game) { int is_action; + struct timeval current_time; + + gettimeofday(¤t_time, NULL); + if (should_ignore_keypress(¤t_time, game)) + return (0); is_action = 0; if (is_esc(game->k_hook, &is_action)) diff --git a/srcs/init/init_game.c b/srcs/init/init_game.c index e2ff82c..4e1f35a 100644 --- a/srcs/init/init_game.c +++ b/srcs/init/init_game.c @@ -66,7 +66,7 @@ void init_game(t_game *game) game->win.size_x = SCREEN_WIDTH; game->win.size_y = SCREEN_HEIGHT; game->win.ptr = mlx_new_window(game->mlx_ptr, game->win.size_x, \ - game->win.size_y, "cub3d"); + game->win.size_y, "cube3d"); init_img(&(game->img), game->mlx_ptr, &game->win); init_txtr(&game->txt, game->mlx_ptr); ft_bzero(&game->k_hook, sizeof(game->k_hook));