-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.c
117 lines (106 loc) · 3.75 KB
/
sprites.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
107
108
109
110
111
112
113
114
115
116
117
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sprites.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mehtel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/03/23 19:18:42 by mehtel #+# #+# */
/* Updated: 2021/03/23 19:20:31 by mehtel ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void find_distanse_to_sprites(t_vars *vars)
{
int i;
i = -1;
while (++i < vars->map->sprite_counter)
{
vars->map->sprites_order[i] = i;
vars->map->sprites_dist[i] =
(float)(pow(vars->player[X] - vars->map->sprites[i].cord[X], 2))
+ (float)(pow(vars->player[Y]
- vars->map->sprites[i].cord[Y], 2));
}
}
void project_sprites(t_vars *vars, t_ray *cd)
{
int i;
double invert;
int *ord;
t_spr spr;
i = -1;
ord = vars->map->sprites_order;
while (++i < vars->map->sprite_counter)
{
spr.cr[X] = vars->map->sprites[ord[i]].cord[X] - vars->player[X];
spr.cr[Y] = vars->map->sprites[ord[i]].cord[Y] - vars->player[Y];
invert = 1.0 /
(cd->plane[X] * cd->dir[Y] - cd->dir[X] * cd->plane[Y]);
spr.transform[X] = invert
* (cd->dir[Y] * spr.cr[X] - cd->dir[X] * spr.cr[Y]);
spr.transform[Y] = invert
* (-cd->plane[Y] * spr.cr[X]
+ cd->plane[X] * spr.cr[Y]);
if (spr.transform[Y] < 0)
continue;
spr.screen[X] = (int)((vars->map->resolution_width >> 1)
* (1 + spr.transform[X] / spr.transform[Y]));
find_projection_size(vars, &spr);
draw_sprite(vars, &spr);
}
}
void find_projection_size(t_vars *vars, t_spr *spr)
{
int h;
h = vars->map->resolution_hight;
spr->height = abs((int)(h / spr->transform[Y] * (double)vars->win_k));
spr->draw[Y][0] = -(spr->height >> 1) + (h >> 1);
spr->draw[Y][0] *= spr->draw[Y][0] >= 0;
spr->draw[Y][1] = (spr->height >> 1) + (h >> 1);
if (spr->draw[Y][1] >= h)
spr->draw[Y][1] = h - 1;
spr->width = abs((int)(h / spr->transform[Y] * (double)vars->win_k));
spr->draw[X][0] = -(spr->width >> 1) + spr->screen[X];
spr->draw[X][0] *= spr->draw[X][0] >= 0;
spr->draw[X][1] = (spr->width >> 1) + spr->screen[X];
if (spr->draw[X][1] >= vars->map->resolution_width)
spr->draw[X][1] = vars->map->resolution_width - 1;
}
void draw_vertical_of_sprite(t_vars *vars, t_spr *spr, int *tex,
int start_y)
{
int color;
int d;
spr->draw[Y][0] = start_y;
while (spr->draw[Y][0]++ < spr->draw[Y][1] - 1)
{
d = (spr->draw[Y][0]) * 256 - vars->map->resolution_hight
* 128 + spr->height * 128;
tex[Y] = ((d * vars->texs[S]->size[Y]) / spr->height) / 256;
color = *(int*)(vars->texs[S]->addr
+ vars->texs[S]->line_length * tex[Y]
+ tex[X] * vars->texs[S]->bits_per_pixel / 8);
if ((color & 0x00FFFFFF) != 0)
pixel_put(vars, spr->draw[X][0], spr->draw[Y][0], color);
}
}
void draw_sprite(t_vars *vars, t_spr *spr)
{
int tex[2];
int y;
y = spr->draw[Y][0];
while (spr->draw[X][0] < spr->draw[X][1])
{
tex[X] = (int)(256 * (spr->draw[X][0]
- (-spr->width / 2 + spr->screen[X]))
* vars->texs[S]->size[Y] / spr->width) / 256;
if (spr->draw[X][0] > -1
&& spr->draw[X][0] < vars->map->resolution_width
&& spr->transform[Y] < vars->map->zbuf[spr->draw[X][0]])
{
draw_vertical_of_sprite(vars, spr, tex, y);
}
spr->draw[X][0]++;
}
}