From 6f8c00007578e277b2d9be107d9db01eef1c8268 Mon Sep 17 00:00:00 2001 From: narnaud Date: Wed, 1 Jun 2022 13:07:14 +0200 Subject: [PATCH] add: move by keeping keys pressed --- includes/cub3d.h | 11 ++++++- sources/main.c | 80 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 67 insertions(+), 24 deletions(-) diff --git a/includes/cub3d.h b/includes/cub3d.h index 3129035..390db2c 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:24:12 by narnaud #+# #+# */ -/* Updated: 2022/05/31 18:59:46 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 12:44:59 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,6 +61,14 @@ typedef struct s_rectangle int color; } t_rectangle; +typedef struct s_control +{ + int up; + int down; + int left; + int right; +} t_control; + typedef struct s_env { void *mlx; @@ -70,6 +78,7 @@ typedef struct s_env int line_bytes; int endian; int *buffer; + t_control control; char *wallTexture[4]; int floorColor; int ceilColor; diff --git a/sources/main.c b/sources/main.c index 45c2443..fb016d4 100644 --- a/sources/main.c +++ b/sources/main.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/21 19:22:50 by narnaud #+# #+# */ -/* Updated: 2022/05/31 17:13:28 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 13:04:31 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,42 +27,74 @@ void init_window(t_env *env) env->win = window; } -int key_hook_primary(int keycode, void *param) +int key_press_hook(int keycode, void *param) +{ + t_env *env; + + env = (t_env *)param; + if (keycode == KEY_W) + env->control.up = 1; + if (keycode == KEY_S) + env->control.down = 1; + if (keycode == KEY_A) + env->control.left = 1; + if (keycode == KEY_D) + env->control.right = 1; + return (1); +} + +int key_release_hook(int keycode, void *param) +{ + t_env *env; + + env = (t_env *)param; + if (keycode == KEY_W) + env->control.up = 0; + if (keycode == KEY_S) + env->control.down = 0; + if (keycode == KEY_A) + env->control.left = 0; + if (keycode == KEY_D) + env->control.right = 0; + return (1); +} + +int update_hook(void *param) { t_env *env; env = (t_env *)param; - if (keycode == KEY_ARROW_UP) + if (env->control.up) { - if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + env->playerDir.x / 5)] == '0') - set_vec(&env->playerPos, env->playerPos.x + env->playerDir.x / 5, env->playerPos.y); - if (env->map[(int)(env->playerPos.y + env->playerDir.y / 5)][(int)env->playerPos.x] == '0') - set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + env->playerDir.y / 5); + if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + env->playerDir.x / 10)] == '0') + set_vec(&env->playerPos, env->playerPos.x + env->playerDir.x / 10, env->playerPos.y); + if (env->map[(int)(env->playerPos.y + env->playerDir.y / 10)][(int)env->playerPos.x] == '0') + set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + env->playerDir.y / 10); } - else if (keycode == KEY_ARROW_DOWN) + if (env->control.down) { - if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x - env->playerDir.x / 5)] == '0') - set_vec(&env->playerPos, env->playerPos.x - env->playerDir.x / 5, env->playerPos.y); - if (env->map[(int)(env->playerPos.y - env->playerDir.y / 5)][(int)env->playerPos.x] == '0') - set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y - env->playerDir.y / 5); + if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x - env->playerDir.x / 10)] == '0') + set_vec(&env->playerPos, env->playerPos.x - env->playerDir.x / 10, env->playerPos.y); + if (env->map[(int)(env->playerPos.y - env->playerDir.y / 10)][(int)env->playerPos.x] == '0') + set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y - env->playerDir.y / 10); } - if (keycode == KEY_ARROW_LEFT) + if (env->control.left) { set_vec(&env->playerDir, - cos(-M_PI / 9) * env->playerDir.x - sin(-M_PI / 9) * env->playerDir.y, - sin(-M_PI / 9) * env->playerDir.x + cos(-M_PI / 9) * env->playerDir.y); + cos(-M_PI / 36) * env->playerDir.x - sin(-M_PI / 36) * env->playerDir.y, + sin(-M_PI / 36) * env->playerDir.x + cos(-M_PI / 36) * env->playerDir.y); set_vec(&env->camPlan, - cos(-M_PI / 9) * env->camPlan.x - sin(-M_PI / 9) * env->camPlan.y, - sin(-M_PI / 9) * env->camPlan.x + cos(-M_PI / 9) * env->camPlan.y); + cos(-M_PI / 36) * env->camPlan.x - sin(-M_PI / 36) * env->camPlan.y, + sin(-M_PI / 36) * env->camPlan.x + cos(-M_PI / 36) * env->camPlan.y); } - else if (keycode == KEY_ARROW_RIGHT) + if (env->control.right) { set_vec(&env->playerDir, - cos(M_PI / 9) * env->playerDir.x - sin(M_PI / 9) * env->playerDir.y, - sin(M_PI / 9) * env->playerDir.x + cos(M_PI / 9) * env->playerDir.y); + cos(M_PI / 36) * env->playerDir.x - sin(M_PI / 36) * env->playerDir.y, + sin(M_PI / 36) * env->playerDir.x + cos(M_PI / 36) * env->playerDir.y); set_vec(&env->camPlan, - cos(M_PI / 9) * env->camPlan.x - sin(M_PI / 9) * env->camPlan.y, - sin(M_PI / 9) * env->camPlan.x + cos(M_PI / 9) * env->camPlan.y); + cos(M_PI / 36) * env->camPlan.x - sin(M_PI / 36) * env->camPlan.y, + sin(M_PI / 36) * env->camPlan.x + cos(M_PI / 36) * env->camPlan.y); } if (DEBUG) { @@ -97,7 +129,9 @@ int main(int argc, char **argv) } init_window(env); render(env); - mlx_key_hook(env->win, key_hook_primary, env); + mlx_hook(env->win, 2, 1L<<0, key_press_hook, env); + mlx_hook(env->win, 3, 1L<<1, key_release_hook, env); + mlx_loop_hook(env->mlx, update_hook, env); mlx_loop(env->mlx); return (EXIT_SUCCESS); }