Files
42_INT_10_cube3d/srcs/draw/draw.c

95 lines
1.9 KiB
C

#include "cube3d.h"
static int pxl_out_limits(t_game *game, int x, int y)
{
int xmax;
int ymax;
xmax = game->sizel / (game->bpp / 8);
ymax = game->win_size_y;
if (x < 0 || y < 0 || x > xmax || y > ymax)
return (1);
return (0);
}
void draw_pixel(t_game *game, int x, int y, int color)
{
unsigned int position;
if (pxl_out_limits(game, x, y))
return ;
position = y * game->sizel + x * (game->bpp / 8);
*(unsigned int*)(game->img_data + position) = color;
}
void rotate(t_game *game, int *x, int *y)
{
int old_x;
int tmp_x;
int tmp_y;
// do nothing if not rotating
if (game->rot == 0)
return ;
// offset center
tmp_x = *x - game->plr_x;
tmp_y = *y - game->plr_y;
// calculate new coordinates
old_x = tmp_x;
tmp_x = tmp_x * game->cosi + tmp_y * game->cosj;
tmp_y = old_x * game->sini + tmp_y * game->sinj;
// de-offset center
*x = tmp_x + game->plr_x;
*y = tmp_y + game->plr_y;
}
void rotate_double(t_game *game, double *x, double *y)
{
double old_x;
double tmp_x;
double tmp_y;
// do nothing if not rotating
if (game->rot == 0)
return ;
// offset center
tmp_x = *x - game->plr_exact_x;
tmp_y = *y - game->plr_exact_y;
// calculate new coordinates
old_x = tmp_x;
tmp_x = tmp_x * game->cosi + tmp_y * game->cosj;
tmp_y = old_x * game->sini + tmp_y * game->sinj;
// de-offset center
*x = tmp_x + game->plr_exact_x;
*y = tmp_y + game->plr_exact_y;
}
void draw_line(t_game *game, int start_x, int start_y, int end_x, int end_y, int color)
{
int dx;
int dy;
int i;
int j;
dx = end_x - start_x;
dy = end_y - start_y;
i = 0;
j = 0;
while (ft_abs(i) <= ft_abs(dx) && ft_abs(j) <= ft_abs(dy))
{
draw_pixel(game, start_x + i, start_y + j, color);
if (!ft_abs(dx) || ft_abs(j) < ft_abs(i * dy / dx))
j += ft_sign(dy);
else
i += ft_sign(dx);
}
}
void draw(t_game *game)
{
draw_map(game);
draw_rays(game);
//draw_view(game);
mlx_put_image_to_window(game->mlx_ptr, game->win_ptr, game->img_ptr, 0, 0);
}