Debian Codegraph @

Descent3/vclip.cpp

raw symbols SCIP
1 /*
2 * Descent 3
3 * Copyright (C) 2024 Parallax Software
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/vclip.cpp $
21 * $Revision: 26 $
22 * $Date: 5/10/00 2:43p $
23 * $Author: Jeff $
24 *
25 * <insert description of file here>
26 *
27 * $Log: /DescentIII/Main/vclip.cpp $
28 *
29 * 26 5/10/00 2:43p Jeff
30 * fixed bug when paging in data if the tablefile specifies a full path to
31 * a primative instead of just a filename (bug in 3rd party tablefile
32 * editors)
33 *
34 * 25 4/19/00 5:07p Matt
35 * From Duane for 1.4
36 * Added checks, asserts, and fixes for bad return values
37 *
38 * 24 10/21/99 9:27p Jeff
39 * B.A. Macintosh code merge
40 *
41 * 23 5/07/99 7:23p Jason
42 * changes for low video memory
43 *
44 * 22 4/14/99 3:57a Jeff
45 * fixed case mismatch in #includes
46 *
47 * 21 4/12/99 3:53p Jason
48 * fixed loading of erroneous vclips
49 *
50 * 20 3/02/99 1:17p Jason
51 * fixed loading of alpha numeric IFLs
52 *
53 * 19 2/10/99 3:47p Jason
54 * table filter changes
55 *
56 * 18 1/22/99 3:59p Jason
57 * added 256x256 textures to help with terrain skies
58 *
59 * 17 1/19/99 11:04a Jason
60 * fixed scaling problem
61 *
62 * 16 1/18/99 11:11a Jason
63 * made vclips be specular
64 *
65 * 15 10/18/98 2:59p Jason
66 * fixes for beta4
67 *
68 * 14 10/14/98 5:50p Jason
69 * added lowmem modes for quartering textures
70 *
71 * 13 10/09/98 3:32p Kevin
72 * New memory library
73 *
74 * 12 10/08/98 4:23p Kevin
75 * Changed code to comply with memory library usage. Always use mem_malloc
76 * , mem_free and mem_strdup
77 *
78 * 11 5/19/98 5:43p Jason
79 * free all vclips at the end of the program
80 *
81 * 10 5/14/98 2:38p Jason
82 * made temp solution for vclip residency problem
83 *
84 * 9 5/14/98 12:56p Jason
85 * changes to help lower memory usage
86 *
87 * 8 2/26/98 2:47p Mark
88 * added assert
89 *
90 * 7 2/11/98 4:58p Jason
91 * fixed some memory problems with resizing and mipping
92 *
93 * 6 11/03/97 6:21p Jason
94 * fixed some problems with animating textures being remapped improperly
95 *
96 * 5 10/31/97 6:33p Jason
97 * fixed bug with whitespace in ifl files
98 *
99 * 4 10/15/97 5:20p Jason
100 * did a HUGE overhaul of the bitmap system
101 *
102 * 3 9/04/97 12:03p Matt
103 * Got rid of warnings
104 *
105 * 2 8/20/97 6:09p Jason
106 * fixed out of bounds problem that bounds checker reported
107 *
108 * 16 5/13/97 2:24p Jason
109 * fixed a couple of animating texture bugs
110 *
111 * 15 5/12/97 11:41a Jason
112 * made game work (default) to 16bit no mip maps mode
113 * Saves us alot of memory
114 *
115 * 14 5/08/97 1:16p Jason
116 * made ChangeEndName work with device independant calls
117 *
118 * 13 5/01/97 1:05p Mark
119 * FROM JASON: Fixed stupid =/== bug in vclip loading
120 *
121 * 12 4/28/97 6:36p Jason
122 * fixed a bug with vclips
123 *
124 * 11 4/25/97 3:31p Jason
125 * implemented better memory management for vclips and bitmaps
126 *
127 * 10 4/24/97 5:42p Jason
128 * got fireball vclips working
129 *
130 * 9 3/03/97 6:21p Matt
131 * Changed cfile functions to use D3 naming convention
132 *
133 * 8 2/20/97 3:27p Jason
134 * made ifl reader ignore lines beginning with spaces
135 *
136 * 7 2/19/97 3:23p Jason
137 * added hooks to import animated iffs
138 *
139 * 6 2/19/97 11:37a Jason
140 * take gametime into consideration when animating a vclip
141 *
142 * 5 2/10/97 1:58p Jason
143 * made vclips free their bitmaps when they are deallocated
144 *
145 * 4 2/04/97 3:19p Jason
146 * better error checking
147 *
148 * 3 1/25/97 5:56p Jason
149 * changes to support new 8bit palettized textures and vclips
150 *
151 * 2 1/21/97 12:22p Jason
152 * added vclip support
153 *
154 * $NoKeywords: $
155 */
156
157 #include <cctype>
158 #include <cstdlib>
159 #include <cstring>
160 #include <filesystem>
161
162 #include "pstypes.h"
163 #include "pserror.h"
164 #include "bitmap.h"
165 #include "vclip.h"
166 #include "cfile.h"
167 #include "mono.h"
168 #include "ddio.h"
169 #include "gametexture.h"
170 #include "mem.h"
171 #include "game.h"
172
173 vclip GameVClips[MAX_VCLIPS];
174 int Num_vclips = 0;
175
176 #define DEFAULT_FRAMETIME .07f
177
178 #define VCLIP_VERSION 1
179 // Frees all the memory used by vclips
180 void FreeAllVClips() {
181 mprintf(0, "Freeing all vclips!\n");
182
183 for (int i = 0; i < MAX_VCLIPS; i++) {
184 if (GameVClips[i].used > 0) {
185 GameVClips[i].used = 1;
186 FreeVClip(i);
187 }
188 }
189 }
190
191 // Simply sets all vclips to unused
192 void InitVClips() {
193 for (int i = 0; i < MAX_VCLIPS; i++)
194 GameVClips[i].used = 0;
195
196 atexit(FreeAllVClips);
197 }
198
199 // Allocs a vclip for use
200 // Returns -1 on error
201 int AllocVClip() {
202 int i;
203
204 for (i = 0; i < MAX_VCLIPS; i++) {
205 if (GameVClips[i].used == 0) {
206 memset(&GameVClips[i], 0, sizeof(vclip));
207 GameVClips[i].frames = (int16_t *)mem_malloc(VCLIP_MAX_FRAMES * sizeof(int16_t));
208 ASSERT(GameVClips[i].frames);
209 GameVClips[i].frame_time = DEFAULT_FRAMETIME;
210 GameVClips[i].flags = VCF_NOT_RESIDENT;
211 GameVClips[i].used = 1;
212 Num_vclips++;
213 return i;
214 }
215 }
216
217 Int3(); // Ran out of vclips. What the hell are you doing? Get Jason
218 return -1;
219 }
220
221 void FreeVClip(int num) {
222 ASSERT(GameVClips[num].used > 0);
223
224 GameVClips[num].used--;
225 if (GameVClips[num].used > 0)
226 return; // other things are using this vclip
227
228 if (!(GameVClips[num].flags & VCF_NOT_RESIDENT)) {
229 for (int i = 0; i < GameVClips[num].num_frames; i++)
230 bm_FreeBitmap(GameVClips[num].frames[i]);
231 }
232
233 mem_free(GameVClips[num].frames);
234
235 Num_vclips--;
236 ASSERT(Num_vclips >= 0);
237 }
238
239 // Saves a given video clip to a file
240 // Returns 1 if everything ok, 0 otherwise
241 // "num" is index into GameVClip array
242 int SaveVClip(const std::filesystem::path& filename, int num) {
243 CFILE *outfile;
244 vclip *vc = &GameVClips[num];
245
246 ASSERT(vc->used);
247 ASSERT(!filename.empty());
248
249 PageInVClip(num);
250
251 outfile = (CFILE *)cfopen(filename, "wb");
252 if (!outfile) {
253 mprintf(0, "Couldn't save vclip %s!\n", filename.u8string().c_str());
254 return 0;
255 }
256
257 // write out the header for this vclip
258 cf_WriteByte(outfile, 127);
259 cf_WriteByte(outfile, (int8_t)VCLIP_VERSION);
260
261 cf_WriteByte(outfile, vc->num_frames);
262 // cf_WriteFloat (outfile,vc->play_time);
263 cf_WriteFloat(outfile, vc->frame_time);
264 // cf_WriteInt (outfile,vc->flags);
265 // cf_WriteFloat (outfile,vc->light_value);
266
267 // Now save each frame of this vclip
268 for (int i = 0; i < vc->num_frames; i++) {
269 if (bm_SaveBitmap(outfile, vc->frames[i]) != 1) {
270 mprintf(0, "Couldn't save frame %d of vclip %s!\n", i, filename.u8string().c_str());
271 Int3();
272 cfclose(outfile);
273 return 0;
274 }
275 }
276
277 cfclose(outfile);
278 return 1;
279 }
280
281 extern int Low_vidmem;
282 // Pages in a vclip if it needs to be
283 void PageInVClip(int vcnum) {
284 ASSERT(GameVClips[vcnum].used);
285 if (!(GameVClips[vcnum].flags & VCF_NOT_RESIDENT))
286 return;
287
288 int mipped = 0;
289 int texture_size = GameVClips[vcnum].target_size;
290 vclip *vc = &GameVClips[vcnum];
291 if (vc->flags & VCF_WANTS_MIPPED)
292 mipped = 1;
293
294 CFILE *infile = (CFILE *)cfopen(vc->name, "rb");
295 if (!infile) {
296 // due to a bug in some 3rd party tablefile editors, full paths might
297 // have been used when they shouldn't have been
298 char *end_ptr, *start_ptr;
299 start_ptr = vc->name;
300 end_ptr = start_ptr + strlen(start_ptr) - 1;
301 while ((end_ptr >= start_ptr) && (*end_ptr != '\\'))
302 end_ptr--;
303 if (end_ptr < start_ptr) {
304 mprintf(0, "Couldn't load vclip %s!\n", vc->name);
305 return;
306 }
307
308 ASSERT(*end_ptr == '\\');
309 end_ptr++;
310
311 infile = (CFILE *)cfopen(end_ptr, "rb");
312 if (!infile) {
313 mprintf(0, "Couldn't load vclip %s!\n", vc->name);
314 return;
315 }
316 }
317
318 mprintf(0, "Paging in vclip %s!\n", vc->name);
319
320 uint8_t start_val = cf_ReadByte(infile);
321 int version = 0;
322 if (start_val != 127) {
323 version = 0;
324 vc->num_frames = start_val;
325 cf_ReadFloat(infile);
326 vc->frame_time = cf_ReadFloat(infile);
327 cf_ReadInt(infile);
328 cf_ReadFloat(infile);
329 vc->frame_time = DEFAULT_FRAMETIME;
330 } else {
331 version = cf_ReadByte(infile);
332 vc->num_frames = cf_ReadByte(infile);
333 vc->frame_time = cf_ReadFloat(infile);
334 vc->frame_time = DEFAULT_FRAMETIME;
335 }
336
337 for (int i = 0; i < vc->num_frames; i++) {
338 int n = bm_AllocLoadBitmap(infile, mipped);
339
340 ASSERT(n > 0);
341
342 int w, h;
343
344 if (texture_size == NORMAL_TEXTURE) {
345 w = TEXTURE_WIDTH;
346 h = TEXTURE_HEIGHT;
347
348 #ifndef EDITOR
349 if (Mem_low_memory_mode || Low_vidmem)
350 {
351 w = TEXTURE_WIDTH / 2;
352 h = TEXTURE_HEIGHT / 2;
353 }
354 #endif
355 } else if (texture_size == SMALL_TEXTURE) {
356 // Make small textures a quarter of the size of normal textures
357 w = TEXTURE_WIDTH / 2;
358 h = TEXTURE_HEIGHT / 2;
359
360 #ifndef EDITOR
361 if (Mem_low_memory_mode || Low_vidmem)
362 {
363 w = TEXTURE_WIDTH / 4;
364 h = TEXTURE_HEIGHT / 4;
365 }
366 #endif
367 } else if (texture_size == TINY_TEXTURE) {
368 // Make these tinys an eigth of the size of normal textures
369 w = TEXTURE_WIDTH / 4;
370 h = TEXTURE_HEIGHT / 4;
371 } else if (texture_size == HUGE_TEXTURE) {
372 // Make these tinys an eigth of the size of normal textures
373 w = TEXTURE_WIDTH * 2;
374 h = TEXTURE_HEIGHT * 2;
375 } else {
376 w = bm_w(n, 0);
377 h = bm_h(n, 0);
378 }
379
380 // If differing size, resize!
381 if (w != bm_w(n, 0) || h != bm_h(n, 0)) {
382 int dest_bm;
383
384 dest_bm = bm_AllocBitmap(w, h, mipped * ((w * h * 2) / 3));
385 ASSERT(dest_bm >= 0);
386 if (mipped)
387 GameBitmaps[dest_bm].flags |= BF_MIPMAPPED;
388 GameBitmaps[dest_bm].format = GameBitmaps[n].format;
389
390 bm_ScaleBitmapToBitmap(dest_bm, n);
391 strcpy(GameBitmaps[dest_bm].name, GameBitmaps[n].name);
392 bm_FreeBitmap(n);
393
394 n = dest_bm;
395 }
396
397 ASSERT(n >= 0);
398 vc->frames[i] = n; // assign frame to bitmap
399 }
400
401 cfclose(infile);
402
403 vc->flags &= ~VCF_NOT_RESIDENT;
404 }
405
406 // Allocs and loads a vclip from the file named "filename"
407 // Returns -1 on error, index into GameVClip array on success
408 int AllocLoadVClip(const char *filename, int texture_size, int mipped, int pageable, int format) {
409 char name[PAGENAME_LEN];
410 int i;
411
412 ASSERT(filename != NULL);
413
414 i = strlen(filename);
415
416 if (filename[i - 4] == '.' && filename[i - 3] == 'i' && filename[i - 2] == 'f' && filename[i - 1] == 'l')
417 return AllocLoadIFLVClip(IGNORE_TABLE(filename), texture_size, mipped, format);
418
419 if (filename[i - 4] == '.' && filename[i - 3] == 'a' && filename[i - 2] == 'b' && filename[i - 1] == 'm')
420 Int3(); // Get Jason
421 // return AllocLoadIFFAnimClip(filename,);
422
423 ChangeVClipName(filename, name);
424
425 // Check to see if this vclip already exists in memory
426 if ((i = FindVClipName(IGNORE_TABLE(name))) != -1) {
427 GameVClips[i].used++;
428 return i;
429 }
430
431 // mprintf(0,"Loading OAF vclip %s\n",name);
432
433 int vcnum = AllocVClip();
434
435 ASSERT(vcnum >= 0);
436 strncpy(GameVClips[vcnum].name, name, PAGENAME_LEN);
437
438 if (mipped)
439 GameVClips[vcnum].flags |= VCF_WANTS_MIPPED;
440
441 GameVClips[vcnum].target_size = texture_size;
442
443 if (pageable == 1)
444 return vcnum;
445
446 PageInVClip(vcnum);
447
448 if (GameVClips[vcnum].num_frames < 1) {
449 FreeVClip(vcnum);
450 return -1;
451 }
452
453 return vcnum;
454 }
455
456 // Allocs and loads a vclip from a 3DS ILS file
457 // Returns -1 on error, else index into GameVClips on success
458 // Argument texture means that this vclip is an animated texture and
459 // needs to have an 8bit version
460 int AllocLoadIFLVClip(const char *filename, int texture_size, int mipped, int format) {
461 CFILE *infile;
462 char name[PAGENAME_LEN];
463 uint32_t i, done = 0;
464
465 ASSERT(filename != NULL);
466
467 ChangeVClipName(filename, name);
468
469 // Check to see if this vclip already exists in memory
470 if ((i = FindVClipName(IGNORE_TABLE(name))) != -1) {
471 GameVClips[i].used++;
472 return i;
473 }
474
475 infile = (CFILE *)cfopen(filename, "rt");
476 if (!infile) {
477 mprintf(0, "Couldn't load IFL vclip %s!\n", filename);
478 return -1;
479 }
480
481 mprintf(0, "Loading IFL vclip %s\n", name);
482
483 int vcnum = AllocVClip();
484
485 ASSERT(vcnum >= 0);
486
487 vclip *vc = &GameVClips[vcnum];
488
489 while (!done) {
490 char curline[200];
491
492 if (cfeof(infile)) {
493 done = 1;
494 continue;
495 }
496
497 // Read a line and parse it
498 cf_ReadString(curline, 200, infile);
499
500 if (curline[0] == ';' || curline[1] == ';' || curline[0] == ' ' || curline[1] == ' ')
501 continue;
502 if (!(isalnum(curline[0])))
503 continue;
504
505 else if (curline[0] == '$') {
506 char new_command[50];
507
508 int i;
509 for (i = 0; curline[i + 1] != '=' && i < 50; i++) {
510 new_command[i] = curline[i + 1];
511 }
512 if (i == 50) {
513 Int3(); // bad command in IFL!
514 return -1;
515 }
516
517 i++; // advance to data
518
519 // parse data
520 if (!stricmp(new_command, "TIME")) {
521 // Set play time
522 float play_time = atof(&curline[i]);
523 ASSERT(play_time >= 0);
524 }
525 } else {
526 int lastslash = -1;
527 char bmname[200];
528
529 for (i = 0; i < strlen(curline); i++)
530 if (curline[i] == '\\')
531 lastslash = i;
532
533 if (lastslash == -1) {
534 for (i = 0; i < strlen(filename); i++)
535 if (filename[i] == '\\')
536 lastslash = i;
537
538 ASSERT(lastslash != -1);
539
540 strcpy(bmname, filename);
541 bmname[lastslash + 1] = 0;
542 strcat(bmname, curline);
543 } else
544 strcpy(bmname, curline);
545
546 // Try and load this file
547
548 int bm = bm_AllocLoadFileBitmap(IGNORE_TABLE(bmname), 0, format);
549 if (bm < 1) {
550 Error("Error loading frame %d of ILS file %s!\n", vc->num_frames, filename);
551 cfclose(infile);
552 return -1;
553 }
554
555 int w, h;
556
557 if (texture_size == NORMAL_TEXTURE) {
558 w = TEXTURE_WIDTH;
559 h = TEXTURE_HEIGHT;
560 } else if (texture_size == SMALL_TEXTURE) {
561 // Make small textures a quarter of the size of normal textures
562 w = TEXTURE_WIDTH / 2;
563 h = TEXTURE_HEIGHT / 2;
564 } else if (texture_size == TINY_TEXTURE) {
565 // Make these tinys an eigth of the size of normal textures
566 w = TEXTURE_WIDTH / 4;
567 h = TEXTURE_HEIGHT / 4;
568 } else {
569 w = bm_w(bm, 0);
570 h = bm_h(bm, 0);
571 }
572
573 // If differing size, resize!
574 if (w != bm_w(bm, 0) || h != bm_h(bm, 0)) {
575 int dest_bm;
576
577 dest_bm = bm_AllocBitmap(w, h, mipped * ((w * h) / 3));
578 ASSERT(dest_bm >= 0);
579 if (mipped)
580 GameBitmaps[dest_bm].flags |= BF_MIPMAPPED;
581 GameBitmaps[dest_bm].format = GameBitmaps[bm].format;
582
583 bm_ScaleBitmapToBitmap(dest_bm, bm);
584 strcpy(GameBitmaps[dest_bm].name, GameBitmaps[bm].name);
585 bm_FreeBitmap(bm);
586
587 bm = dest_bm;
588 }
589
590 vc->frames[vc->num_frames] = bm;
591 vc->num_frames++;
592 }
593 }
594
595 cfclose(infile);
596
597 if (vc->num_frames == 0) {
598 mprintf(0, "vclip had no valid bitmap names!\n");
599 FreeVClip(vcnum);
600 return -1;
601 }
602
603 strcpy(vc->name, name);
604 return vcnum;
605 }
606
607 // gets the filename from a path, plus appends our .oaf extension
608 void ChangeVClipName(const char *src, char *dest) {
609 int limit;
610 char path[256], ext[256], filename[256];
611
612 limit = PAGENAME_LEN - 5;
613
614 ddio_SplitPath(src, path, filename, ext);
615
616 // Make sure we don't go over our name length limit
617 strncpy(dest, filename, limit);
618
619 strcat(dest, ".oaf");
620 }
621 // Searches thru all vclips for a specific name, returns -1 if not found
622 // or index of vclip with name
623 int FindVClipName(const char *name) {
624 int i;
625
626 for (i = 0; i < MAX_VCLIPS; i++)
627 if (GameVClips[i].used && !stricmp(GameVClips[i].name, name))
628 return i;
629
630 return -1;
631 }