From 5281fe6abdaa452177c8204897d4dbb399d255c3 Mon Sep 17 00:00:00 2001 From: Theo Champion Date: Wed, 30 Jul 2025 16:25:12 +0200 Subject: [PATCH] feat: made 3d... for the cub3d --- Makefile | 3 ++ src/cub3d_data.h | 4 ++- src/main.c | 12 ++++++-- src/raycast/ray.c | 7 +++-- src/raycast/raycaster.h | 22 ++++++++++++++ src/raycast/render.c | 67 +++++++++++++++++++++++++++++++++++++++++ src/renderer/render.c | 54 +++++++++++++++++++++++++++++++++ src/renderer/render.h | 21 +++++++++++++ 8 files changed, 185 insertions(+), 5 deletions(-) create mode 100644 src/raycast/raycaster.h create mode 100644 src/raycast/render.c create mode 100644 src/renderer/render.c create mode 100644 src/renderer/render.h diff --git a/Makefile b/Makefile index 5280891..0efe3b2 100644 --- a/Makefile +++ b/Makefile @@ -23,6 +23,9 @@ SOURCEFILES = \ src/player/angle.c \ src/player/player.c \ src/player/move.c \ + src/raycast/ray.c \ + src/raycast/render.c \ + src/renderer/render.c \ OBJECTS = $(SOURCEFILES:.c=.o) NAME = cub3d diff --git a/src/cub3d_data.h b/src/cub3d_data.h index 28c3859..708943f 100644 --- a/src/cub3d_data.h +++ b/src/cub3d_data.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:59:37 by kcolin #+# #+# */ -/* Updated: 2025/07/29 13:43:40 by tchampio ### ########.fr */ +/* Updated: 2025/07/30 16:07:36 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ # include "draw/img_data.h" # include "player/player.h" # include "utils/keypresses.h" +# include "consts.h" typedef struct s_cub3d_data { @@ -26,6 +27,7 @@ typedef struct s_cub3d_data t_mapdata *map; t_player player; t_keypresses keypresses; + int screen_matrix[HEIGHT][WIDTH]; } t_cub3d_data; #endif // CUB3D_DATA_H diff --git a/src/main.c b/src/main.c index 77d1693..33de6d0 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/29 14:00:36 by tchampio ### ########.fr */ +/* Updated: 2025/07/30 16:21:26 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,6 +21,9 @@ #include "draw/draw_map.h" #include "map/mapdata.h" #include "player/player.h" +#include "raycast/raycaster.h" +#include "renderer/render.h" +#include "raycast/ray.h" #include "utils/hooks.h" #include "utils/frees.h" #include @@ -103,8 +106,12 @@ float coord_global_to_local(float coord) int game_loop(t_cub3d_data *data) { + t_ray ray; + mlx_destroy_image(data->mlx, data->img_data->img); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); + reset_matrix(data); + raycaster(data, &ray); move_player(data); // if (player_intercardinal_dir(data->player) == SOUTHEAST) @@ -137,7 +144,8 @@ int game_loop(t_cub3d_data *data) // ray_angle += fraction; // } - draw_map(data->map, &data->player, data->img_data); + //draw_map(data->map, &data->player, data->img_data); + matrix_to_image(data); mlx_put_image_to_window(data->mlx, data->mlx_win, data->img_data->img, 0, 0); mlx_string_put(data->mlx, data->mlx_win, 10, 10, 0x00FFFFFF, COMPILED_TEXT); diff --git a/src/raycast/ray.c b/src/raycast/ray.c index 976af12..6cbd601 100644 --- a/src/raycast/ray.c +++ b/src/raycast/ray.c @@ -6,11 +6,12 @@ /* By: tchampio +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/30 12:10:17 by tchampio #+# #+# */ -/* Updated: 2025/07/30 12:36:11 by tchampio ### ########.fr */ +/* Updated: 2025/07/30 16:06:19 by tchampio ### ########.fr */ /* */ /* ************************************************************************** */ #include "ray.h" +#include "raycaster.h" #include "../player/player.h" #include "../consts.h" #include "../cub3d_data.h" @@ -84,7 +85,7 @@ void calculate_wall_height(t_ray *ray, t_player *player) ray->draw_start = 0; ray->draw_end = ray->wall_height / 2 + HEIGHT / 2; if (ray->draw_end >= HEIGHT) - ray->draw_start = HEIGHT - 1; + ray->draw_end = HEIGHT - 1; if (ray->side == 0) ray->wall_x = player->y + ray->wall_dist * ray->dir_y; else @@ -103,5 +104,7 @@ void raycaster(t_cub3d_data *data, t_ray *ray) ray_calculate_step(ray, &data->player); calculate_wall_dist(ray, data->map->map); calculate_wall_height(ray, &data->player); + render_walls(data, ray, x); + x++; } } diff --git a/src/raycast/raycaster.h b/src/raycast/raycaster.h new file mode 100644 index 0000000..3a6232e --- /dev/null +++ b/src/raycast/raycaster.h @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* raycaster.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/30 15:16:04 by tchampio #+# #+# */ +/* Updated: 2025/07/30 15:17:02 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RAYCASTER_H +# define RAYCASTER_H + +# include "ray.h" +# include "../cub3d_data.h" + +void raycaster(t_cub3d_data *data, t_ray *ray); +void render_walls(t_cub3d_data *data, t_ray *ray, int x); + +#endif // RAYCASTER_H diff --git a/src/raycast/render.c b/src/raycast/render.c new file mode 100644 index 0000000..c03057f --- /dev/null +++ b/src/raycast/render.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/30 12:50:10 by tchampio #+# #+# */ +/* Updated: 2025/07/30 16:22:42 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../cub3d_data.h" +#include "../consts.h" +#include "ray.h" + +int get_cardinal(t_ray *ray) +{ + if (ray->side == 0) + { + if (ray->dir_x < 0) + return (2); + else + return (3); + } + else + { + if (ray->dir_y > 0) + return (1); + else + return (0); + } + +} + +/* + * Dir values are: + * 0: North + * 1: South + * 2: West + * 3: East +*/ +void render_walls(t_cub3d_data *data, t_ray *ray, int x) +{ + int dir; + int tex_x; + unsigned int color; + double step; + double pos; + static int texture_size = 64; // to be changed in favor of a const + + dir = get_cardinal(ray); + tex_x = (int)(ray->wall_x * texture_size); + if ((ray->side == 0 && ray->dir_x < 0) || (ray->side == 1 && ray->dir_y > 0)) + tex_x = texture_size - tex_x - 1; + step = 1.0 * texture_size / ray->wall_height; + pos = (ray->draw_start - HEIGHT / 2 + ray->wall_height / 2) * step; + while (ray->draw_start < ray->draw_end) + { + pos += step; + color = 0x0000ff; + (void)dir; // cc complains otherwise + (void)pos; // cc complains otherwise + data->screen_matrix[ray->draw_start][x] = color; + ray->draw_start++; + } +} diff --git a/src/renderer/render.c b/src/renderer/render.c new file mode 100644 index 0000000..d39ae85 --- /dev/null +++ b/src/renderer/render.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/30 14:53:39 by tchampio #+# #+# */ +/* Updated: 2025/07/30 16:21:18 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "render.h" +#include "../consts.h" +#include "../cub3d_data.h" +#include "../draw/drawutils.h" +#include "../../libft/includes/libft.h" + +void reset_matrix(t_cub3d_data *data) +{ + int x; + int y; + + y = 0; + while (y < HEIGHT) + { + x = 0; + while (x < WIDTH) + { + data->screen_matrix[y][x] = 0; + x++; + } + y++; + } +} + +void matrix_to_image(t_cub3d_data *data) +{ + int x; + int y; + + y = 0; + while (y < HEIGHT) + { + x = 0; + while (x < WIDTH) + { + if (data->screen_matrix[y][x] > 0) + my_mlx_pixel_put(data->img_data, x, y, data->screen_matrix[y][x]); + x++; + } + y++; + } +} diff --git a/src/renderer/render.h b/src/renderer/render.h new file mode 100644 index 0000000..4471629 --- /dev/null +++ b/src/renderer/render.h @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* render.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tchampio +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/07/30 14:52:12 by tchampio #+# #+# */ +/* Updated: 2025/07/30 16:20:57 by tchampio ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef RENDER_H +# define RENDER_H + +# include "../cub3d_data.h" + +void reset_matrix(t_cub3d_data *data); +void matrix_to_image(t_cub3d_data *data); + +#endif // RENDER_H