aboutsummaryrefslogtreecommitdiffstats
path: root/src/ft_sort_s_t.c
blob: 433fd0767046d35796eeb817ca3326cebf0cb048 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_sort_s_t.c                                      :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/24 20:22:45 by rbousset          #+#    #+#             */
/*   Updated: 2020/03/09 18:56:01 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

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

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);
}

static int8_t
	ft_set_current_sprite(t_cub *cl, int8_t j)
{
	const int8_t	st_o = cl->mlist.st_o[j];

	if (st_o == 0)
		return (4);
	else if (st_o >= 1 && st_o <= 7)
		return (st_o + 7);
	else if (st_o == 8)
		return (15);
	else if (st_o == 9)
		return (17);
	else if (st_o >= 10)
		return (st_o + 8);
	return (0);
}

static void
	ft_alloc_big_t(t_cub *cl)
{
	int32_t	i;

	if (!(cl->big_t = (float**)malloc((cl->big_t_val) * sizeof(float*))))
	{
		ft_alloc_error(cl);
	}
	i = 0;
	while (i < cl->big_t_val)
	{
		if (!(cl->big_t[i] = (float*)malloc(4 * sizeof(float))))
		{
			ft_alloc_error(cl);
		}
		i++;
	}
}

static void
	ft_fill_big_t(float **dist_tab, t_cub *cl)
{
	int32_t	i;
	int32_t	j;
	int32_t	k;

	i = 0;
	j = 0;
	k = 0;
	while (i < cl->big_t_val)
	{
		if (dist_tab[j][k])
		{
			cl->big_t[i][0] = dist_tab[j][k];
			cl->big_t[i][1] = (float)ft_set_current_sprite(cl, j);
			cl->big_t[i][2] = (float)j;
			cl->big_t[i][3] = (float)k;
			k++;
			i++;
		}
		else
		{
			k = 0;
			j++;
		}
	}
}

static void
	ft_sort_big_t(t_cub *cl)
{
	int32_t		i;
	int8_t		j;
	float		tmp[4];

	i = 0;
	while (i < cl->big_t_val - 1)
	{
		if (cl->big_t[i][0] < cl->big_t[i + 1][0])
		{
			j = -1;
			while (++j < 4)
			{
				tmp[j] = cl->big_t[i][j];
				cl->big_t[i][j] = cl->big_t[i + 1][j];
				cl->big_t[i + 1][j] = tmp[j];
			}
			i = -1;
		}
		i++;
	}
}

static void
	ft_init_st(int32_t *st_o)
{
	int8_t	i;

	i = 0;
	while (i < FT_TOTAL_SPRT)
	{
		st_o[i] = i;
		i++;
	}
}

void
	ft_sort_s_t(t_cub *cl, float **dist_tab)
{
	int32_t		i;

	ft_init_st(cl->mlist.st_o);
	i = -1;
	while (++i < cl->big_t_val)
		ft_memdel((void*)&cl->big_t[i]);
	ft_memdel((void*)&cl->big_t);
	i = -1;
	cl->big_t_val = 0;
	while (++i < FT_TOTAL_SPRT)
		cl->big_t_val += cl->mlist.sprite_nbr[i];
	ft_alloc_big_t(cl);
	ft_fill_big_t(dist_tab, cl);
	ft_sort_big_t(cl);
	i = -1;
	while (++i < FT_TOTAL_SPRT + 1)
		ft_memdel((void*)&dist_tab[i]);
	ft_memdel((void*)&dist_tab);
}