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 cde6c7e..55d163f 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 16:11:01 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 16:18:49 by tsuchen ### ########.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 MINI_MAP_W 200 # define MINI_MAP_H 200 @@ -78,6 +79,7 @@ typedef enum e_keys A_KEY = 97, S_KEY = 115, D_KEY = 100, + P_KEY = 112, } t_keys; typedef enum e_bool { @@ -117,6 +119,7 @@ typedef struct s_data char **map; void *mlx; void *window; + int pause; t_image image; t_image mini_map; t_textdata *textures; @@ -131,6 +134,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 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/key_events.c b/srcs/events/key_events.c index a9530bc..1997e8e 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 13:43:02 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) { @@ -54,19 +54,35 @@ static void 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) - move_camera(data, CLOCK); - else if (keycode == ARROW_LEFT) - move_camera(data, CCLOCK); - else if (keycode == W_KEY) + 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 && data->pause == FALSE) + key_move_camera(data, CCLOCK); + 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 new file mode 100644 index 0000000..914d957 --- /dev/null +++ b/srcs/events/mouse_events.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* mouse_events.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jteissie +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/09/02 11:59:13 by jteissie #+# #+# */ +/* Updated: 2024/09/02 13:33:50 by jteissie ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "cub3d.h" + +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) + { + 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/game_init.c b/srcs/game_init.c index 1cb76d0..6688250 100644 --- a/srcs/game_init.c +++ b/srcs/game_init.c @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 17:03:43 by jteissie #+# #+# */ -/* Updated: 2024/09/02 16:12:09 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 16:17:55 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/srcs/main.c b/srcs/main.c index a44bb0c..0b8ae93 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: tsuchen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/09/02 13:48:42 by tsuchen ### ########.fr */ +/* Updated: 2024/09/02 16:20:08 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -61,9 +61,11 @@ 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); + mlx_hook(data->window, MotionNotify, PointerMotionMask, &mouse_move, data); mlx_loop(data->mlx); }