42_cube3D/game/dda.c

106 lines
3.0 KiB
C
Raw Normal View History

2023-05-02 10:58:55 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dda.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */
2023-05-11 10:25:29 -04:00
/* Updated: 2023/05/11 16:22:58 by erey-bet ### ########.fr */
2023-05-02 10:58:55 -04:00
/* */
/* ************************************************************************** */
#include "game.h"
2023-05-11 10:25:29 -04:00
void init_dda(t_dda *dda, t_ply *ply, t_ray *ray)
2023-05-03 11:48:36 -04:00
{
dda->map_x = (int)ply->pos_x;
dda->map_y = (int)ply->pos_y;
2023-05-02 10:58:55 -04:00
if (ray->dir_x == 0)
dda->delta_dist_x = 1e30;
else
2023-05-03 11:48:36 -04:00
dda->delta_dist_x = ft_abs(1 / ray->dir_x);
2023-05-02 10:58:55 -04:00
if (ray->dir_y == 0)
dda->delta_dist_y = 1e30;
else
2023-05-03 11:48:36 -04:00
dda->delta_dist_y = ft_abs(1 / ray->dir_y);
dda->step_x = 1;
dda->step_y = 1;
2023-05-02 10:58:55 -04:00
if (ray->dir_x < 0)
dda->step_x = -1;
2023-05-11 10:25:29 -04:00
if (ray->dir_y < 0)
dda->step_y = -1;
if (ray->dir_x < 0)
2023-05-03 11:48:36 -04:00
dda->side_dist_x = (ply->pos_x - dda->map_x) * dda->delta_dist_x;
2023-05-02 10:58:55 -04:00
else
2023-05-03 11:48:36 -04:00
dda->side_dist_x = (dda->map_x + 1.0 - ply->pos_x) * dda->delta_dist_x;
2023-05-02 10:58:55 -04:00
if (ray->dir_y < 0)
2023-05-03 11:48:36 -04:00
dda->side_dist_y = (ply->pos_y - dda->map_y) * dda->delta_dist_y;
2023-05-02 10:58:55 -04:00
else
2023-05-03 11:48:36 -04:00
dda->side_dist_y = (dda->map_y + 1.0 - ply->pos_y) * dda->delta_dist_y;
dda->hit = 0;
2023-05-02 10:58:55 -04:00
}
2023-05-11 10:25:29 -04:00
void loop_dda(t_game *game, t_dda *dda)
2023-05-02 10:58:55 -04:00
{
while (dda->hit == 0)
{
if (dda->side_dist_x < dda->side_dist_y)
{
dda->side_dist_x += dda->delta_dist_x;
dda->map_x += dda->step_x;
dda->side = 0;
}
else
{
dda->side_dist_y += dda->delta_dist_y;
dda->map_y += dda->step_y;
dda->side = 1;
}
2023-05-11 10:25:29 -04:00
if (dda->map_x < 0 || dda->map_y < 0 || dda->map_y >= game->map.size_y
|| dda->map_x >= (int)ft_strlen(game->map.map[dda->map_y]))
{
dda->perp_wall_dist = 1e30;
return ;
}
2023-05-11 10:25:29 -04:00
else
dda->perp_wall_dist = 0;
if (game->map.map[dda->map_y][dda->map_x] == '1')
2023-05-02 10:58:55 -04:00
dda->hit = 1;
2023-05-11 10:25:29 -04:00
}
}
void ray_dda(t_dda *dda)
{
2023-05-11 10:25:29 -04:00
if (dda->perp_wall_dist != 1e30)
{
if (dda->side == 0)
dda->perp_wall_dist = dda->side_dist_x - dda->delta_dist_x;
else
dda->perp_wall_dist = dda->side_dist_y - dda->delta_dist_y;
if (dda->perp_wall_dist == 0)
dda->perp_wall_dist = 1e30;
}
2023-05-02 10:58:55 -04:00
dda->line_height = (int)(HEIGHT / dda->perp_wall_dist);
2023-05-03 11:48:36 -04:00
dda->draw_start = -(dda->line_height) / 2 + HEIGHT / 2;
2023-05-11 10:25:29 -04:00
if (dda->draw_start < 0)
2023-05-02 10:58:55 -04:00
dda->draw_start = 0;
dda->draw_end = dda->line_height / 2 + HEIGHT / 2;
2023-05-11 10:25:29 -04:00
if (dda->draw_end >= HEIGHT)
2023-05-03 11:48:36 -04:00
dda->draw_end = HEIGHT - 1;
}
void dda(t_game *game, int x)
2023-05-02 10:58:55 -04:00
{
2023-05-11 10:25:29 -04:00
t_dda dda;
t_ray *ray;
t_ply *ply;
2023-05-03 11:48:36 -04:00
2023-05-11 10:25:29 -04:00
ray = &game->ray;
ply = &game->ply;
init_dda(&dda, ply, ray);
2023-05-03 11:48:36 -04:00
loop_dda(game, &dda);
ray_dda(&dda);
draw(game, &dda, x);
2023-05-02 10:58:55 -04:00
}