player rotates and moves, rays for raycasting on map

This commit is contained in:
Hugo LAMY
2022-04-18 18:27:16 +02:00
parent 00b3612c16
commit 01eb2255d2
21 changed files with 689 additions and 138 deletions

View File

@@ -2,8 +2,8 @@
static int pxl_out_limits(t_game *game, int x, int y)
{
int xmax;
int ymax;
int xmax;
int ymax;
xmax = game->sizel / (game->bpp / 8);
ymax = game->win_size_y;
@@ -12,7 +12,7 @@ static int pxl_out_limits(t_game *game, int x, int y)
return (0);
}
static void draw_pixel(t_game *game, int x, int y, int color)
void draw_pixel(t_game *game, int x, int y, int color)
{
unsigned int position;
@@ -22,30 +22,73 @@ static void draw_pixel(t_game *game, int x, int y, int color)
*(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)
{
// temp, draw a map of points
int x;
int y;
int x_size;
// unsigned int screen_size;
x_size = game->sizel / (game->bpp / 8);
// screen_size = x_size * game->win_size_y / 5;
x = 0;
y = 0;
while (y <= game->win_size_y)
{
// y = x % x_size * 5;
draw_pixel(game, x, y, 0x00999999);
x += 5;
if (x > x_size)
{
x = 0;
y += 5;
}
}
// temp
draw_pixel(game, game->plr_x, game->plr_y, 0x0000FF00);
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);
}