Skip to content

Commit

Permalink
Merged main to this branch
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsunghao-C committed Sep 2, 2024
2 parents 9f289d1 + 4507101 commit c7df545
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 18 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ #
# By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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 #
# #
# **************************************************************************** #

Expand All @@ -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 \
Expand All @@ -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

Expand Down
6 changes: 5 additions & 1 deletion includes/cub3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
38 changes: 27 additions & 11 deletions srcs/events/key_events.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* key_events.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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)
{
Expand All @@ -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);
Expand Down
33 changes: 33 additions & 0 deletions srcs/events/mouse_events.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mouse_events.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
2 changes: 1 addition & 1 deletion srcs/game_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down
4 changes: 3 additions & 1 deletion srcs/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: tsuchen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit c7df545

Please sign in to comment.