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);
}

202
srcs/draw/draw_window.c Normal file
View File

@@ -0,0 +1,202 @@
#include "cube3d.h"
void ray_intersect(t_game *game, int start_x, int start_y, int *end_x, int *end_y)
{
int cell_x;
int cell_y;
int slope_x;
int slope_y;
int ray_sign_x;
int ray_sign_y;
int first_next_x;
int first_next_y;
int next_x;
int next_y;
int next_cell_x;
int next_cell_y;
int ray_step_x;
int ray_step_y;
int is_x;
is_x = 0;
// ray sign
ray_sign_x = 1;
if (start_x < *end_x)
ray_sign_x = -1;
ray_sign_y = 1;
if (start_y < *end_y)
ray_sign_y = -1;
// cell position of ray
cell_x = start_x / game->cell;
cell_y = start_y / game->cell;
// slope of ray
slope_x = *end_x - start_x;
slope_y = *end_y - start_y;
// direction of next cell
next_cell_x = -ray_sign_x;
next_cell_y = -ray_sign_y;
// ray steps in both axis
ray_step_x = ft_abs(game->cell * slope_y);
ray_step_y = ft_abs(game->cell * slope_x);
// first next time ray cross grid
first_next_x = start_x % game->cell;
if (first_next_x && ray_sign_x < 0)
first_next_x = game->cell - first_next_x;
if (!first_next_x && ray_sign_x < 0)
first_next_x = game->cell;
first_next_y = start_y % game->cell;
if (first_next_y && ray_sign_y < 0)
first_next_y = game->cell - first_next_y;
if (!first_next_y && ray_sign_y < 0)
first_next_y = game->cell;
next_x = ft_abs(first_next_x * slope_y);
next_y = ft_abs(first_next_y * slope_x);
// loop through grid
if (game->ray_activ == 1) printf("\ncell_x:%i, cell_y:%i, first_next_x:%i, first_next_y:%i\n", cell_x, cell_y, first_next_x, first_next_y);
while (!is_wall(game, cell_x, cell_y))
{
if (game->ray_activ == 1) printf("next_x:%-4i, next_y:%-4i, ", next_x, next_y);
if (next_x > next_y)
{
cell_y += next_cell_y;
next_y += ray_step_y;
is_x = 0;
}
else
{
cell_x += next_cell_x;
next_x += ray_step_x;
is_x = 1;
}
if (game->ray_activ == 1) printf("cell_x:%-4i, cell_y:%-4i\n", cell_x, cell_y);
}
// end ray position
double ratio;
if (is_x)
{
*end_x = cell_x * game->cell;
if (ray_sign_x == 1)
*end_x += game->cell;
if (slope_x)
{
ratio = (double)(*end_x - start_x) / (double)slope_x;
*end_y = start_y + (double)slope_y * ratio;
}
}
else
{
*end_y = cell_y * game->cell;
if (ray_sign_y == 1)
*end_y += game->cell;
if (slope_y)
{
ratio = (double)(*end_y - start_y) / (double)slope_y;
*end_x = start_x + (double)slope_x * ratio;
}
}
}
void draw_rays(t_game *game)
{
int start_x;
int start_y;
int end_x;
int end_y;
int i;
// draw screen size
start_x = game->screen_size.start_x + game->plr_x;
start_y = game->screen_size.start_y + game->plr_y;
rotate(game, &start_x, &start_y);
end_x = game->screen_size.end_x + game->plr_x;
end_y = game->screen_size.end_y + game->plr_y;
rotate(game, &end_x, &end_y);
draw_line(game, start_x, start_y, end_x, end_y, 0x00FFFFFF);
// draw rays
i = 0;
while (i <= SCREEN_DEF)
{
// tmp display one ray with infos
if (i == game->ray_highlight)
game->ray_activ = 1;
// tmp end
start_x = game->ray.start_x + game->plr_x;
start_y = game->ray.start_y + game->plr_y;
end_x = game->ray.end_x + game->plr_x + i * SCREEN_SIZE / SCREEN_DEF;
end_y = game->ray.end_y + game->plr_y;
rotate(game, &end_x, &end_y);
ray_intersect(game, start_x, start_y, &end_x, &end_y);
draw_line(game, start_x, start_y, end_x, end_y, 0x00FF00FF);
// tmp display one ray with infos
if (game->ray_activ == 1)
draw_line(game, start_x, start_y, end_x, end_y, 0x00FF0000);
// tmp end
i++;
game->ray_activ = 0;
}
// draw screen dist
start_x = game->screen_dist.start_x + game->plr_x;
start_y = game->screen_dist.start_y + game->plr_y;
end_x = game->screen_dist.end_x + game->plr_x;
end_y = game->screen_dist.end_y + game->plr_y;
rotate(game, &end_x, &end_y);
draw_line(game, start_x, start_y, end_x, end_y, 0x00FFFFFF);
}
static void draw_square(t_game *game, int x, int y, int border, int fill, int size, int rotation)
{
int i;
int j;
int new_x;
int new_y;
i = 0;
while (i < size)
{
j = 0;
while (j < size)
{
new_x = x + j;
new_y = y + i;
if (rotation)
rotate(game, &new_x, &new_y);
if (!i || i == size - 1)
draw_pixel(game, new_x, new_y, border);
else if (!j || j == size - 1)
draw_pixel(game, new_x, new_y, border);
else
draw_pixel(game, new_x, new_y, fill);
j++;
}
i++;
}
}
void draw_map(t_game *game)
{
int i;
int j;
int x;
int y;
i = 0;
x = 0;
y = 0;
while ((game->map)[i])
{
j = 0;
while ((game->map)[i][j])
{
if ((game->map)[i][j] == '1' )
draw_square(game, x, y, 0x00999999, 0x00000000, game->cell, 0);
else
draw_square(game, x, y, 0x00555555, 0x00333333, game->cell, 0);
j++;
x += game->cell;
}
i++;
x = 0;
y += game->cell;
}
draw_square(game, game->plr_x - game->cell / 2, game->plr_y - game->cell / 2, 0x00999900, 0x00330033, game->cell, 1);
}

View File

@@ -1,18 +0,0 @@
#include "cube3d.h"
void plr_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)
plr_posx_increment(game);
else if (y < 0)
plr_posy_increment(game);
else if (x > xmax)
plr_posx_decrement(game);
else if (y > ymax)
plr_posy_decrement(game);
}

View File

@@ -1,25 +0,0 @@
#include "cube3d.h"
void plr_posx_decrement(t_game *game)
{
(game->plr_x) -= 5;
plr_limits(game, game->plr_x, game->plr_y);
}
void plr_posy_decrement(t_game *game)
{
(game->plr_y) -= 5;
plr_limits(game, game->plr_x, game->plr_y);
}
void plr_posx_increment(t_game *game)
{
(game->plr_x) += 5;
plr_limits(game, game->plr_x, game->plr_y);
}
void plr_posy_increment(t_game *game)
{
(game->plr_y) += 5;
plr_limits(game, game->plr_x, game->plr_y);
}