/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* dda.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: erey-bet +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/05/02 15:49:00 by erey-bet #+# #+# */ /* Updated: 2023/05/02 16:58:31 by erey-bet ### ########.fr */ /* */ /* ************************************************************************** */ #include "game.h" void init_dda(t_game *game, t_dda *dda) { t_ray *ray ray = game->ray; dda->map_x = (int)game->ply->pos_x; dda->map_y = (int)game->ply->pos_y; if (ray->dir_x == 0) dda->delta_dist_x = 1e30; else dda->delta_dist_x = abs(1 / ray->dir_x); if (ray->dir_y == 0) dda->delta_dist_y = 1e30; else dda->delta_dist_y = abs(1 / ray->dir_y); dda->hit = 0; if (ray->dir_x < 0) { dda->step_x = -1; dda->side_dist_x = (game->ply->pos_x - dda->map_x) * dda->delta_dist_x; } else { dda->step_x = 1; dda->side_dist_x = (dda->map_x + 1.0 - game->ply->pos_y) * dda->delta_dist_x; } if (ray->dir_y < 0) { dda->step_y = -1; dda->side_dist_y = (game->ply->pos_y - dda->map_y) * dda->delta_dist_y; } else { dda->step_y = 1; dda->side_dist_y = (dda->map_y + 1.0 - game->ply->pos_y) * dda->delta_dist_y; } } void loop_dda(t_game *game, t_dda *dda) { 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; } if (game->map->map[dda->map_x][dda->map_y] > 0) dda->hit = 1; } } void ray_dda(t_game *game, t_dda *dda) { 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); dda->line_height = (int)(HEIGHT / dda->perp_wall_dist); dda->draw_start = -dda->line_height / 2 + HEIGHT / 2; if(dda->draw_start < 0) dda->draw_start = 0; dda->draw_end = dda->line_height / 2 + HEIGHT / 2; if(draw_end >= HEIGHT) draw_end = HEIGHT - 1; } int create_trgb(int t, int r, int g, int b) { return (t << 24 | r << 16 | g << 8 | b); } #include #include void draw_dda(t_game *game, t_dda *dda) { int color; srand(time(NULL)); int r = rand() % 4; if (r == 0) color = create_trgv(255, 150, 10, 200); else if (r == 1) color = create_trgv(255, 10, 150, 200); else if (r == 2) color = create_trgv(255, 200, 150, 10); else if (r == 3) color = create_trgv(255, 10, 200, 150); mlx_put_pixel(game->mlx->window, abs(x), y, } void dda(t_game *game, t_dda *dda) { init_dda(game, dda); loop_dda(game, dda); ray_dda(game, dda); draw_dda(game, dda); }