-
Notifications
You must be signed in to change notification settings - Fork 12
/
sprite.c
301 lines (273 loc) · 6.23 KB
/
sprite.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* Digger Remastered
Copyright (c) Andrew Jenner 1998-2004 */
#include <assert.h>
#include <stdlib.h>
#include "def.h"
#include "sprite.h"
#include "hardware.h"
#include "draw_api.h"
static bool retrflag=true;
static bool sprrdrwf[SPRITES+1];
static bool sprrecf[SPRITES+1];
static bool sprenf[SPRITES];
static int16_t sprch[SPRITES+1];
static uint8_t *sprmov[SPRITES];
static int16_t sprx[SPRITES+1];
static int16_t spry[SPRITES+1];
static int16_t sprwid[SPRITES+1];
static int16_t sprhei[SPRITES+1];
static int16_t sprbwid[SPRITES];
static int16_t sprbhei[SPRITES];
static int16_t sprnch[SPRITES];
static int16_t sprnwid[SPRITES];
static int16_t sprnhei[SPRITES];
static int16_t sprnbwid[SPRITES];
static int16_t sprnbhei[SPRITES];
static void clearrdrwf(void);
static void clearrecf(void);
static void setrdrwflgs(int16_t n);
static bool collide(int16_t bx,int16_t si);
static bool bcollide(int16_t bx,int16_t si);
static void putims(void);
static void putis(void);
static void bcollides(int bx);
#if defined(DIGGER_DEBUG)
static void gwrite_debug(int16_t x, int16_t y, int16_t ch, int16_t c);
#endif
static const struct digger_draw_api dda_static = {
.ginit = &vgainit,
.gclear = &vgaclear,
.gpal = &vgapal,
.ginten = &vgainten,
.gputi = &vgaputi,
.ggeti = &vgageti,
.gputim = &vgaputim,
.ggetpix = &vgagetpix,
.gtitle = &vgatitle,
#if !defined(DIGGER_DEBUG)
.gwrite = &vgawrite,
#else
.gwrite = &gwrite_debug,
#endif
.gflush = &doscreenupdate
};
const struct digger_draw_api *ddap = &dda_static;
void setretr(bool f)
{
retrflag=f;
}
void createspr(int16_t n,int16_t ch,uint8_t *mov,int16_t wid,int16_t hei,int16_t bwid,
int16_t bhei)
{
sprnch[n]=sprch[n]=ch;
sprmov[n]=mov;
sprnwid[n]=sprwid[n]=wid;
sprnhei[n]=sprhei[n]=hei;
sprnbwid[n]=sprbwid[n]=bwid;
sprnbhei[n]=sprbhei[n]=bhei;
sprenf[n]=false;
}
void movedrawspr(int16_t n,int16_t x,int16_t y)
{
sprx[n]=x&-4;
spry[n]=y;
sprch[n]=sprnch[n];
sprwid[n]=sprnwid[n];
sprhei[n]=sprnhei[n];
sprbwid[n]=sprnbwid[n];
sprbhei[n]=sprnbhei[n];
clearrdrwf();
setrdrwflgs(n);
putis();
ddap->ggeti(sprx[n],spry[n],sprmov[n],sprwid[n],sprhei[n]);
sprenf[n]=true;
sprrdrwf[n]=true;
putims();
}
void erasespr(int16_t n)
{
if (!sprenf[n])
return;
ddap->gputi(sprx[n],spry[n],sprmov[n],sprwid[n],sprhei[n]);
sprenf[n]=false;
clearrdrwf();
setrdrwflgs(n);
putims();
}
void drawspr(int16_t n,int16_t x,int16_t y)
{
int16_t t1,t2,t3,t4;
x&=-4;
clearrdrwf();
setrdrwflgs(n);
t1=sprx[n];
t2=spry[n];
t3=sprwid[n];
t4=sprhei[n];
sprx[n]=x;
spry[n]=y;
sprwid[n]=sprnwid[n];
sprhei[n]=sprnhei[n];
clearrecf();
setrdrwflgs(n);
sprhei[n]=t4;
sprwid[n]=t3;
spry[n]=t2;
sprx[n]=t1;
sprrdrwf[n]=true;
putis();
sprenf[n]=true;
sprx[n]=x;
spry[n]=y;
sprch[n]=sprnch[n];
sprwid[n]=sprnwid[n];
sprhei[n]=sprnhei[n];
sprbwid[n]=sprnbwid[n];
sprbhei[n]=sprnbhei[n];
ddap->ggeti(sprx[n],spry[n],sprmov[n],sprwid[n],sprhei[n]);
putims();
bcollides(n);
}
void initspr(int16_t n,int16_t ch,int16_t wid,int16_t hei,int16_t bwid,int16_t bhei)
{
sprnch[n]=ch;
sprnwid[n]=wid;
sprnhei[n]=hei;
sprnbwid[n]=bwid;
sprnbhei[n]=bhei;
}
void initmiscspr(int16_t x,int16_t y,int16_t wid,int16_t hei)
{
sprx[SPRITES]=x;
spry[SPRITES]=y;
sprwid[SPRITES]=wid;
sprhei[SPRITES]=hei;
clearrdrwf();
setrdrwflgs(SPRITES);
putis();
}
void getis(void)
{
int16_t i;
for (i=0;i<SPRITES;i++)
if (sprrdrwf[i])
ddap->ggeti(sprx[i],spry[i],sprmov[i],sprwid[i],sprhei[i]);
putims();
}
void drawmiscspr(int16_t x,int16_t y,int16_t ch,int16_t wid,int16_t hei)
{
sprx[SPRITES]=x&-4;
spry[SPRITES]=y;
sprch[SPRITES]=ch;
sprwid[SPRITES]=wid;
sprhei[SPRITES]=hei;
ddap->gputim(sprx[SPRITES],spry[SPRITES],sprch[SPRITES],sprwid[SPRITES],
sprhei[SPRITES]);
}
static void clearrdrwf(void)
{
int16_t i;
clearrecf();
for (i=0;i<SPRITES+1;i++)
sprrdrwf[i]=false;
}
static void clearrecf(void)
{
int16_t i;
for (i=0;i<SPRITES+1;i++)
sprrecf[i]=false;
}
static void setrdrwflgs(int16_t n)
{
int16_t i;
if (!sprrecf[n]) {
sprrecf[n]=true;
for (i=0;i<SPRITES;i++)
if (sprenf[i] && i!=n) {
if (collide(i,n)) {
sprrdrwf[i]=true;
setrdrwflgs(i);
}
}
}
}
static bool collide(int16_t bx,int16_t si)
{
if (sprx[bx]>=sprx[si]) {
if (sprx[bx]>(sprwid[si]<<2)+sprx[si]-1)
return false;
}
else
if (sprx[si]>(sprwid[bx]<<2)+sprx[bx]-1)
return false;
if (spry[bx]>=spry[si]) {
if (spry[bx]<=sprhei[si]+spry[si]-1)
return true;
return false;
}
if (spry[si]<=sprhei[bx]+spry[bx]-1)
return true;
return false;
}
static bool bcollide(int16_t bx,int16_t si)
{
if (sprx[bx]>=sprx[si]) {
if (sprx[bx]+sprbwid[bx]>(sprwid[si]<<2)+sprx[si]-sprbwid[si]-1)
return false;
}
else
if (sprx[si]+sprbwid[si]>(sprwid[bx]<<2)+sprx[bx]-sprbwid[bx]-1)
return false;
if (spry[bx]>=spry[si]) {
if (spry[bx]+sprbhei[bx]<=sprhei[si]+spry[si]-sprbhei[si]-1)
return true;
return false;
}
if (spry[si]+sprbhei[si]<=sprhei[bx]+spry[bx]-sprbhei[bx]-1)
return true;
return false;
}
static void putims(void)
{
int i;
for (i=0;i<SPRITES;i++)
if (sprrdrwf[i])
ddap->gputim(sprx[i],spry[i],sprch[i],sprwid[i],sprhei[i]);
}
static void putis(void)
{
int i;
for (i=0;i<SPRITES;i++)
if (sprrdrwf[i])
ddap->gputi(sprx[i],spry[i],sprmov[i],sprwid[i],sprhei[i]);
}
int first[TYPES],coll[SPRITES];
static int firstt[TYPES]={FIRSTBONUS,FIRSTBAG,FIRSTMONSTER,FIRSTFIREBALL,FIRSTDIGGER};
static int lastt[TYPES]={LASTBONUS,LASTBAG,LASTMONSTER,LASTFIREBALL,LASTDIGGER};
static void bcollides(int spr)
{
int spc,next,i;
for (next=0;next<TYPES;next++)
first[next]=-1;
for (next=0;next<SPRITES;next++)
coll[next]=-1;
for (i=0;i<TYPES;i++) {
next=-1;
for (spc=firstt[i];spc<lastt[i];spc++)
if (sprenf[spc] && spc!=spr)
if (bcollide(spr,spc)) {
if (next==-1)
first[i]=next=spc;
else
coll[next=(coll[next]=spc)]=-1;
}
}
}
#if defined(DIGGER_DEBUG)
static void gwrite_debug(int16_t x, int16_t y, int16_t ch, int16_t c)
{
assert(x + CHR_W <= MAX_W);
assert(y + CHR_H <= MAX_H);
vgawrite(x, y, ch, c);
}
#endif