-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.c
100 lines (90 loc) · 2.29 KB
/
map.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: schaaban <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/12/05 13:30:58 by schaaban #+# #+# */
/* Updated: 2017/12/08 05:33:02 by schaaban ### ########.fr */
/* */
/* ************************************************************************** */
#include "fillit.h"
#include "libft/libft.h"
#include <stdlib.h>
t_map **map_creator(int count)
{
t_map **tab;
int i;
i = 0;
if ((tab = (t_map**)malloc(sizeof(t_map*) * (count + 1))) == NULL)
return (NULL);
while (i < count)
{
if ((tab[i] = (t_map*)malloc(sizeof(t_map))) == NULL)
return (NULL);
i++;
}
tab[i] = NULL;
return (tab);
}
void map_init(t_map *self, int map_size)
{
int i;
i = 0;
self->map_size = map_size;
if ((self->tab = (uint16_t*)malloc(sizeof(uint16_t) * 12)) == NULL)
return ;
while (i < 12)
{
self->tab[i] = 0;
i++;
}
}
int map_add_tetri(t_map *self, t_tetri *tetri)
{
uint16_t tetri_map_value;
int i;
i = 0;
while (i < tetri->height)
{
if ((tetri_good(tetri, self->map_size, i)
& self->tab[i + tetri->y]) != 0)
return (0);
i++;
}
i = 0;
while (i < tetri->height)
{
tetri_map_value = tetri_good(tetri, self->map_size, i);
self->tab[i + tetri->y] = tetri_map_value | self->tab[i + tetri->y];
i++;
}
return (1);
}
int map_rem_tetri(t_map *self, t_tetri *tetri)
{
uint16_t tetri_map_value;
int i;
i = 0;
while (i < tetri->height)
{
tetri_map_value = tetri_good(tetri, self->map_size, i);
self->tab[i + tetri->y] = self->tab[i + tetri->y] - tetri_map_value;
i++;
}
return (1);
}
void map_dtor(t_map *self)
{
if (self != NULL)
{
if (self->tab != NULL)
{
free(self->tab);
self->tab = NULL;
}
free(self);
self = NULL;
}
}