aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_draw_sprite.c
blob: b048521dc4c057b8dca72c505e41b35e2877e538 (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_draw_sprite.c                                   :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: joelecle <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/24 20:22:45 by joelecle          #+#    #+#             */
/*   Updated: 2020/03/09 18:56:01 by joelecle         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <cub3d.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>

static float
	**ft_alloc_dist_tab(void)
{
	uint16_t	i;
	float		**dist_tab;

	if (!(dist_tab = (float **)ft_calloc((FT_TOTAL_SPRT + 1), sizeof(float*))))
		return (NULL);
	i = 0;
	while (i < FT_TOTAL_SPRT + 1)
	{
		if (!(dist_tab[i] = (float *)ft_calloc(4096, sizeof(float))))
			return (NULL);
		i++;
	}
	return (dist_tab);
}

void
	ft_sort_sprites(t_cub *cl, int16_t it, int16_t jt)
{
	float	**dist_tab;

	if (!(dist_tab = ft_alloc_dist_tab()))
		ft_alloc_error(cl);
	while (++jt < FT_TOTAL_SPRT)
	{
		if (cl->mlist.sprite_nbr[jt] == 0)
			dist_tab[jt][0] = 0;
		else
		{
			while (++it < cl->mlist.sprite_nbr[jt])
			{
				dist_tab[jt][it] =
					((cl->plist.pos_x - cl->sprites[jt][it].s_pos_x) *
					(cl->plist.pos_x - cl->sprites[jt][it].s_pos_x) +
					(cl->plist.pos_y - cl->sprites[jt][it].s_pos_y) *
					(cl->plist.pos_y - cl->sprites[jt][it].s_pos_y));
			}
			it = -1;
		}
	}
	ft_sort_s_t(cl, dist_tab);
}

static void
	ft_put_sprite(t_sprite *sprite, t_cub *cl)
{
	double		dist;
	t_rgb		rgb;

	if ((dist = sqrt(pow(cl->plist.pos_x - sprite->s_pos_x, 2)
		+ pow(cl->plist.pos_y - sprite->s_pos_y, 2))) <= 0)
		dist = 0.0001;
	rgb.r = (uint8_t)cl->tlist[sprite->current_sprite].ptr[sprite->tex_x
		* 4 + 4 * cl->tlist[sprite->current_sprite].img_w * sprite->tex_y + 2];
	rgb.g = (uint8_t)cl->tlist[sprite->current_sprite].ptr[sprite->tex_x
		* 4 + 4 * cl->tlist[sprite->current_sprite].img_w * sprite->tex_y + 1];
	rgb.b = (uint8_t)cl->tlist[sprite->current_sprite].ptr[sprite->tex_x
		* 4 + 4 * cl->tlist[sprite->current_sprite].img_w * sprite->tex_y];
	*(int*)(cl->img.ptr + ((uint16_t)sprite->x * 4 +
		(sprite->y * cl->img.sizeline))) = ft_rgb_to_hex(dist, rgb, cl);
}

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[sprite->current_sprite].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[sprite->current_sprite].img_h / 2) /
				sprite->spriteheight) / 128;
			if (sprite->transformy > 0 &&
				cl->tlist[sprite->current_sprite].ptr[sprite->tex_x * 4 + 4 *
					cl->tlist[sprite->current_sprite].img_w * sprite->tex_y]
				&& cl->rlist.wall_dist_tab[sprite->x] > sprite->transformy)
				ft_put_sprite(sprite, cl);
			sprite->y++;
		}
		sprite->x++;
	}
}