From 6ecded8d721b5c5ae8838b28b27f27d6dc6d596e Mon Sep 17 00:00:00 2001 From: narnaud Date: Wed, 1 Jun 2022 14:28:36 +0200 Subject: [PATCH] rework of controls --- Makefile | 2 +- includes/cub3d.h | 10 ++++-- sources/hooks.c | 53 ++++++++++++++++++++++++++++++++ sources/main.c | 80 +----------------------------------------------- sources/render.c | 5 +-- sources/utils.c | 8 ++++- 6 files changed, 73 insertions(+), 85 deletions(-) create mode 100644 sources/hooks.c diff --git a/Makefile b/Makefile index 0fc9538..da5eaa4 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ LIBFT = libft.a MLX = includes/mlx.h SRCS = sources/main.c sources/parsing.c sources/getline.c sources/utils.c -SRCS += sources/render.c +SRCS += sources/render.c sources/hooks.c OBJS = ${SRCS:.c=.o} CC = gcc diff --git a/includes/cub3d.h b/includes/cub3d.h index 390db2c..0b1dd1a 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/06/01 12:44:59 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 14:23:18 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,7 +78,8 @@ typedef struct s_env int line_bytes; int endian; int *buffer; - t_control control; + int minimap; + int controls[256]; char *wallTexture[4]; int floorColor; int ceilColor; @@ -94,9 +95,14 @@ void render_minimap(t_env *env); void render_view(t_env *env); void render(t_env *env); +int key_press_hook(int keycode, t_env *env); +int key_release_hook(int keycode, t_env *env); +int update_hook(t_env *env); + int rgb_to_int(char **rgb); void set_vec(t_vec_d *vec, double x, double y); double vec_len(t_vec_d vec); +void rot_vec(t_vec_d *vec, double rad); t_env *parse_envFile(char *filename); char *get_next_line(int fd); diff --git a/sources/hooks.c b/sources/hooks.c new file mode 100644 index 0000000..3382ab5 --- /dev/null +++ b/sources/hooks.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hooks.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/06/01 13:47:30 by narnaud #+# #+# */ +/* Updated: 2022/06/01 14:26:26 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../includes/cub3d.h" + +int key_press_hook(int keycode, t_env *env) +{ + if (keycode == KEY_M) + env->minimap = !env->minimap; + else + env->controls[keycode] = 1; + return (1); +} + +int key_release_hook(int keycode, t_env *env) +{ + env->controls[keycode] = 0; + return (1); +} + +int update_hook(t_env *env) +{ + double dX; + double dY; + + dX = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.x / 10; + dY = (env->controls[KEY_W] - env->controls[KEY_S]) * env->playerDir.y / 10; + + if (env->map[(int)env->playerPos.y][(int)(env->playerPos.x + dX)] == '0') + set_vec(&env->playerPos, env->playerPos.x + dX, env->playerPos.y); + if (env->map[(int)(env->playerPos.y + dY)][(int)env->playerPos.x] == '0') + set_vec(&env->playerPos, env->playerPos.x, env->playerPos.y + dY); + rot_vec(&env->playerDir, + (double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36); + rot_vec(&env->camPlan, + (double)(env->controls[KEY_D] - env->controls[KEY_A]) / 36); + if (DEBUG) + { + printf("playerPos: %f, %f. playerDir: %f, %f\n", env->playerPos.x, env->playerPos.y, env->playerDir.x, env->playerDir.y); + printf("camPlan: %f, %f\n", env->camPlan.x, env->camPlan.y); + } + render(env); + return (1); +} diff --git a/sources/main.c b/sources/main.c index fb016d4..ccfbc32 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/06/01 13:04:31 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 13:49:31 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,84 +27,6 @@ void init_window(t_env *env) env->win = window; } -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 (env->control.up) - { - 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 (env->control.down) - { - 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 (env->control.left) - { - set_vec(&env->playerDir, - 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 / 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 (env->control.right) - { - set_vec(&env->playerDir, - 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 / 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) - { - printf("playerPos: %f, %f. playerDir: %f, %f\n", env->playerPos.x, env->playerPos.y, env->playerDir.x, env->playerDir.y); - printf("camPlan: %f, %f\n", env->camPlan.x, env->camPlan.y); - } - render(env); - return (1); -} - int main(int argc, char **argv) { t_env *env; diff --git a/sources/render.c b/sources/render.c index 6292744..8511dba 100644 --- a/sources/render.c +++ b/sources/render.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/31 09:33:02 by narnaud #+# #+# */ -/* Updated: 2022/06/01 12:22:39 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 14:06:23 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -186,7 +186,8 @@ void render(t_env *env) &env->pixel_bits, &env->line_bytes, &env->endian); env->line_bytes /= 4; render_view(env); - render_minimap(env); + if (env->minimap) + render_minimap(env); mlx_put_image_to_window(env->mlx, env->win, env->img, 0, 0); mlx_destroy_image(env->mlx, env->img); } diff --git a/sources/utils.c b/sources/utils.c index db24837..4db51be 100644 --- a/sources/utils.c +++ b/sources/utils.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/31 08:59:01 by narnaud #+# #+# */ -/* Updated: 2022/05/31 12:22:41 by narnaud ### ########.fr */ +/* Updated: 2022/06/01 14:21:41 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,6 +23,12 @@ void set_vec(t_vec_d *vec, double x, double y) vec->y = y; } +void rot_vec(t_vec_d *vec, double rad) +{ + vec->x = cos(M_PI * rad) * vec->x - sin(M_PI * rad) * vec->y; + vec->y = sin(M_PI * rad) * vec->x + cos(M_PI * rad) * vec->y; +} + int rgb_to_int(char **rgb) { int ret;