-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sprite_projection.c
141 lines (130 loc) · 3.6 KB
/
Sprite_projection.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Sprite_projection.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hgrissen <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/10 11:36:38 by hgrissen #+# #+# */
/* Updated: 2021/03/06 15:23:26 by hgrissen ### ########.fr */
/* */
/* ************************************************************************** */
#include "cub3d.h"
void init_sprites(void)
{
int i;
int j;
int k;
int w;
int h;
k = 0;
i = -1;
g_sp = malloc(sizeof(t_sprite) * (g_s_count + 1));
if (!(g_si.img = mlx_xpm_file_to_image(g_mlx.mlx, g_prm.s, &w, &h)))
texture_error(5);
g_si.addr = mlx_get_data_addr(g_si.img, &g_si.bpp, &g_si.ll, &g_si.end);
while (g_prm.map[++i] && (j = -1) && (k < g_s_count))
{
while (g_prm.map[i][++j] && (k < g_s_count))
if (g_prm.map[i][j] == '2')
{
g_sp[k].x = (float)((j + 0.5) * TILE_SIZE);
g_sp[k].y = (float)((i + 0.5) * TILE_SIZE);
g_sp[k].dist = sqrtf(((g_sp[k].x) - g_p.x) * ((g_sp[k].x)
- g_p.x) + ((g_sp[k].y) - g_p.y) * ((g_sp[k].y) - g_p.y));
k++;
}
}
}
void sprite_sort(void)
{
int i;
int j;
t_sprite tmp;
i = 0;
while (i < g_s_count - 1)
{
j = i;
while (j < g_s_count - 1)
{
if (g_sp[j].dist < g_sp[j + 1].dist)
{
tmp = g_sp[j];
g_sp[j] = g_sp[j + 1];
g_sp[j + 1] = tmp;
}
j++;
}
i++;
}
}
void sprites_conf(void)
{
float angle;
int k;
k = -1;
sprite_sort();
angle = 0;
while (++k < g_s_count)
{
g_sp[k].dist = sqrtf(((g_sp[k].x) - g_p.x) * ((g_sp[k].x)
- g_p.x) + ((g_sp[k].y) - g_p.y) * ((g_sp[k].y) - g_p.y));
angle = atan2f(g_sp[k].y - g_p.y, g_sp[k].x - g_p.x);
while (angle - g_p.rotang > M_PI)
angle -= 2 * M_PI;
while (angle - g_p.rotang < -M_PI)
angle += 2 * M_PI;
g_sp[k].size = (float)((g_prm.w / g_sp[k].dist) * TILE_SIZE);
g_sp[k].yof = (float)(g_prm.h / 2 - g_sp[k].size / 2);
g_sp[k].xof = (float)(((deg(angle) - deg(g_p.rotang)) * g_prm.w)
/ (TILE_SIZE - (TILE_SIZE / 2)) + ((g_prm.w / 2) - (int)g_sp[k].size / 2));
draw_sprite(k);
}
}
unsigned int s_shadow(unsigned int color, int id)
{
unsigned int r;
unsigned int g;
unsigned int b;
float fact;
unsigned int dark;
if (BON == 0)
return (color);
fact = LGHT * 2 / g_sp[id].dist;
if (fact > 1)
return (color);
r = (((color >> 16) & 0xFF)) * fact;
g = (((color >> 8) & 0xFF)) * fact;
b = ((color) & (0xFF)) * fact;
dark = rgb_to_int(0, r, g, b);
if (dark > color)
dark = color;
return (dark);
}
void draw_sprite(int id)
{
int i;
int j;
int c;
i = -1;
g_sz = g_sp[id].size;
g_spad = (int*)g_si.addr;
while (++i < g_sz - 1 && (j = -1))
{
if (g_sp[id].xof + i <= 0 || g_sp[id].xof + i > g_prm.w - 1)
continue ;
if (g_rays[(int)(g_sp[id].xof + i)].dist <= g_sp[id].dist)
continue ;
while (++j < g_sz - 1)
{
if (g_sp[id].yof + j <= (g_p.crouch)
|| g_sp[id].yof + j > g_prm.h + (g_p.crouch) - 1)
continue ;
c = g_spad[(int)((TILE_SIZE) * (TILE_SIZE * j / (int)g_sz)
+ (TILE_SIZE * i / (int)g_sz))];
if (c != g_spad[0])
my_mlx_pixel_put(&g_img, i + g_sp[id].xof, j + g_sp[id].yof
- g_p.crouch, s_shadow(c, id));
}
}
}