-
Notifications
You must be signed in to change notification settings - Fork 0
/
wall.c
106 lines (99 loc) · 2.92 KB
/
wall.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include "cub3d.h"
void wall_init(t_info *info)
{
info->rc.cameraX = 2 * info->rc.x / (double)info->width - 1;
info->rc.rayDirX = info->dirX + info->planeX * info->rc.cameraX;
info->rc.rayDirY = info->dirY + info->planeY * info->rc.cameraX;
info->rc.mapX = (int)info->posX;
info->rc.mapY = (int)info->posY;
info->rc.dDX = fabs(1 / info->rc.rayDirX);
info->rc.dDY = fabs(1 / info->rc.rayDirY);
info->rc.hit = 0;
}
void ray_walking(t_info *info)
{
if (info->rc.rayDirX < 0)
{
info->rc.stepX = -1;
info->rc.sideDistX = (info->posX - info->rc.mapX) * info->rc.dDX;
}
else
{
info->rc.stepX = 1;
info->rc.sideDistX = (info->rc.mapX + 1.0 - info->posX) * info->rc.dDX;
}
if (info->rc.rayDirY < 0)
{
info->rc.stepY = -1;
info->rc.sideDistY = (info->posY - info->rc.mapY) * info->rc.dDY;
}
else
{
info->rc.stepY = 1;
info->rc.sideDistY = (info->rc.mapY + 1.0 - info->posY) * info->rc.dDY;
}
}
void dda_algorithm(t_info *info)
{
while (info->rc.hit == 0)
{
if (info->rc.sideDistX < info->rc.sideDistY)
{
info->rc.sideDistX += info->rc.dDX;
info->rc.mapX += info->rc.stepX;
info->rc.side = 0;
if (info->rc.stepX == 1)
info->rc.side = 0;
else if (info->rc.stepX == -1)
info->rc.side = 1;
}
else
{
info->rc.sideDistY += info->rc.dDY;
info->rc.mapY += info->rc.stepY;
info->rc.side = 1;
if (info->rc.stepY == 1)
info->rc.side = 2;
else if (info->rc.stepY == -1)
info->rc.side = 3;
}
if (info->map[info->rc.mapX][info->rc.mapY] == '1')
info->rc.hit = 1;
}
}
void wall_calc(t_info *info)
{
double norm;
double norm2;
norm = (info->rc.mapX - info->posX + (1 - info->rc.stepX) / 2);
norm2 = (info->rc.mapY - info->posY + (1 - info->rc.stepY) / 2);
if (info->rc.side == 0 || info->rc.side == 1)
info->rc.perpWallDist = norm / info->rc.rayDirX;
else
info->rc.perpWallDist = norm2 / info->rc.rayDirY;
info->rc.lineHeight = (int)(info->height / info->rc.perpWallDist);
info->rc.drawStart = -info->rc.lineHeight / 2 + info->height / 2;
if (info->rc.drawStart < 0)
info->rc.drawStart = 0;
info->rc.drawEnd = info->rc.lineHeight / 2 + info->height / 2;
if (info->rc.drawEnd >= info->height)
info->rc.drawEnd = info->height - 1;
}
void wall_text(t_info *info)
{
double norm;
norm = (info->rc.drawStart - info->height / 2 + info->rc.lineHeight / 2);
if (info->rc.side == 0 || info->rc.side == 1)
info->rc.wallX = info->posY + info->rc.perpWallDist * info->rc.rayDirY;
else
info->rc.wallX = info->posX + info->rc.perpWallDist * info->rc.rayDirX;
info->rc.wallX -= floor(info->rc.wallX);
info->rc.textX = (int)(info->rc.wallX * (double)TEXTWIDTH);
if (info->rc.side == 0 && info->rc.rayDirX > 0)
info->rc.textX = TEXTWIDTH - info->rc.textX - 1;
if (info->rc.side == 1 && info->rc.rayDirY < 0)
info->rc.textX = TEXTWIDTH - info->rc.textX - 1;
info->rc.step = 1.0 * TEXTHEIGHT / info->rc.lineHeight;
info->rc.textPos = norm * info->rc.step;
info->rc.y = info->rc.drawStart;
}