cub3d/src/raycast/render.c

83 lines
2 KiB
C
Raw Normal View History

2025-07-30 16:25:12 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* render.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: freddy <freddy@student.42.fr> +#+ +:+ +#+ */
2025-07-30 16:25:12 +02:00
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/30 12:50:10 by tchampio #+# #+# */
/* Updated: 2025/07/31 11:24:59 by tchampio ### ########.fr */
2025-07-30 16:25:12 +02:00
/* */
/* ************************************************************************** */
#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);
}
}
static int get_color(int dir)
{
int color;
if (dir == 0)
color = 0x0000ff;
else if (dir == 1)
color = 0x00ff00;
else if (dir == 2)
color = 0xff0000;
else if (dir == 3)
color = 0xffeb3b;
else
color = 0xff53ff;
return (color);
}
2025-07-30 16:25:12 +02:00
/*
* 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;
dir = get_cardinal(ray);
2025-07-30 16:40:33 +02:00
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;
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;
2025-07-30 16:25:12 +02:00
data->screen_matrix[ray->draw_start][x] = color;
ray->draw_start++;
}
}