2025-07-30 16:25:12 +02:00
|
|
|
/* ************************************************************************** */
|
|
|
|
|
/* */
|
|
|
|
|
/* ::: :::::::: */
|
2025-08-05 12:40:53 +02:00
|
|
|
/* walls.c :+: :+: :+: */
|
2025-07-30 16:25:12 +02:00
|
|
|
/* +:+ +:+ +:+ */
|
2025-07-31 13:17:27 +02:00
|
|
|
/* By: kcolin <kcolin@42.fr> +#+ +:+ +#+ */
|
2025-07-30 16:25:12 +02:00
|
|
|
/* +#+#+#+#+#+ +#+ */
|
2025-07-31 13:17:27 +02:00
|
|
|
/* Created: 2025/07/31 13:17:39 by kcolin #+# #+# */
|
2025-08-05 14:55:41 +02:00
|
|
|
/* Updated: 2025/08/05 14:59:24 by kcolin ### ########.fr */
|
2025-07-30 16:25:12 +02:00
|
|
|
/* */
|
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
|
|
|
|
|
#include "../cub3d_data.h"
|
|
|
|
|
#include "../consts.h"
|
|
|
|
|
#include "ray.h"
|
2025-07-31 13:17:27 +02:00
|
|
|
#include "../renderer/render.h"
|
2025-07-30 16:25:12 +02:00
|
|
|
|
2025-08-05 12:40:53 +02:00
|
|
|
t_cardinal_dir get_cardinal(t_ray *ray)
|
2025-07-30 16:25:12 +02:00
|
|
|
{
|
2025-08-05 12:40:53 +02:00
|
|
|
if (ray->side == NORTH)
|
2025-07-30 16:25:12 +02:00
|
|
|
{
|
|
|
|
|
if (ray->dir_x < 0)
|
2025-08-05 12:40:53 +02:00
|
|
|
return (WEST);
|
2025-07-30 16:25:12 +02:00
|
|
|
else
|
2025-08-05 12:40:53 +02:00
|
|
|
return (EAST);
|
2025-07-30 16:25:12 +02:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (ray->dir_y > 0)
|
2025-08-05 12:40:53 +02:00
|
|
|
return (SOUTH);
|
2025-07-30 16:25:12 +02:00
|
|
|
else
|
2025-08-05 12:40:53 +02:00
|
|
|
return (NORTH);
|
2025-07-30 16:25:12 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 12:55:57 +02:00
|
|
|
static int my_mlx_pixel_get(t_img_data *img, int x, int y)
|
2025-07-31 11:24:23 +02:00
|
|
|
{
|
2025-08-05 12:55:57 +02:00
|
|
|
char *dst;
|
2025-07-31 11:24:23 +02:00
|
|
|
|
2025-08-05 12:55:57 +02:00
|
|
|
dst = img->addr + (y * img->line_length + x * (img->bits_per_pixel / 8));
|
|
|
|
|
return (*(int *)dst);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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-08-05 12:40:53 +02:00
|
|
|
if (dir == NORTH)
|
2025-08-05 12:55:57 +02:00
|
|
|
texture = data->no_texture;
|
2025-08-05 12:40:53 +02:00
|
|
|
else if (dir == SOUTH)
|
2025-08-05 12:55:57 +02:00
|
|
|
texture = data->so_texture;
|
2025-08-05 12:40:53 +02:00
|
|
|
else if (dir == WEST)
|
2025-08-05 12:55:57 +02:00
|
|
|
texture = data->we_texture;
|
2025-08-05 12:40:53 +02:00
|
|
|
else if (dir == EAST)
|
2025-08-05 12:55:57 +02:00
|
|
|
texture = data->ea_texture;
|
2025-07-31 11:24:23 +02:00
|
|
|
else
|
2025-08-05 12:55:57 +02:00
|
|
|
return (0xff53ff);
|
|
|
|
|
return (my_mlx_pixel_get(texture, tex_x, tex_y));
|
2025-07-31 11:24:23 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
}
|