-
Notifications
You must be signed in to change notification settings - Fork 5
/
vclip.cpp
549 lines (431 loc) · 14 KB
/
vclip.cpp
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF OUTRAGE
ENTERTAINMENT, INC. ("OUTRAGE"). OUTRAGE, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1996-2000 OUTRAGE ENTERTAINMENT, INC. ALL RIGHTS RESERVED.
*/
#include <stdlib.h>
#include <string.h>
#include "pstypes.h"
#include "pserror.h"
#include "bitmap.h"
#include "vclip.h"
#include "CFILE.H"
#include "mono.h"
#include "ddio.h"
#include "gametexture.h"
#include "texture.h"
#include <string.h>
#include "ctype.h"
#include "mem.h"
#include "game.h"
vclip GameVClips[MAX_VCLIPS];
int Num_vclips = 0;
#define DEFAULT_FRAMETIME .07f
#define VCLIP_VERSION 1
// Frees all the memory used by vclips
void FreeAllVClips() {
mprintf((0, "Freeing all vclips!\n"));
for (int i = 0; i < MAX_VCLIPS; i++) {
if (GameVClips[i].used > 0) {
GameVClips[i].used = 1;
FreeVClip(i);
}
}
}
// Simply sets all vclips to unused
void InitVClips() {
for (int i = 0; i < MAX_VCLIPS; i++)
GameVClips[i].used = 0;
atexit(FreeAllVClips);
}
// Allocs a vclip for use
// Returns -1 on error
int AllocVClip() {
int i;
for (i = 0; i < MAX_VCLIPS; i++) {
if (GameVClips[i].used == 0) {
memset(&GameVClips[i], 0, sizeof(vclip));
GameVClips[i].frames = (short *)mem_malloc(VCLIP_MAX_FRAMES * sizeof(short));
ASSERT(GameVClips[i].frames);
GameVClips[i].frame_time = DEFAULT_FRAMETIME;
GameVClips[i].flags = VCF_NOT_RESIDENT;
GameVClips[i].used = 1;
Num_vclips++;
return i;
}
}
Int3(); // Ran out of vclips. What the hell are you doing? Get Jason
return -1;
}
void FreeVClip(int num) {
ASSERT(GameVClips[num].used > 0);
GameVClips[num].used--;
if (GameVClips[num].used > 0)
return; // other things are using this vclip
if (!(GameVClips[num].flags & VCF_NOT_RESIDENT)) {
for (int i = 0; i < GameVClips[num].num_frames; i++)
bm_FreeBitmap(GameVClips[num].frames[i]);
}
mem_free(GameVClips[num].frames);
Num_vclips--;
ASSERT(Num_vclips >= 0);
}
// Frees up the bitmaps used by a vclip
void FreeVClipResidency(int num) {
ASSERT(GameVClips[num].used > 0);
mprintf((0, "Freeing vclip residency!\n"));
if (!(GameVClips[num].flags & VCF_NOT_RESIDENT)) {
for (int i = 0; i < GameVClips[num].num_frames; i++)
bm_FreeBitmap(GameVClips[num].frames[i]);
}
GameVClips[num].flags |= VCF_NOT_RESIDENT;
}
// Saves a given video clip to a file
// Returns 1 if everything ok, 0 otherwise
// "num" is index into GameVClip array
int SaveVClip(char *filename, int num) {
CFILE *outfile;
vclip *vc = &GameVClips[num];
ASSERT(vc->used);
ASSERT(filename != NULL);
PageInVClip(num);
outfile = (CFILE *)cfopen(filename, "wb");
if (!outfile) {
mprintf((0, "Couldn't save vclip %s!\n", filename));
return 0;
}
// write out the header for this vclip
cf_WriteByte(outfile, 127);
cf_WriteByte(outfile, (sbyte)VCLIP_VERSION);
cf_WriteByte(outfile, vc->num_frames);
// cf_WriteFloat (outfile,vc->play_time);
cf_WriteFloat(outfile, vc->frame_time);
// cf_WriteInt (outfile,vc->flags);
// cf_WriteFloat (outfile,vc->light_value);
// Now save each frame of this vclip
for (int i = 0; i < vc->num_frames; i++) {
if (!bm_SaveBitmap(outfile, vc->frames[i])) {
mprintf((0, "Couldn't save frame %d of vclip %s!\n", i, filename));
Int3();
cfclose(outfile);
return 0;
}
}
cfclose(outfile);
return 1;
}
extern int Low_vidmem;
// Pages in a vclip if it needs to be
void PageInVClip(int vcnum) {
ASSERT(GameVClips[vcnum].used);
if (!(GameVClips[vcnum].flags & VCF_NOT_RESIDENT))
return;
int mipped = 0;
int texture_size = GameVClips[vcnum].target_size;
vclip *vc = &GameVClips[vcnum];
if (vc->flags & VCF_WANTS_MIPPED)
mipped = 1;
CFILE *infile = (CFILE *)cfopen(vc->name, "rb");
if (!infile) {
mprintf((0, "Couldn't load vclip %s!\n", vc->name));
return;
}
mprintf((0, "Paging in vclip %s!\n", vc->name));
ubyte start_val = cf_ReadByte(infile);
int version = 0;
if (start_val != 127) {
version = 0;
vc->num_frames = start_val;
cf_ReadFloat(infile);
vc->frame_time = cf_ReadFloat(infile);
cf_ReadInt(infile);
cf_ReadFloat(infile);
vc->frame_time = DEFAULT_FRAMETIME;
} else {
version = cf_ReadByte(infile);
vc->num_frames = cf_ReadByte(infile);
vc->frame_time = cf_ReadFloat(infile);
vc->frame_time = DEFAULT_FRAMETIME;
}
for (int i = 0; i < vc->num_frames; i++) {
int n = bm_AllocLoadBitmap(infile, mipped);
ASSERT(n > 0);
int w, h;
if (texture_size == NORMAL_TEXTURE) {
w = TEXTURE_WIDTH;
h = TEXTURE_HEIGHT;
#ifndef EDITOR
#ifdef MACINTOSH
if (Render_state.cur_texture_quality <= 1 || Low_vidmem)
#else
if (Mem_low_memory_mode || Low_vidmem)
#endif
{
w = TEXTURE_WIDTH / 2;
h = TEXTURE_HEIGHT / 2;
}
#endif
} else if (texture_size == SMALL_TEXTURE) {
// Make small textures a quarter of the size of normal textures
w = TEXTURE_WIDTH / 2;
h = TEXTURE_HEIGHT / 2;
#ifndef EDITOR
#ifdef MACINTOSH
if (Render_state.cur_texture_quality <= 1 || Low_vidmem)
#else
if (Mem_low_memory_mode || Low_vidmem)
#endif
{
w = TEXTURE_WIDTH / 4;
h = TEXTURE_HEIGHT / 4;
}
#endif
} else if (texture_size == TINY_TEXTURE) {
// Make these tinys an eigth of the size of normal textures
w = TEXTURE_WIDTH / 4;
h = TEXTURE_HEIGHT / 4;
} else if (texture_size == HUGE_TEXTURE) {
// Make these tinys an eigth of the size of normal textures
w = TEXTURE_WIDTH * 2;
h = TEXTURE_HEIGHT * 2;
} else {
w = bm_w(n, 0);
h = bm_h(n, 0);
}
// If differing size, resize!
if (w != bm_w(n, 0) || h != bm_h(n, 0)) {
int dest_bm;
dest_bm = bm_AllocBitmap(w, h, mipped * ((w * h * 2) / 3));
if (mipped)
GameBitmaps[dest_bm].flags |= BF_MIPMAPPED;
GameBitmaps[dest_bm].format = GameBitmaps[n].format;
bm_ScaleBitmapToBitmap(dest_bm, n);
strcpy(GameBitmaps[dest_bm].name, GameBitmaps[n].name);
bm_FreeBitmap(n);
n = dest_bm;
}
ASSERT(n >= 0);
vc->frames[i] = n; // assign frame to bitmap
}
cfclose(infile);
vc->flags &= ~VCF_NOT_RESIDENT;
}
// Allocs and loads a vclip from the file named "filename"
// Returns -1 on error, index into GameVClip array on success
int AllocLoadVClip(char *filename, int texture_size, int mipped, int pageable, int format) {
char name[PAGENAME_LEN];
int i;
ASSERT(filename != NULL);
i = strlen(filename);
if (filename[i - 4] == '.' && filename[i - 3] == 'i' && filename[i - 2] == 'f' && filename[i - 1] == 'l')
return AllocLoadIFLVClip(IGNORE_TABLE(filename), texture_size, mipped, format);
if (filename[i - 4] == '.' && filename[i - 3] == 'a' && filename[i - 2] == 'b' && filename[i - 1] == 'm')
Int3(); // Get Jason
// return AllocLoadIFFAnimClip(filename,);
ChangeVClipName(filename, name);
// Check to see if this vclip already exists in memory
if ((i = FindVClipName(IGNORE_TABLE(name))) != -1) {
GameVClips[i].used++;
return i;
}
// mprintf ((0,"Loading OAF vclip %s\n",name));
int vcnum = AllocVClip();
ASSERT(vcnum >= 0);
strncpy(GameVClips[vcnum].name, name, PAGENAME_LEN);
if (mipped)
GameVClips[vcnum].flags |= VCF_WANTS_MIPPED;
GameVClips[vcnum].target_size = texture_size;
if (pageable == 1)
return vcnum;
PageInVClip(vcnum);
if (GameVClips[vcnum].num_frames < 1) {
FreeVClip(vcnum);
return -1;
}
return vcnum;
}
// Allocs and loads a vclip from a 3DS ILS file
// Returns -1 on error, else index into GameVClips on success
// Argument texture means that this vclip is an animated texture and
// needs to have an 8bit version
int AllocLoadIFLVClip(char *filename, int texture_size, int mipped, int format) {
CFILE *infile;
char name[PAGENAME_LEN];
unsigned int i, done = 0;
ASSERT(filename != NULL);
ChangeVClipName(filename, name);
// Check to see if this vclip already exists in memory
if ((i = FindVClipName(IGNORE_TABLE(name))) != -1) {
GameVClips[i].used++;
return i;
}
infile = (CFILE *)cfopen(filename, "rt");
if (!infile) {
mprintf((0, "Couldn't load IFL vclip %s!\n", filename));
return -1;
}
mprintf((0, "Loading IFL vclip %s\n", name));
int vcnum = AllocVClip();
ASSERT(vcnum >= 0);
vclip *vc = &GameVClips[vcnum];
while (!done) {
char curline[200];
if (cfeof(infile)) {
done = 1;
continue;
}
// Read a line and parse it
cf_ReadString(curline, 200, infile);
if (curline[0] == ';' || curline[1] == ';' || curline[0] == ' ' || curline[1] == ' ')
continue;
if (!(isalnum(curline[0])))
continue;
else if (curline[0] == '$') {
char new_command[50];
for (int i = 0; curline[i + 1] != '=' && i < 50; i++)
new_command[i] = curline[i + 1];
if (i == 50) {
Int3(); // bad command in IFL!
return -1;
}
i++; // advance to data
// parse data
if (!stricmp(new_command, "TIME")) {
// Set play time
float play_time = atof(&curline[i]);
ASSERT(play_time >= 0);
}
} else {
int lastslash = -1;
char bmname[200];
for (i = 0; i < strlen(curline); i++)
#ifdef MACINTOSH
if (curline[i] == ':')
#else
if (curline[i] == '\\')
#endif
lastslash = i;
if (lastslash == -1) {
for (i = 0; i < strlen(filename); i++)
#ifdef MACINTOSH
if (filename[i] == ':')
#else
if (filename[i] == '\\')
#endif
lastslash = i;
ASSERT(lastslash != -1);
strcpy(bmname, filename);
bmname[lastslash + 1] = 0;
strcat(bmname, curline);
} else
strcpy(bmname, curline);
// Try and load this file
int bm = bm_AllocLoadFileBitmap(IGNORE_TABLE(bmname), 0, format);
if (bm < 1) {
Error("Error loading frame %d of ILS file %s!\n", vc->num_frames, filename);
cfclose(infile);
return -1;
}
int w, h;
if (texture_size == NORMAL_TEXTURE) {
w = TEXTURE_WIDTH;
h = TEXTURE_HEIGHT;
} else if (texture_size == SMALL_TEXTURE) {
// Make small textures a quarter of the size of normal textures
w = TEXTURE_WIDTH / 2;
h = TEXTURE_HEIGHT / 2;
} else if (texture_size == TINY_TEXTURE) {
// Make these tinys an eigth of the size of normal textures
w = TEXTURE_WIDTH / 4;
h = TEXTURE_HEIGHT / 4;
} else {
w = bm_w(bm, 0);
h = bm_h(bm, 0);
}
// If differing size, resize!
if (w != bm_w(bm, 0) || h != bm_h(bm, 0)) {
int dest_bm;
dest_bm = bm_AllocBitmap(w, h, mipped * ((w * h) / 3));
if (mipped)
GameBitmaps[dest_bm].flags |= BF_MIPMAPPED;
GameBitmaps[dest_bm].format = GameBitmaps[bm].format;
bm_ScaleBitmapToBitmap(dest_bm, bm);
strcpy(GameBitmaps[dest_bm].name, GameBitmaps[bm].name);
bm_FreeBitmap(bm);
bm = dest_bm;
}
vc->frames[vc->num_frames] = bm;
vc->num_frames++;
}
}
cfclose(infile);
if (vc->num_frames == 0) {
mprintf((0, "vclip had no valid bitmap names!\n"));
FreeVClip(vcnum);
return -1;
}
strcpy(vc->name, name);
return vcnum;
}
// gets the filename from a path, plus appends our .oaf extension
void ChangeVClipName(char *src, char *dest) {
int limit;
char path[256], ext[256], filename[256];
limit = PAGENAME_LEN - 5;
ddio_SplitPath(src, path, filename, ext);
// Make sure we don't go over our name length limit
strncpy(dest, filename, limit);
strcat(dest, ".oaf");
}
// Searches thru all vclips for a specific name, returns -1 if not found
// or index of vclip with name
int FindVClipName(char *name) {
int i;
for (i = 0; i < MAX_VCLIPS; i++)
if (GameVClips[i].used && !stricmp(GameVClips[i].name, name))
return i;
return -1;
}
// Returns frame "frame" of vclip "vclip". Will mod the frame so that there
// is no overflow
int GetVClipBitmap(int v, int frame) {
ASSERT(GameVClips[v].used > 0);
ASSERT(v >= 0 && v < MAX_VCLIPS);
ASSERT(frame >= 0);
vclip *vc = &GameVClips[v];
int bm = vc->frames[frame % vc->num_frames];
return bm;
}
// Loads an animation from an IFF ANIM file
int AllocLoadIFFAnimClip(char *filename, int texture) {
/* char name[PAGENAME_LEN];
int i;
ASSERT (filename!=NULL);
ChangeVClipName (filename,name);
if ((i=FindVClipName(name))!=-1)
{
GameVClips[i].used++;
return i;
}
mprintf ((0,"Loading IFF vclip %s\n",name));
int vcnum=AllocVClip ();
ASSERT (vcnum>=0);
vclip *vc=&GameVClips[vcnum];
vc->num_frames=bm_AllocLoadIFFAnim (filename,vc->frames,0);
if (vc->num_frames==-1)
{
mprintf ((0,"Couldn't load vclip named %d!\n",name));
FreeVClip (vcnum);
return -1;
}
strcpy (vc->name,name);
return vcnum;*/
return -1;
}