Cub3D
Loading...
Searching...
No Matches
perform_dda.c
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* perform_dda.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: kamitsui <kamitsui@student.42tokyo.jp> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/08/26 22:33:05 by kamitsui #+# #+# */
9/* Updated: 2024/11/30 15:46:35 by kamitsui ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "cub3d.h"
14
15static int get_line_type(t_ray_cast ray_cast)
16{
17 if (ray_cast.next_distance.x < ray_cast.next_distance.y)
18 return (ON_VERTICAL_LINE);
19 else
20 return (ON_HORIZONTAL_LINE);
21}
22
23static bool is_hit_wall(t_map *map, t_ray_cast *ray_cast)
24{
25 return (map->data[ray_cast->grid.y][ray_cast->grid.x] == '1');
26}
27
28static void move_next_grid_line(t_ray_cast *ray_cast, int type)
29{
30 if (type == ON_VERTICAL_LINE)
31 {
32 ray_cast->next_distance.x += ray_cast->delta_distance.x;
33 ray_cast->grid.x += ray_cast->step_dir.x;
34 }
35 else
36 {
37 ray_cast->next_distance.y += ray_cast->delta_distance.y;
38 ray_cast->grid.y += ray_cast->step_dir.y;
39 }
40}
41
42static double calculate_distance(t_ray_cast ray_cast, int type)
43{
44 if (type == ON_VERTICAL_LINE)
45 return (ray_cast.next_distance.x - ray_cast.delta_distance.x);
46 else
47 return (ray_cast.next_distance.y - ray_cast.delta_distance.y);
48}
49
56void perform_dda(t_frame *frame, int x)
57{
58 bool hit;
59 int type;
60
61 hit = false;
62 type = START_POINT;
63 while (hit == 0)
64 {
65 type = get_line_type(frame->ray_cast);
66 move_next_grid_line(&frame->ray_cast, type);
67 hit = is_hit_wall(frame->map, &frame->ray_cast);
68 }
69 frame->dda.type_of_grid_line = type;
70 frame->dda.perp_wall_dist = calculate_distance(frame->ray_cast, type);
71 (void)x;
72}
73// debug code
74//{
75// bool hit;
76// int type;
77//
78// hit = false;
79// type = START_POINT;
80// debug_dda(frame, type, "perform_dda() ... start");
81// while (hit == 0)
82// {
83// type = get_line_type(frame->ray_cast);
84// move_next_grid_line(&frame->ray_cast, type);
85// hit = is_hit_wall(frame->map, &frame->ray_cast);
86// debug_dda(frame, type, "perform_dda() ... loop until hit wall");
87// }
88// frame->dda.type_of_grid_line = type;
89// frame->dda.perp_wall_dist = calculate_distance(frame->ray_cast, type);
90// (void)x;
91//}
92// reference cub3d.h
93//# define START_POINT -1
94//# define ON_VERTICAL_LINE 0
95//# define ON_HORIZONTAL_LINE 1
single still image for render_frame
Definition type_cub3d.h:247
game map
Definition type_cub3d.h:237