Repository navigation
Expand file tree
/
Copy pathentity.c
More file actions
95 lines (82 loc) · 1.78 KB
/
Copy pathentity.c
File metadata and controls
95 lines (82 loc) · 1.78 KB
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
/*
* 2021, Maksymilian Mruszczak <u at one u x dot o r g>
*
* Entities procedures
*/
#include "MKL05Z4.h"
#include "sprite.h"
#include "entity.h"
#define NULL (void *)0
#define ENTITY_CNT 32
static Entity entity_buf[ENTITY_CNT];
static Entity *entity_first;
static Entity *entity_last;
void
init_entities(void)
{
int i;
for (i = 0; i < ENTITY_CNT; ++i)
{
entity_buf[i].exists = 0;
entity_buf[i].val = 0;
}
entity_first = NULL;
entity_last = NULL;
}
Entity *
create_entity(const Sprite *spr, const Sprite *alt, const Sprite *alt2, uint8_t x, uint8_t y, uint8_t t)
{
int i = -1; /* search for free space */
while (entity_buf[++i].exists)
if (i+1 >= ENTITY_CNT)
return NULL;
entity_buf[i].type = t;
entity_buf[i].exists = 1;
entity_buf[i].frame = 0;
entity_buf[i].x = x;
entity_buf[i].y = y;
entity_buf[i].val = 0;
entity_buf[i].sprite[0] = (Sprite *)spr;
entity_buf[i].sprite[1] = (Sprite *)alt;
entity_buf[i].sprite[2] = (Sprite *)alt2;
if (entity_last == NULL) {
entity_buf[i].prev = NULL;
entity_first = &entity_buf[i];
} else {
entity_buf[i].prev = entity_last;
entity_last->next = &entity_buf[i];
}
entity_buf[i].next = NULL;
entity_last = &entity_buf[i];
return entity_last;
}
void
delete_entity(Entity *e)
{
clear_sprite(e->sprite[e->frame], e->x, e->y);
/* relink list and freeup the slot */
if (e->prev != NULL)
e->prev->next = e->next;
if (e->next != NULL)
e->next->prev = e->prev;
if (e == entity_first)
entity_first = e->next;
if (e == entity_last)
entity_last = e->prev;
e->exists = 0;
e->val = 0;
}
void
render_entity(Entity *e)
{
draw_sprite(e->sprite[e->frame], e->x, e->y);
}
void
render_entities(void)
{
Entity *e = entity_first;
while (e != NULL) {
draw_sprite(e->sprite[e->frame], e->x, e->y);
e = e->next;
}
}