-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsprites.c
134 lines (124 loc) · 4.17 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* sprites.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kkalinic <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/08 17:22:54 by kkalinic #+# #+# */
/* Updated: 2021/05/08 17:36:26 by kkalinic ### ########.fr */
/* */
/* ************************************************************************** */
#include "headers/cub_head.h"
static void stripe_p(t_data *mlx_s, double *zbuffer, t_s *spr_s)
{
int texX;
int texY;
int y;
int d;
unsigned int color;
while (spr_s->stripe < spr_s->drawEndX)
{
texX = (int )(256 * (spr_s->stripe - (-spr_s->spriteWidth / 2
+ spr_s->spriteScreenX)) * 150
/ spr_s->spriteWidth) / 256;
//printf("%5d%5d%5d%5d\n", spr_s->transformY > 0, spr_s->stripe > 0, spr_s->stripe
// < mlx_s->width, spr_s->transformY < zbuffer[spr_s->stripe]);
//printf("zb %5f transformY %5f stripe %5d\n", zbuffer[spr_s->stripe], spr_s->transformY, spr_s->stripe);
if (spr_s->transformY > 0 && spr_s->stripe > 0 && spr_s->stripe
< mlx_s->width && spr_s->transformY < zbuffer[spr_s->stripe])
{
y = spr_s->drawStartY - 1;
while (++y < spr_s->drawEndY)
{
d = (y) * 256 - mlx_s->height * 128 + spr_s->spriteHeight * 128;
texY = ((d * 180) / spr_s->spriteHeight) / 256;
mlx_pixel_get(mlx_s->tex->sprite, texX, texY, &color);
if ((color & 0x00FFFFFF) != 0)
my_mlx_pixel_put(mlx_s, spr_s->stripe, y, color);
}
}
spr_s->stripe++;
}
}
int *sort_sp(t_data *mlx_s, t_ray *ray)
{
int i;
int *spOrder;
double *spDist;
i = -1;
spOrder = malloc(sizeof(int) * mlx_s->spriteNum);
spDist = malloc(sizeof(double) * mlx_s->spriteNum);
if (!spOrder || !spDist)
error(1);
while (++i < mlx_s->spriteNum)
{
spOrder[i] = i;
spDist[i] = ((ray->pos->x - mlx_s->sprite[i]->x)
* (ray->pos->x - mlx_s->sprite[i]->x)
+ (ray->pos->y - mlx_s->sprite[i]->y)
* (ray->pos->y - mlx_s->sprite[i]->y));
}
bubbleSort(spDist, spOrder, mlx_s->spriteNum);
free(spDist);
return (spOrder);
}
void extra_sp(t_doub *db, t_data *mlx_s, t_ray *ray, t_s *spr_s)
{
db->invDet = 1.0 / (ray->plane->x
* ray->dir->y - ray->dir->x * ray->plane->y);
db->transformX = db->invDet
* (ray->dir->y * db->spriteX - ray->dir->x * db->spriteY);
spr_s->transformY = db->invDet
* (-ray->plane->y * db->spriteX + ray->plane->x * db->spriteY);
spr_s->spriteScreenX = (int)((mlx_s->width / 2)
* (1 + db->transformX / spr_s->transformY));
spr_s->spriteHeight = abs((int)(mlx_s->height / (spr_s->transformY)));
spr_s->drawStartY = -spr_s->spriteHeight / 2 + mlx_s->height / 2;
spr_s->drawEndY = spr_s->spriteHeight / 2 + mlx_s->height / 2;
}
void print_sprite(t_data *mlx_s, t_ray *ray, double *zbuffer)
{
t_s spr_s;
int i;
int *spOrder;
t_doub db;
i = mlx_s->spriteNum;
spOrder = sort_sp(mlx_s, ray);
while (--i >= 0)
{
db.spriteX = mlx_s->sprite[spOrder[i]]->x - ray->pos->x;
db.spriteY = mlx_s->sprite[spOrder[i]]->y - ray->pos->y;
extra_sp(&db, mlx_s, ray, &spr_s);
limits(&spr_s.drawStartY, &spr_s.drawEndY, mlx_s->height - 1);
spr_s.spriteWidth = abs((int)(mlx_s->height / (spr_s.transformY)));
spr_s.drawStartX = -spr_s.spriteWidth / 2 + spr_s.spriteScreenX;
spr_s.drawEndX = spr_s.spriteWidth / 2 + spr_s.spriteScreenX;
limits(&spr_s.drawStartX, &spr_s.drawEndX, mlx_s->width - 1);
spr_s.stripe = spr_s.drawStartX;
//printf("---%d-----\n", spr_s.stripe);
stripe_p(mlx_s, zbuffer, &spr_s);
}
free(spOrder);
}
void s_place(char **map, t_sprite **sprites)
{
int i;
int j;
int n;
i = -1;
j = -1;
n = 0;
while (map[++i])
{
while (map[i][++j])
{
if (map[i][j] == '2')
{
sprites[n] = put_s_place(i, j);
n++;
}
}
j = -1;
}
}