cub3d/src/render/render_line.c

96 lines
2.7 KiB
C

/* ************************************************************************** */
/* */
/* :::::::: */
/* render_line.c :+: :+: */
/* +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2025/04/17 20:06:40 by qmennen #+# #+# */
/* Updated: 2025/05/07 11:36:57 by whaffman ######## odam.nl */
/* */
/* ************************************************************************** */
#include "render.h"
static void line_low(t_screen *screen,
t_vec2 start, t_vec2 end, unsigned int color)
{
int delta;
int yi;
t_vec2 cur;
t_vec2 delta_point;
delta_point.x = end.x - start.x;
delta_point.y = abs((int) end.y - (int) start.y);
yi = 1;
if (end.y - start.y < 0)
yi = -1;
delta = 2 * delta_point.y - delta_point.x;
cur = start;
while (cur.x <= end.x)
{
if (render_check_bounds(screen, &cur))
mlx_put_pixel(screen->minimap, (int)cur.x, (int)cur.y, color);
if (delta > 0)
{
cur.y += yi;
delta -= 2 * delta_point.x;
}
delta += 2 * delta_point.y;
cur.x++;
}
}
static void line_high(t_screen *screen,
t_vec2 start, t_vec2 end, unsigned int color)
{
int delta;
int xi;
t_vec2 cur;
t_vec2 delta_point;
delta_point.x = abs((int) end.x - (int) start.x);
delta_point.y = end.y - start.y;
xi = 1;
if (end.x - start.x < 0)
xi = -1;
delta = 2 * delta_point.x - delta_point.y;
cur = start;
while (cur.y <= end.y)
{
if (render_check_bounds(screen, &cur))
mlx_put_pixel(screen->minimap, (int)cur.x, (int)cur.y, color);
if (delta > 0)
{
cur.x += xi;
delta -= 2 * delta_point.y;
}
delta += 2 * delta_point.x;
cur.y++;
}
}
void render_line(t_screen *screen,
t_vec2 start, t_vec2 end, unsigned int color)
{
if ((start.x < 0 || start.x >= (int) screen->img->width
|| start.y < 0 || start.y >= (int) screen->img->height)
&& (end.x < 0 || end.x >= (int) screen->img->width
|| end.y < 0 || end.y >= (int) screen->img->height))
return ;
if (abs((int) end.y - (int) start.y) < abs((int) end.x - (int) start.x))
{
if (start.x > end.x)
line_low(screen, end, start, color);
else
line_low(screen, start, end, color);
}
else
{
if (start.y > end.y)
line_high(screen, end, start, color);
else
line_high(screen, start, end, color);
}
}