-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimgfile.c
294 lines (243 loc) · 5.3 KB
/
imgfile.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
/* :ts=8 bk=0
*
* imgfile.c: Routines to parse and load 3DO image files.
*
* Leo L. Schwab 9301.04
*/
#include <types.h>
#include <mem.h>
#include <graphics.h>
#include <form3do.h>
#include "castle.h"
#include "imgfile.h"
#include "app_proto.h"
typedef struct memfile {
int32 *start;
int32 *end;
int32 *scan;
} memfile;
/***************************************************************************
* Local function prototypes. (I hate ANSI.)
*/
static CelArray *parseanim(memfile *);
static CelArray *parsecat(memfile *);
static void filloutccb(CCB *, void *, int32, void *, int32);
/***************************************************************************
* Parsing code.
*/
struct CelArray *
parse3DO (filename)
char *filename;
{
memfile mf;
int32 errlen;
/*
* Load file.
*/
if (!(mf.start = allocloadfile (filename, MEMTYPE_CEL, &errlen)))
{
filerr (filename, errlen);
return (NULL);
}
mf.scan = mf.start;
mf.end = (int32 *) ((ubyte *) mf.start + errlen);
if (*mf.scan == CHUNK_ANIM)
return (parseanim (&mf));
else if (*mf.scan == CHUNK_CCB)
return (parsecat (&mf));
else
die ("WTFIGO?\n");
}
struct CelArray *
alloccelarray (nentries)
int32 nentries;
{
return (AllocMem (sizeof (CelArray) +
(nentries - 1) * sizeof (CCB *),
0));
}
static CelArray *
parseanim (mf)
memfile *mf;
{
register AnimChunk *ac;
register CelArray *cels;
register int32 *buf;
register int i;
CCB *ccb;
int32 curplutsiz;
void *curplut;
/*
* Allocate CelArray.
*/
buf = mf->scan;
ac = (AnimChunk *) buf;
i = ac->numFrames;
if (!(cels = alloccelarray (i)))
die ("Can't allocate cel array (parseanim).\n");
cels->ncels = i;
if (ac->animType == 1) {
/*
* The file contains only one CCB. Create new ones.
*/
if (!(ccb = ALLOCMEM (sizeof (CCB) * (i - 1),
MEMTYPE_CEL | MEMTYPE_FILL)))
die ("Can't allocate CCB's to load imagery.\n");
/*
* Initialize pointer array.
*/
while (--i > 0)
cels->celptrs[i] = ccb + i - 1;
cels->ca_Buffer = mf->start;
cels->ca_BufSiz = (int) mf->end - (int) mf->start;
cels->ca_Type = CHUNK_ANIM;
}
ccb = curplut = NULL;
i = 0;
buf = (int32 *) ((ubyte *) buf + *(buf + 1));
while (i >= 0) {
switch (*buf) {
case CHUNK_CCB:
{
/*
* We've found the one and only CCB in this file.
* Replicate it.
*/
register int n;
ccb = cels->celptrs[0] =
(CCB *) (&((CCC *) buf)->ccb_Flags);
for (n = ac->numFrames; --n > 0; )
*cels->celptrs[n] = *ccb;
break;
}
case CHUNK_PLUT:
curplut = &((PLUTChunk *) buf)->PLUT;
curplutsiz = ((PLUTChunk *) buf)->numentries;
break;
case CHUNK_PDAT:
filloutccb (ccb,
curplut, curplutsiz,
buf + 2, *(buf + 1) - 8);
if (++i >= ac->numFrames)
i = -1; /* Force loop exit */
ccb = cels->celptrs[i];
break;
}
buf = (int32 *) ((ubyte *) buf + *(buf + 1));
}
mf->scan = buf;
return (cels);
}
static CelArray *
parsecat (mf)
memfile *mf;
{
register CelArray *cels;
register int32 *buf;
register int ncels, i;
CCB *curccb;
int32 curplutsiz, curpdatsiz;
void *curplut, *curpdat;
buf = mf->scan;
ncels = 0;
/*
* Count number of cels in file.
*/
while (1) {
if (buf >= mf->end ||
*buf == CHUNK_ANIM)
break;
if (*buf == CHUNK_CCB)
ncels++;
buf = (int32 *) ((ubyte *) buf + *(buf + 1));
}
/*
* Allocate CelArray.
*/
if (!(cels = alloccelarray (ncels)))
die ("Can't allocate cel array (parsecat).\n");
/*
* Fill CelArray.
*/
cels->ca_Buffer = mf->start;
cels->ca_BufSiz = (int) mf->end - (int) mf->start;
cels->ca_Type = CHUNK_CCB;
cels->ncels = ncels;
buf = mf->scan;
i = 0;
curccb = curplut = curpdat = NULL;
while (buf < mf->end) {
switch (*buf) {
case CHUNK_CCB:
if (curccb) {
filloutccb (curccb,
curplut, curplutsiz,
curpdat, curpdatsiz);
cels->celptrs[i] = curccb;
ncels--;
i++;
}
curccb = (CCB *) (&((CCC *) buf)->ccb_Flags);
break;
case CHUNK_PLUT:
curplut = &((PLUTChunk *) buf)->PLUT;
curplutsiz = ((PLUTChunk *) buf)->numentries;
break;
case CHUNK_PDAT:
curpdat = buf + 2;
curpdatsiz = *(buf + 1) - 8; // This are the size.
break;
}
buf = (int32 *) ((ubyte *) buf + *(buf + 1));
}
/*
* Possibly redundant step, depending on file layout.
*/
filloutccb (curccb, curplut, curplutsiz, curpdat, curpdatsiz);
cels->celptrs[i] = curccb;
return (cels);
}
static void
filloutccb (ccb, plut, plutsiz, pdat, pdatsiz)
register CCB *ccb;
void *plut, *pdat;
int32 plutsiz, pdatsiz;
{
if (ccb->ccb_Flags & CCB_LDPLUT) {
ccb->ccb_PLUTPtr = plut;
ccb->ccb_Flags |= CCB_PPABS;
}
ccb->ccb_SourcePtr = pdat;
ccb->ccb_Flags |= CCB_SPABS;
ccb->ccb_HDX = ONE_HD;
ccb->ccb_VDY = ONE_VD;
ccb->ccb_HDY =
ccb->ccb_VDX =
ccb->ccb_HDDX =
ccb->ccb_HDDY = 0;
ccb->ccb_Width = cvt2power (ccb->ccb_Width);
ccb->ccb_Height = cvt2power (ccb->ccb_Height);
}
void
freecelarray (ca)
register struct CelArray *ca;
{
if (ca) {
if (ca->ca_Type == CHUNK_ANIM)
FreeMem (ca->celptrs[1],
sizeof (CCB) * (ca->ncels - 1));
if (ca->ca_Buffer)
FreeMem (ca->ca_Buffer, ca->ca_BufSiz);
FreeMem (ca, sizeof(*ca) + sizeof (CCB *) * (ca->ncels - 1));
}
}
int32
cvt2power (val)
register int32 val;
{
register int32 i;
for (i = 32; --i >= 0; )
if (val == (1 << i))
return (-i);
return (val);
}