-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprite_management.c
56 lines (51 loc) · 1.81 KB
/
sprite_management.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
/* ************************************************************************** */
/* */
/* :::::::: */
/* sprite_management.c :+: :+: */
/* +:+ */
/* By: cschuijt <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/11/23 14:16:47 by cschuijt #+# #+# */
/* Updated: 2022/11/23 14:16:47 by cschuijt ######## odam.nl */
/* */
/* ************************************************************************** */
#include "so_long.h"
#include <stdlib.h>
t_sprite *add_new_sprite(t_map *map, uint8_t **sheet, size_t index)
{
t_sprite *sprite;
t_sprite *end_of_list;
sprite = ft_calloc(sizeof(t_sprite), 1);
if (!sprite)
return (NULL);
sprite->spritesheet = sheet;
sprite->index = index;
sprite->image = mlx_new_image(map->mlx, 32, 32);
sprite->animation_frames = 0;
free(sprite->image->pixels);
sprite->image->pixels = sheet[index];
if (!map->sprites)
map->sprites = sprite;
else
{
end_of_list = map->sprites;
while (end_of_list->next)
end_of_list = end_of_list->next;
end_of_list->next = sprite;
}
return (sprite);
}
t_sprite *find_or_create_sprite(t_map *map, uint8_t **sheet, size_t index)
{
t_sprite *sprite;
sprite = map->sprites;
while (sprite)
{
if (sprite->index == index && sprite->spritesheet == sheet)
return (sprite);
if (!sprite->next)
break ;
sprite = sprite->next;
}
return (add_new_sprite(map, sheet, index));
}