From f705f20fe8c8f82fa225bd5964e84900f434491b Mon Sep 17 00:00:00 2001 From: djonker Date: Sun, 29 Oct 2023 12:11:25 +0100 Subject: [PATCH] mouse movement now works :) --- cub3d.h | 5 ++++- src/keys.c | 35 ++++++++++++++++++++++++++++++++++- src/main.c | 4 +++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/cub3d.h b/cub3d.h index 1a6940e..3f27c58 100644 --- a/cub3d.h +++ b/cub3d.h @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 10:46:35 by houtworm #+# #+# */ -/* Updated: 2023/10/27 16:46:25 by djonker ######## odam.nl */ +/* Updated: 2023/10/29 11:50:54 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -51,6 +51,8 @@ typedef struct s_varlist int stepy; int hit; int side; + double oldmouseposx; + double oldmouseposy; int32_t fcolor; int32_t ccolor; char *northwt; @@ -71,6 +73,7 @@ void ft_movementkeys(t_varlist *vl); void keyhook(mlx_key_data_t kd, void *param); void scrollhook(double xdelta, double ydelta, void *param); void resizehook(int x, int y, void *param); +void cursorhook(double x, double y, void *param); // raycast.c void ft_raycast(t_varlist *vl); // draw.c diff --git a/src/keys.c b/src/keys.c index 660232a..9509148 100644 --- a/src/keys.c +++ b/src/keys.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 16:50:23 by houtworm #+# #+# */ -/* Updated: 2023/10/29 06:34:27 by houtworm ######## odam.nl */ +/* Updated: 2023/10/29 12:10:26 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -120,6 +120,39 @@ void scrollhook(double xdelta, double ydelta, void *param) ft_putendl("scroll down"); } +#include +void cursorhook(double xpos, double ypos, void *param) +{ + t_varlist *vl; + double olddirx; + double oldplanex; + double speed; + + vl = param; + ypos++; + if (xpos < vl->oldmouseposx) + { + speed = (vl->oldmouseposx - xpos) * vl->rotspeed / 20; + olddirx = vl->dirx; + vl->dirx = vl->dirx * cos(speed) - vl->diry * sin(speed); + vl->diry = olddirx * sin(speed) + vl->diry * cos(speed); + oldplanex = vl->planex; + vl->planex = vl->planex * cos(speed) - vl->planey * sin(speed); + vl->planey = oldplanex * sin(speed) + vl->planey * cos(speed); + } + if (xpos > vl->oldmouseposx) + { + speed = (xpos - vl->oldmouseposx) * vl->rotspeed / 20; + olddirx = vl->dirx; + vl->dirx = vl->dirx * cos(-speed) - vl->diry * sin(-speed); + vl->diry = olddirx * sin(-speed) + vl->diry * cos(-speed); + oldplanex = vl->planex; + vl->planex = vl->planex * cos(-speed) - vl->planey * sin(-speed); + vl->planey = oldplanex * sin(-speed) + vl->planey * cos(-speed); + } + vl->oldmouseposx = xpos; +} + void resizehook(int x, int y, void *param) { t_varlist *vl; diff --git a/src/main.c b/src/main.c index 674ff06..7beeb27 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: houtworm +#+ */ /* +#+ */ /* Created: 2023/10/26 14:13:07 by houtworm #+# #+# */ -/* Updated: 2023/10/27 14:59:03 by djonker ######## odam.nl */ +/* Updated: 2023/10/29 12:08:59 by houtworm ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -51,6 +51,8 @@ int main(int argc, char **argv) mlx_key_hook(vl.mlx, &keyhook, &vl); mlx_resize_hook(vl.mlx, &resizehook, &vl); mlx_scroll_hook(vl.mlx, &scrollhook, &vl); + mlx_set_cursor_mode(vl.mlx, MLX_MOUSE_DISABLED); + mlx_cursor_hook(vl.mlx, &cursorhook, &vl); mlx_loop_hook(vl.mlx, &mainloop, &vl); mlx_loop(vl.mlx); mlx_delete_image(vl.mlx, vl.img);