cub3d/src/raycast/walls.c

92 lines
2.5 KiB
C
Raw Normal View History

2025-07-30 16:25:12 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* walls.c :+: :+: :+: */
2025-07-30 16:25:12 +02:00
/* +:+ +:+ +:+ */
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
2025-07-30 16:25:12 +02:00
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */
2025-09-09 10:17:28 +02:00
/* Updated: 2025/09/09 09:59:45 by tchampio ### ########.fr */
2025-07-30 16:25:12 +02:00
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../consts.h"
#include "ray.h"
#include "../renderer/render.h"
#include "../draw/drawutils.h"
2025-09-09 10:17:28 +02:00
#include "../../libft/includes/libft.h"
2025-07-30 16:25:12 +02:00
t_cardinal_dir get_cardinal(t_ray *ray)
2025-07-30 16:25:12 +02:00
{
if (ray->side == NORTH)
2025-07-30 16:25:12 +02:00
{
if (ray->dir_x < 0)
return (WEST);
2025-07-30 16:25:12 +02:00
else
return (EAST);
2025-07-30 16:25:12 +02:00
}
else
{
if (ray->dir_y > 0)
return (SOUTH);
2025-07-30 16:25:12 +02:00
else
return (NORTH);
2025-07-30 16:25:12 +02:00
}
}
2025-08-05 12:55:57 +02:00
static int get_color(t_cub3d_data *data, t_ray *ray, int tex_y)
{
t_cardinal_dir dir;
int tex_x;
t_img_data *texture;
tex_x = (int)(ray->wall_x * TEXTURE_SIZE);
if ((ray->side == NORTH && ray->dir_x < 0)
|| (ray->side == SOUTH && ray->dir_y > 0))
tex_x = TEXTURE_SIZE - tex_x - 1;
dir = get_cardinal(ray);
2025-09-09 10:17:28 +02:00
if (ft_strchr("2345678", data->map->map[ray->map_y][ray->map_x]))
return (my_mlx_pixel_get(data->barricades_texture, tex_x, tex_y));
if (dir == NORTH)
2025-08-05 12:55:57 +02:00
texture = data->no_texture;
else if (dir == SOUTH)
2025-08-05 12:55:57 +02:00
texture = data->so_texture;
else if (dir == WEST)
2025-08-05 12:55:57 +02:00
texture = data->we_texture;
else if (dir == EAST)
2025-08-05 12:55:57 +02:00
texture = data->ea_texture;
else
2025-08-05 12:55:57 +02:00
return (0xff53ff);
return (my_mlx_pixel_get(texture, tex_x, tex_y));
}
2025-07-30 16:25:12 +02:00
void render_walls(t_cub3d_data *data, t_ray *ray, int x)
{
unsigned int color;
double step;
2025-08-05 12:55:57 +02:00
double tex_y;
2025-08-05 14:55:41 +02:00
int y;
2025-07-30 16:25:12 +02:00
2025-07-30 16:40:33 +02:00
step = 1.0 * TEXTURE_SIZE / ray->wall_height;
2025-08-05 12:55:57 +02:00
tex_y = (ray->draw_start - HEIGHT / 2 + ray->wall_height / 2) * step;
2025-08-05 14:55:41 +02:00
y = 0;
while (y < ray->draw_start)
{
matrix_set(data, x, y, data->map->c_color);
y++;
}
while (y < ray->draw_end - 1)
2025-07-30 16:25:12 +02:00
{
2025-08-05 12:55:57 +02:00
tex_y += step;
color = get_color(data, ray, (int)tex_y);
2025-08-05 14:55:41 +02:00
matrix_set(data, x, y, color);
y++;
}
while (y < HEIGHT)
{
matrix_set(data, x, y, data->map->f_color);
y++;
2025-07-30 16:25:12 +02:00
}
}