From 9b3379c37eb43e5aacc9fec3e2e465a405f024e0 Mon Sep 17 00:00:00 2001 From: Jean Teissier Date: Mon, 2 Sep 2024 12:17:40 +0200 Subject: [PATCH 1/3] Added mousemove_events() function to trap the cursor at the centre of the screen and handle the camera move. Added corresponding mlx_hook() call with MotionNotify and PointerMotionMask parameters in start_game --- Makefile | 8 ++++---- includes/cub3d.h | 5 +++-- srcs/events/key_events.c | 10 +++++----- srcs/events/mouse_events.c | 25 +++++++++++++++++++++++++ srcs/main.c | 5 +++-- 5 files changed, 40 insertions(+), 13 deletions(-) create mode 100644 srcs/events/mouse_events.c diff --git a/Makefile b/Makefile index ae58812..b2fd125 100644 --- a/Makefile +++ b/Makefile @@ -3,10 +3,10 @@ # ::: :::::::: # # Makefile :+: :+: :+: # # +:+ +:+ +:+ # -# By: tsuchen +#+ +:+ +#+ # +# By: jteissie +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/08/15 12:02:08 by tsuchen #+# #+# # -# Updated: 2024/09/02 11:11:09 by tsuchen ### ########.fr # +# Updated: 2024/09/02 11:59:32 by jteissie ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,7 @@ NAME = cub3d SRCS_M = main.c \ game_init.c \ cleanup.c \ - color_utils.c + color_utils.c SRCS_PS = parser.c \ @@ -24,7 +24,7 @@ SRCS_PS = parser.c \ get_textures.c \ get_textures_utils.c -SRCS_EV = key_events.c wall_collision.c +SRCS_EV = key_events.c wall_collision.c mouse_events.c SRCS_MAP = map.c diff --git a/includes/cub3d.h b/includes/cub3d.h index f992a18..c7f8475 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* cub3d.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ +/* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 11:12:53 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 12:05:23 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -125,6 +125,7 @@ typedef struct s_data int cleanup(t_data *data); int game_init(t_data *data); int key_events(int keycode, t_data *data); +int mousemove_events(int x, int y, t_data *data); void move_check(t_vec *step, t_data *data, int add_or_sub); /* color utils*/ int create_trgb(int t, int r, int g, int b); diff --git a/srcs/events/key_events.c b/srcs/events/key_events.c index a9530bc..df7e89f 100644 --- a/srcs/events/key_events.c +++ b/srcs/events/key_events.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* key_events.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ +/* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 18:40:50 by jteissie #+# #+# */ -/* Updated: 2024/09/02 11:24:41 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 11:59:01 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ static void move_player(t_data *data, t_move dir) return ; } -static void move_camera(t_data *data, t_rot dir) +static void key_move_camera(t_data *data, t_rot dir) { if (dir == CLOCK) { @@ -57,9 +57,9 @@ static void move_camera(t_data *data, t_rot dir) int key_events(int keycode, t_data *data) { if (keycode == ARROW_RIGHT) - move_camera(data, CLOCK); + key_move_camera(data, CLOCK); else if (keycode == ARROW_LEFT) - move_camera(data, CCLOCK); + key_move_camera(data, CCLOCK); else if (keycode == W_KEY) move_player(data, FORWARD); else if (keycode == S_KEY) diff --git a/srcs/events/mouse_events.c b/srcs/events/mouse_events.c new file mode 100644 index 0000000..621a5a7 --- /dev/null +++ b/srcs/events/mouse_events.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mouse_events.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jteissie +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/02 11:59:13 by jteissie #+# #+# */ +/* Updated: 2024/09/02 12:16:18 by jteissie ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +// static void mouse_move_camera() + + +int mousemove_events(int x, int y, t_data *data) +{ + (void)data; + printf("x: %d\n", x); + printf("y: %d\n", y); + mlx_mouse_move(data->mlx, data->window, WIDTH / 2, HEIGHT / 2); + return (SUCCESS); +} diff --git a/srcs/main.c b/srcs/main.c index b43239c..13c5294 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ +/* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/08/30 14:09:06 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 12:14:45 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,6 +62,7 @@ void start_game(t_data *data) mlx_loop_hook(data->mlx, rc_rendering, data); mlx_hook(data->window, KeyPress, KeyPressMask, &key_events, data); mlx_hook(data->window, DestroyNotify, StructureNotifyMask, &cleanup, data); + mlx_hook(data->window, MotionNotify, PointerMotionMask, &mousemove_events, data); mlx_loop(data->mlx); } From 78ec7f2ef6922209d3f1a99a48a4ed9ee31c6a3b Mon Sep 17 00:00:00 2001 From: Jean Teissier Date: Mon, 2 Sep 2024 13:31:31 +0200 Subject: [PATCH 2/3] mouse_move() function working. Traps the cursor in the middle of the screen and correctly moves and keeps moving the camera left and right depending on the motion the user inputs through their mouse --- includes/cub3d.h | 5 +++-- srcs/events/mouse_events.c | 22 ++++++++++++++-------- srcs/main.c | 4 ++-- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/includes/cub3d.h b/includes/cub3d.h index c7f8475..6bdef16 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 12:05:23 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 13:30:19 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,6 +48,7 @@ # define HEIGHT 720 # define FOV 90 # define ROT_STEP 1.5f +# define MOUSE_ROT_STEP 1.0f # define MOV_STEP 0.1f # define KEY_PRESS 2 # define MOUSE_PRESS 4 @@ -125,7 +126,7 @@ typedef struct s_data int cleanup(t_data *data); int game_init(t_data *data); int key_events(int keycode, t_data *data); -int mousemove_events(int x, int y, t_data *data); +int mouse_move(int x, int y, t_data *data); void move_check(t_vec *step, t_data *data, int add_or_sub); /* color utils*/ int create_trgb(int t, int r, int g, int b); diff --git a/srcs/events/mouse_events.c b/srcs/events/mouse_events.c index 621a5a7..6f6951c 100644 --- a/srcs/events/mouse_events.c +++ b/srcs/events/mouse_events.c @@ -6,20 +6,26 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/02 11:59:13 by jteissie #+# #+# */ -/* Updated: 2024/09/02 12:16:18 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 13:30:19 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -// static void mouse_move_camera() - - -int mousemove_events(int x, int y, t_data *data) +int mouse_move(int x, int y, t_data *data) { - (void)data; - printf("x: %d\n", x); - printf("y: %d\n", y); + if (x == WIDTH / 2 && y == HEIGHT / 2) + return (SUCCESS); + if (x >= WIDTH / 2) + { + vec_rotate(&data->p_cam, MOUSE_ROT_STEP * -1); + vec_rotate(&data->p_dir, MOUSE_ROT_STEP * -1); + } + else + { + vec_rotate(&data->p_cam, MOUSE_ROT_STEP); + vec_rotate(&data->p_dir, MOUSE_ROT_STEP); + } mlx_mouse_move(data->mlx, data->window, WIDTH / 2, HEIGHT / 2); return (SUCCESS); } diff --git a/srcs/main.c b/srcs/main.c index 13c5294..dfa1ff5 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 12:14:45 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 13:30:25 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -62,7 +62,7 @@ void start_game(t_data *data) mlx_loop_hook(data->mlx, rc_rendering, data); mlx_hook(data->window, KeyPress, KeyPressMask, &key_events, data); mlx_hook(data->window, DestroyNotify, StructureNotifyMask, &cleanup, data); - mlx_hook(data->window, MotionNotify, PointerMotionMask, &mousemove_events, data); + mlx_hook(data->window, MotionNotify, PointerMotionMask, &mouse_move, data); mlx_loop(data->mlx); } From a832a6dd7b2d247e3be449a8d4849be13d2efce7 Mon Sep 17 00:00:00 2001 From: Jean Teissier Date: Mon, 2 Sep 2024 13:46:56 +0200 Subject: [PATCH 3/3] Added pause_game() function in key_events() that releases the cursor from the game and blocks all other movement and rotation events from being called. Adds pause flag in data struct to track the current state of the game --- includes/cub3d.h | 4 +++- srcs/events/key_events.c | 30 +++++++++++++++++++++++------- srcs/events/mouse_events.c | 4 +++- srcs/game_init.c | 4 ++-- srcs/main.c | 3 ++- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/includes/cub3d.h b/includes/cub3d.h index 6bdef16..ebbaa26 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 13:30:19 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 13:38:28 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,6 +75,7 @@ typedef enum e_keys A_KEY = 97, S_KEY = 115, D_KEY = 100, + P_KEY = 112, } t_keys; typedef enum e_bool { @@ -113,6 +114,7 @@ typedef struct s_data char **map; void *mlx; void *window; + int pause; t_image image; t_textdata *textures; t_wall w_data; diff --git a/srcs/events/key_events.c b/srcs/events/key_events.c index df7e89f..1997e8e 100644 --- a/srcs/events/key_events.c +++ b/srcs/events/key_events.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 18:40:50 by jteissie #+# #+# */ -/* Updated: 2024/09/02 11:59:01 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 13:43:02 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,19 +54,35 @@ static void key_move_camera(t_data *data, t_rot dir) return ; } +static void pause_game(t_data *data) +{ + if (data->pause == TRUE) + { + mlx_mouse_hide(data->mlx, data->window); + data->pause = FALSE; + } + else + { + mlx_mouse_show(data->mlx, data->window); + data->pause = TRUE; + } +} + int key_events(int keycode, t_data *data) { - if (keycode == ARROW_RIGHT) + if (keycode == P_KEY) + pause_game(data); + else if (keycode == ARROW_RIGHT && data->pause == FALSE) key_move_camera(data, CLOCK); - else if (keycode == ARROW_LEFT) + else if (keycode == ARROW_LEFT && data->pause == FALSE) key_move_camera(data, CCLOCK); - else if (keycode == W_KEY) + else if (keycode == W_KEY && data->pause == FALSE) move_player(data, FORWARD); - else if (keycode == S_KEY) + else if (keycode == S_KEY && data->pause == FALSE) move_player(data, BACKWARD); - else if (keycode == A_KEY) + else if (keycode == A_KEY && data->pause == FALSE) move_player(data, LEFT); - else if (keycode == D_KEY) + else if (keycode == D_KEY && data->pause == FALSE) move_player(data, RIGHT); else if (keycode == ESC_KEY) cleanup(data); diff --git a/srcs/events/mouse_events.c b/srcs/events/mouse_events.c index 6f6951c..914d957 100644 --- a/srcs/events/mouse_events.c +++ b/srcs/events/mouse_events.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/09/02 11:59:13 by jteissie #+# #+# */ -/* Updated: 2024/09/02 13:30:19 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 13:33:50 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,8 @@ int mouse_move(int x, int y, t_data *data) { + if (data->pause == TRUE) + return (0); if (x == WIDTH / 2 && y == HEIGHT / 2) return (SUCCESS); if (x >= WIDTH / 2) diff --git a/srcs/game_init.c b/srcs/game_init.c index b626a7a..a00df49 100644 --- a/srcs/game_init.c +++ b/srcs/game_init.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* game_init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: tsuchen +#+ +:+ +#+ */ +/* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 17:03:43 by jteissie #+# #+# */ -/* Updated: 2024/09/02 09:38:54 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 13:33:15 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/main.c b/srcs/main.c index dfa1ff5..ae86069 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: jteissie +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 13:30:25 by jteissie ### ########.fr */ +/* Updated: 2024/09/02 13:39:24 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,6 +59,7 @@ static int init_data(t_data *data, char *map_file) void start_game(t_data *data) { + mlx_mouse_hide(data->mlx, data->window); mlx_loop_hook(data->mlx, rc_rendering, data); mlx_hook(data->window, KeyPress, KeyPressMask, &key_events, data); mlx_hook(data->window, DestroyNotify, StructureNotifyMask, &cleanup, data);