blob: 1393d385569ce4ca7b1cdf553ae6afd3eb16e208 (
plain)
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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_draw_sprite.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/24 20:22:45 by rbousset #+# #+# */
/* Updated: 2020/02/24 20:22:47 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <cub3d.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
void
ft_sort_sprites(t_cub *cl)
{
float dist_tab[4096];
uint32_t tmp;
int32_t it;
it = 0;
while (it < cl->mlist.sprite_nbr)
{
dist_tab[it] = ((cl->plist.pos_x - cl->sprites[it].s_pos_x) *
(cl->plist.pos_x - cl->sprites[it].s_pos_x) +
(cl->plist.pos_y - cl->sprites[it].s_pos_y) *
(cl->plist.pos_y - cl->sprites[it].s_pos_y));
cl->mlist.sprite_order[it] = it;
it++;
}
it = 0;
while (it < cl->mlist.sprite_nbr)
{
if (dist_tab[it] > dist_tab[it + 1])
{
tmp = dist_tab[it];
dist_tab[it] = dist_tab[it + 1];
dist_tab[it + 1] = tmp;
tmp = cl->mlist.sprite_order[it];
cl->mlist.sprite_order[it] = cl->mlist.sprite_order[it + 1];
cl->mlist.sprite_order[it + 1] = tmp;
it = 0;
}
}
}
void
ft_sprite_height(t_cub *cl, t_sprite *sprite)
{
sprite->spriteheight = abs((int)(cl->wlist.y_size / (sprite->transformy)));
sprite->drawstarty = -sprite->spriteheight / 2 + cl->wlist.y_size / 2;
if(sprite->drawstarty < 0)
sprite->drawstarty = 0;
sprite->drawendy = sprite->spriteheight / 2 + cl->wlist.y_size / 2;
if(sprite->drawendy >= (int)cl->wlist.y_size)
sprite->drawendy = cl->wlist.y_size - 1;
}
void
ft_sprite_width(t_cub *cl, t_sprite *sprite)
{
sprite->spritewidth = abs((int)(cl->wlist.x_size / (sprite->transformy)));
sprite->drawstartx = -sprite->spritewidth / 2 + sprite->spritescreenx;
if(sprite->drawstartx < 0)
sprite->drawstartx = 0;
sprite->drawendx = sprite->spritewidth / 2 + sprite->spritescreenx;
if(sprite->drawendx >= (int)cl->wlist.x_size) sprite->drawendx = cl->wlist.x_size - 1;
}
void
ft_calc_sprite(t_cub *cl)
{
t_sprite sprite;
int i = 0;
while (i < cl->mlist.sprite_nbr)
{
sprite = cl->sprites[i];
sprite.spritey = sprite.s_pos_x - (cl->plist.pos_x - 0.5);
sprite.spritex = sprite.s_pos_y - (cl->plist.pos_y - 0.5);
sprite.invdet = 1.0 / (cl->plist.plane_x * cl->plist.dir_y - cl->plist.dir_x * cl->plist.plane_y);
sprite.transformx = sprite.invdet * (cl->plist.dir_y * sprite.spritex- cl->plist.dir_x* sprite.spritey);
sprite.transformy = sprite.invdet * (-cl->plist.plane_y* sprite.spritex + cl->plist.plane_x * sprite.spritey);
sprite.spritescreenx = (int)(cl->wlist.x_size / 2) * (1 + sprite.transformx / sprite.transformy);
ft_sprite_height(cl, &sprite);
ft_sprite_width(cl, &sprite);
ft_draw_sprite(cl, &sprite);
i++;
}
}
void
ft_draw_sprite(t_cub *cl, t_sprite *sprite)
{
int32_t d;
sprite->x = sprite->drawstartx;
while (sprite->x < sprite->drawendx)
{
sprite->tex_x = (int32_t)((sprite->x - (-sprite->spritewidth / 2 +
sprite->spritescreenx)) * cl->tlist[4].img_w / sprite->spritewidth);
sprite->y = sprite->drawstarty;
while (sprite->y < sprite->drawendy)
{
d = sprite->y * 256 - cl->wlist.y_size * 128 +
sprite->spriteheight * 128;
sprite->tex_y = ((d * cl->tlist[4].img_h / 2) /
sprite->spriteheight) / 128;
if (cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y]
&& cl->rlist.wall_dist_tab[sprite->x] > sprite->transformy)
{
cl->img.ptr[sprite->x * 4 + (sprite->y * cl->img.sizeline)] =
cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y];
cl->img.ptr[sprite->x * 4 + (sprite->y *
cl->img.sizeline) + 1] =
cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y + 1];
cl->img.ptr[sprite->x * 4 + (sprite->y *
cl->img.sizeline) + 2] =
cl->tlist[4].ptr[sprite->tex_x * 4 + 4 * cl->tlist[4].img_h * sprite->tex_y + 2];
}
sprite->y++;
}
sprite->x++;
}
}
|