diff --git a/Makefile b/Makefile index 8b079fd..f60006b 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/08/15 12:02:08 by tsuchen #+# #+# # -# Updated: 2024/08/29 17:27:35 by jteissie ### ########.fr # +# Updated: 2024/08/29 17:44:16 by jteissie ### ########.fr # # # # **************************************************************************** # @@ -15,7 +15,8 @@ NAME = cub3d SRCS_M = main.c \ game_init.c \ cleanup.c \ - color_utils.c + color_utils.c \ + render_utils.c SRCS_PS = parser.c \ diff --git a/includes/cub3d.h b/includes/cub3d.h index 590d8e8..e15afc0 100644 --- a/includes/cub3d.h +++ b/includes/cub3d.h @@ -6,7 +6,7 @@ /* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 00:19:44 by tsuchen #+# #+# */ -/* Updated: 2024/08/29 17:33:28 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 17:42:57 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -48,10 +48,11 @@ # define WIDTH 1280 # define HEIGHT 720 # define FOV 90 +# define ROT_STEP 1.0f +# define MOV_STEP 0.1f # define KEY_PRESS 2 # define MOUSE_PRESS 4 # define MOUSE_MOVE 6 -# define CLOSE_BUTTON 17 # define FILE_EXTENSION ".cub" typedef enum e_texture t_texture; @@ -80,6 +81,20 @@ typedef enum e_bool FALSE, TRUE, } t_bool; + +typedef enum e_rot +{ + CLOCK, + CCLOCK, +} t_rot; + +typedef enum e_move +{ + FORWARD, + BACKWARD, + RIGHT, + LEFT, +} t_move; typedef enum e_p_dir { NORTH = 'N', @@ -88,6 +103,15 @@ typedef enum e_p_dir EAST = 'E', } t_p_dir; +typedef struct s_image +{ + void *img; + char *addr; + int bpp; + int line_length; + int endian; +} t_image; + typedef struct s_data { char *map_path; @@ -97,7 +121,7 @@ typedef struct s_data char **map; void *mlx; void *window; - t_image *image; + t_image image; t_textdata *textures; t_wall wall_data; t_p_dir p_dir_default; @@ -117,5 +141,8 @@ int create_trgb(int t, int r, int g, int b); int get_color(int trgb, char index); int add_shade(double factor, int color); int get_opposite(int color); +/* rendering utils*/ +void rc_mlx_pixel_put(t_image *image, int x, int y, int color); +void rc_stripe_pixel_put(t_data *data, int x, double ray_dist); #endif diff --git a/includes/raycasting.h b/includes/raycasting.h index 400631c..8244c2c 100644 --- a/includes/raycasting.h +++ b/includes/raycasting.h @@ -6,34 +6,18 @@ /* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 17:39:13 by tsuchen #+# #+# */ -/* Updated: 2024/08/29 17:37:26 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 17:43:28 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef RAYCASTING_H # define RAYCASTING_H -# define WIDTH 1280 -# define HEIGHT 720 - # include "vector.h" # include "cub3d.h" -typedef struct s_vec t_vec; typedef struct s_data t_data; -typedef struct s_image -{ - void *img; - char *addr; - int bpp; - int line_length; - int endian; -} t_image; - - -void rc_mlx_pixel_put(t_image *image, int x, int y, int color); -void rc_stripe_pixel_put(t_data *data, int x, double ray_dist); int rc_dda(t_vec *dist, t_vec *unit_dist, t_vec *pos, t_vec *dir); void rc_ray_init(t_vec *dist, t_vec *pos, t_vec *dir, t_vec *unit_dst); double rc_raydist(t_vec *ray, t_data *data); diff --git a/includes/vector.h b/includes/vector.h index a67cc28..ecd7556 100644 --- a/includes/vector.h +++ b/includes/vector.h @@ -6,7 +6,7 @@ /* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 10:56:32 by tsuchen #+# #+# */ -/* Updated: 2024/08/28 16:45:05 by tsuchen ### ########.fr */ +/* Updated: 2024/08/29 16:43:25 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -38,5 +38,6 @@ void vec_mirror(t_vec *vec); void vec_transp(t_vec *vec); void vec_rotate(t_vec *vec, double angle); double vec_cos(t_vec *vec_a, t_vec *vec_b); +void vec_copy_scale(t_vec *vec_a, const t_vec *vec_b, double scale); #endif diff --git a/srcs/cleanup.c b/srcs/cleanup.c index 38fd28b..03ea629 100644 --- a/srcs/cleanup.c +++ b/srcs/cleanup.c @@ -6,7 +6,7 @@ /* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 17:04:32 by jteissie #+# #+# */ -/* Updated: 2024/08/29 14:38:59 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 17:45:39 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,11 +37,11 @@ int cleanup(t_data *data) { if (data->textures) cleanup_textures(data->textures, data->mlx); - if (data->image) - mlx_destroy_image(data->mlx, data->image->img); + if (data->image.img) + mlx_destroy_image(data->mlx, data->image.img); if (data->window) mlx_destroy_window(data->mlx, data->window); mlx_destroy_display(data->mlx); free(data->mlx); - return (SUCCESS); + exit(SUCCESS); } diff --git a/srcs/events/key_events.c b/srcs/events/key_events.c index 6c74734..e91bdbb 100644 --- a/srcs/events/key_events.c +++ b/srcs/events/key_events.c @@ -3,54 +3,72 @@ /* ::: :::::::: */ /* key_events.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ +/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 18:40:50 by jteissie #+# #+# */ -/* Updated: 2024/08/28 15:08:21 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 16:52:06 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ #include "cub3d.h" -static void move_player(t_data *data, int value) +static void move_player(t_data *data, t_move dir) { - (void)data; - (void)value; + t_vec step; + + if (dir == FORWARD) + { + vec_copy_scale(&step, &data->p_dir, MOV_STEP); + vec_add(&data->p_pos, &step); + } + else if (dir == BACKWARD) + { + vec_copy_scale(&step, &data->p_dir, MOV_STEP); + vec_sub(&data->p_pos, &step); + } + else if (dir == RIGHT) + { + vec_copy_scale(&step, &data->p_cam, MOV_STEP); + vec_add(&data->p_pos, &step); + } + else if (dir == LEFT) + { + vec_copy_scale(&step, &data->p_cam, MOV_STEP); + vec_sub(&data->p_pos, &step); + } return ; } -static void move_camera(t_data *data, int value) +static void move_camera(t_data *data, t_rot dir) { - (void)data; - (void)value; + if (dir == CLOCK) + { + vec_rotate(&data->p_cam, ROT_STEP * -1); + vec_rotate(&data->p_dir, ROT_STEP * -1); + } + else + { + vec_rotate(&data->p_cam, ROT_STEP); + vec_rotate(&data->p_dir, ROT_STEP); + } return ; } int key_events(int keycode, t_data *data) { - int value; - - value = 0; if (keycode == ARROW_RIGHT) - move_camera(data, value); + move_camera(data, CLOCK); else if (keycode == ARROW_LEFT) - move_camera(data, value); - else if (keycode == ARROW_UP) - move_camera(data, value); - else if (keycode == ARROW_DOWN) - move_camera(data, value); + move_camera(data, CCLOCK); else if (keycode == W_KEY) - move_player(data, value); + move_player(data, FORWARD); else if (keycode == S_KEY) - move_player(data, value); + move_player(data, BACKWARD); else if (keycode == A_KEY) - move_player(data, value); + move_player(data, LEFT); else if (keycode == D_KEY) - move_player(data, value); + move_player(data, RIGHT); else if (keycode == ESC_KEY) - { cleanup(data); - exit(EXIT_SUCCESS); - } return (SUCCESS); } diff --git a/srcs/game_init.c b/srcs/game_init.c index 090629f..b062c4d 100644 --- a/srcs/game_init.c +++ b/srcs/game_init.c @@ -6,7 +6,7 @@ /* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/27 17:03:43 by jteissie #+# #+# */ -/* Updated: 2024/08/29 17:29:02 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 17:44:55 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,7 +45,7 @@ int game_init(t_data *data) data->mlx = mlx_init(); if (!data->mlx) return (PANIC); - data->window = mlx_new_window(data->mlx, WIDTH, HEIGHT, "cub3d"); + data->window = mlx_new_window(data->mlx, WIDTH, HEIGHT, P_NAME); if (!data->window) { cleanup(data); @@ -56,5 +56,8 @@ int game_init(t_data *data) cleanup(data); return (PANIC); } + data->image.img = mlx_new_image(data->mlx, WIDTH, HEIGHT); + data->image.addr = mlx_get_data_addr(data->image.img, &data->image.bpp, + &data->image.line_length, &data->image.endian); return (SUCCESS); } diff --git a/srcs/main.c b/srcs/main.c index d36564a..42732ab 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ +/* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:03:33 by tsuchen #+# #+# */ -/* Updated: 2024/08/28 17:39:19 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 16:37:04 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -58,15 +58,11 @@ static int init_data(t_data *data, char *map_file) return (SUCCESS); } -void init_hooks(t_data *data) -{ - mlx_hook(data->window, KeyRelease, KeyReleaseMask, &key_events, data); - mlx_hook(data->window, DestroyNotify, StructureNotifyMask, &cleanup, data); -} - void start_game(t_data *data) { - init_hooks(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_loop(data->mlx); } diff --git a/srcs/raycasting/raycasting.c b/srcs/raycasting/raycasting.c index c8415e4..a5e27c5 100644 --- a/srcs/raycasting/raycasting.c +++ b/srcs/raycasting/raycasting.c @@ -6,7 +6,7 @@ /* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/15 12:13:34 by tsuchen #+# #+# */ -/* Updated: 2024/08/29 17:30:08 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 17:45:05 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,7 @@ double rc_raydist(t_vec *ray, t_data *data) t_vec unit_dist; t_vec dist_ray; - vec_init(&ray_pos, (int)data->p_pos.x, (int)data->p_pos.y); + vec_init(&ray_pos, data->p_pos.x, data->p_pos.y); unit_dist.x = sqrt(1 + ((ray->y * ray->y) / (ray->x * ray->x))); unit_dist.y = sqrt(1 + ((ray->x * ray->x) / (ray->y * ray->y))); rc_ray_init(&dist_ray, &ray_pos, ray, &unit_dist); @@ -68,7 +68,7 @@ double rc_raydist(t_vec *ray, t_data *data) return ((dist_ray.x - unit_dist.x) * vec_cos(ray, &data->p_dir)); } -void rc_rendering(t_data *data) +int rc_rendering(t_data *data) { int x; double cam_x; @@ -84,5 +84,6 @@ void rc_rendering(t_data *data) ray_dist = rc_raydist(&data->ray_dir, data); rc_stripe_pixel_put(data, x, ray_dist); } - mlx_put_image_to_window(data->mlx, data->window, data->image->img, 0, 0); + mlx_put_image_to_window(data->mlx, data->window, data->image.img, 0, 0); + return (0); } diff --git a/srcs/raycasting/render_utils.c b/srcs/render_utils.c similarity index 85% rename from srcs/raycasting/render_utils.c rename to srcs/render_utils.c index 826327b..a04d1ca 100644 --- a/srcs/raycasting/render_utils.c +++ b/srcs/render_utils.c @@ -6,11 +6,11 @@ /* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/29 12:12:17 by tsuchen #+# #+# */ -/* Updated: 2024/08/29 16:52:12 by jteissie ### ########.fr */ +/* Updated: 2024/08/29 17:47:57 by jteissie ### ########.fr */ /* */ /* ************************************************************************** */ -#include "raycasting.h" +#include "cub3d.h" void rc_mlx_pixel_put(t_image *image, int x, int y, int color) { @@ -30,9 +30,9 @@ void rc_stripe_pixel_put(t_data *data, int x, double ray_dist) while (++y < HEIGHT) { if (y < (HEIGHT - wall_height) / 2) - rc_mlx_pixel_put(data->image, x, y, data->textures->ceiling); + rc_mlx_pixel_put(&data->image, x, y, data->textures->ceiling); else if (y > (HEIGHT + wall_height) / 2) - rc_mlx_pixel_put(data->image, x, y, data->textures->floor); + rc_mlx_pixel_put(&data->image, x, y, data->textures->floor); else mlx_render_wall(data, x, y, ray_dist); } diff --git a/srcs/vector/vector_3.c b/srcs/vector/vector_3.c index 82d8424..25852b3 100644 --- a/srcs/vector/vector_3.c +++ b/srcs/vector/vector_3.c @@ -6,7 +6,7 @@ /* By: tsuchen <tsuchen@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/28 16:41:00 by tsuchen #+# #+# */ -/* Updated: 2024/08/29 11:46:19 by tsuchen ### ########.fr */ +/* Updated: 2024/08/29 16:42:46 by tsuchen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,3 +19,10 @@ double vec_cos(t_vec *vec_a, t_vec *vec_b) output = vec_dot(vec_a, vec_b) / (vec_absv(vec_a) * vec_absv(vec_b)); return (output); } + +void vec_copy_scale(t_vec *vec_a, const t_vec *vec_b, double scale) +{ + vec_a->x = vec_b->x; + vec_a->y = vec_b->y; + vec_muls(vec_a, scale); +}