From b6aa74d8d81c95c7ef241647b1926deb814c986c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 28 Jul 2025 13:04:32 +0200 Subject: [PATCH] feat: draw view cone (without DDA) --- src/main.c | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index f8bc0f7..3acace7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,13 +6,14 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/07/17 14:14:30 by kcolin #+# #+# */ -/* Updated: 2025/07/28 12:57:08 by kcolin ### ########.fr */ +/* Updated: 2025/07/28 13:16:19 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ #include "../libft/includes/libft.h" #include "../mlx/mlx.h" #include "consts.h" +#include "cub3d_data.h" #include "draw/drawutils.h" #include "map/map_checker.h" #include "draw/draw_map.h" @@ -53,27 +54,33 @@ bool touch(float px, float py, t_mapdata *map) return (false); } +void draw_line(t_cub3d_data *data, float start_x) +{ + float cos_angle = cos(start_x); + float sin_angle = sin(start_x); + float ray_x = data->player.x; + float ray_y = data->player.y; + + while(!touch(ray_x, ray_y, data->map)) + { + my_mlx_pixel_put(data->img_data, (int)(MAP_SIZE * (ray_x / SIZE)), (int)(MAP_SIZE * (ray_y / SIZE)), 0xcc241b); + ray_x += sin_angle; + ray_y += cos_angle; + } +} + int game_loop(t_cub3d_data *data) { mlx_destroy_image(data->mlx, data->img_data->img); data->img_data->img = mlx_new_image(data->mlx, WIDTH, HEIGHT); - int column = 0; - while (column < 1) + int column = 0; + float fraction = M_PI / 3 / WIDTH; + float start_angle = data->player.yaw - M_PI / 6; + while (column < WIDTH) { - // calculate angle - double cos_angle = cos(data->player.yaw); - double sin_angle = sin(data->player.yaw); - double ray_x = data->player.x; - double ray_y = data->player.y; - while (!touch(ray_x, ray_y, data->map)) - { - my_mlx_pixel_put(data->img_data, (int)(MAP_SIZE * (ray_x / SIZE)), (int)(MAP_SIZE * (ray_y / SIZE)), 0xFF0000); - ray_x += sin_angle; - ray_y += cos_angle; - } - // calculate distance - // draw + draw_line(data, start_angle); + start_angle += fraction; column++; }