cub3d/src/raycast/walls.c

77 lines
2.1 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 #+# #+# */
/* Updated: 2025/08/05 12:49:26 by kcolin ### ########.fr */
2025-07-30 16:25:12 +02:00
/* */
/* ************************************************************************** */
#include "../cub3d_data.h"
#include "../consts.h"
#include "ray.h"
#include "../renderer/render.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
}
}
static int get_color(t_cardinal_dir dir)
{
int color;
if (dir == NORTH)
color = 0x0000ff;
else if (dir == SOUTH)
color = 0x00ff00;
else if (dir == WEST)
color = 0xff0000;
else if (dir == EAST)
color = 0xffeb3b;
else
color = 0xff53ff;
return (color);
}
2025-07-30 16:25:12 +02:00
void render_walls(t_cub3d_data *data, t_ray *ray, int x)
{
t_cardinal_dir dir;
2025-07-30 16:25:12 +02:00
int tex_x;
unsigned int color;
double step;
double pos;
dir = get_cardinal(ray);
2025-07-30 16:40:33 +02:00
tex_x = (int)(ray->wall_x * TEXTURE_SIZE);
if ((ray->side == NORTH && ray->dir_x < 0)
|| (ray->side == SOUTH && ray->dir_y > 0))
2025-07-30 16:40:33 +02:00
tex_x = TEXTURE_SIZE - tex_x - 1;
step = 1.0 * TEXTURE_SIZE / ray->wall_height;
2025-07-30 16:25:12 +02:00
pos = (ray->draw_start - HEIGHT / 2 + ray->wall_height / 2) * step;
while (ray->draw_start < ray->draw_end)
{
pos += step;
color = get_color(dir);
2025-07-30 16:40:33 +02:00
(void)pos;
matrix_set(data, x, ray->draw_start, color);
2025-07-30 16:25:12 +02:00
ray->draw_start++;
}
}