This repository has been archived by the owner on Jun 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
sprites.h
193 lines (155 loc) · 3.63 KB
/
sprites.h
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#ifndef _SPRITES_H
#define _SPRITES_H
#ifndef NULL
#define NULL ((uchar *)0)
#endif
#define MAX_SPRITES 8
#define PHEAD 0
#define PBODY 1
#define ST_ALL 254 // all but the player!
#define ST_PLAYER 1
#define ST_PLATFORM 2
#define ST_OBJECT 4
#define ST_ZAP 8
#define ST_EZAP 16
#define ST_WZAP 32
#define ST_ENEMY 64
#define ST_EFX 128
#define OBJ_USED 255
// this is usually fullscreen; define it in your main
extern struct sp1_Rect cr;
struct ssprites {
struct sp1_ss *s;
// position
uchar x;
uchar y;
// movement
char ix;
char iy;
uchar frame;
uchar delay;
uchar alive;
uchar type;
uchar subtype;
struct ssprites *n;
};
struct ssprites sprites[MAX_SPRITES];
// lists
struct ssprites *sp_free;
struct ssprites *sp_used;
// shared iterators
struct ssprites *sp_iter, *sp_iter2;
// if there are items to garbage collect
int sp_collect = 0;
void
init_sprites()
{
// to be run ONCE
sprites[PBODY].s = sp1_CreateSpr(SP1_DRAW_MASK2LB, SP1_TYPE_2BYTE, 2, 8, 1);
sp1_AddColSpr(sprites[PBODY].s, SP1_DRAW_MASK2, 0, 40, 0);
sp1_AddColSpr(sprites[PBODY].s, SP1_DRAW_MASK2RB, 0, 0, 0);
sprites[PBODY].alive = 1;
sprites[PBODY].n = NULL;
sprites[PBODY].type = ST_PLAYER;
sprites[PHEAD].s = sp1_CreateSpr(SP1_DRAW_MASK2LB, SP1_TYPE_2BYTE, 2, 8, 1);
sp1_AddColSpr(sprites[PHEAD].s, SP1_DRAW_MASK2, 0, 40, 0);
sp1_AddColSpr(sprites[PHEAD].s, SP1_DRAW_MASK2RB, 0, 0, 0);
sprites[PHEAD].alive = 1;
sprites[PHEAD].n = sprites + 1;
sprites[PHEAD].type = ST_PLAYER;
// init our lists
sp_used = sprites;
sp_free = sprites + 2; // the player is two sprites
for (i = 2; i < MAX_SPRITES - 1; ++i)
{
sprites[i].n = sprites + i + 1;
sprites[i].alive = 0;
}
sprites[MAX_SPRITES - 1].n = NULL;
sprites[MAX_SPRITES - 1].alive = 0;
}
void collect_sprites();
int
add_sprite()
{
struct ssprite *t;
if (!sp_free)
{
// FIXME
zx_border(INK_BLUE);
return 0;
}
t = sp_used;
sp_used = sp_free;
sp_free = sp_free->n;
memset(sp_used, 0, sizeof(struct ssprites));
sp_used->n = t;
sp_used->alive = 1;
return 1;
}
void
collect_sprites()
{
struct ssprites *t, *tp;
// enable this if you're not checking before calling!
//if (!sp_collect)
// return;
// current, previous
for (t = tp = sp_used; t && sp_collect;)
{
if (!t->alive)
{
if (t == sp_used)
{
tp = sp_free;
sp_free = sp_used;
sp_used = sp_used->n;
sp_free->n = tp;
t = tp = sp_used;
--sp_collect;
continue;
}
else
{
tp->n = t->n;
t->n = sp_free;
sp_free = t;
t = tp->n;
--sp_collect;
continue;
}
}
tp = t;
t = t->n;
}
}
void
destroy_type_sprite(uchar type)
{
struct ssprites *t;
if (!sp_used)
return;
for (t = sp_used; t; t = t->n)
if (t->alive && t->type & type)
{
sp1_MoveSprPix(t->s, &cr, NULL, 0, 200);
sp1_DeleteSpr(t->s);
t->alive = 0;
++sp_collect;
}
}
void
remove_sprite(struct ssprites *s)
{
sp1_MoveSprPix(s->s, &cr, NULL, 0, 200);
sp1_DeleteSpr(s->s);
s->alive = 0;
++sp_collect;
}
void
hide_all_sprites()
{
for (sp_iter = sp_used; sp_iter; sp_iter = sp_iter->n)
sp1_MoveSprPix(sp_iter->s, &cr, sp_iter->s->frame, 0, 200);
}
#endif // _SPRITES_H