-
Notifications
You must be signed in to change notification settings - Fork 0
/
FLI.Cpp
712 lines (590 loc) · 15.6 KB
/
FLI.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
/*
Copyright (C) 1998 BJ Eirich (aka vecna)
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿
// ³ The VERGE Engine ³
// ³ Copyright (C)1998 BJ Eirich (aka vecna) ³
// ³ FLI/FLC Playback module ³
// ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ
//1.30.00 -- zeromus
//changed BITMAP to fliBITMAP to nuke conflict with windows-defined struct BITMAP
#define FLI_H
#include <sys/stat.h>
#include <fcntl.h>
//#include <unistd.h>
#include <conio.h>
#include "verge.h"
// ================================= Data ====================================
#ifndef __WATCOMC__
#define INT_MAX 0xFFFFFFFF
#define INT_MIN 0
#endif
#ifndef MIN
#define MIN(x,y) (((x) < (y)) ? (x) : (y))
#define MAX(x,y) (((x) > (y)) ? (x) : (y))
#define MID(x,y,z) MAX((x), MIN((y), (z)))
#endif
#ifndef ABS
#define ABS(x) (((x) >= 0) ? (x) : (-(x)))
#endif
#ifndef SGN
#define SGN(x) (((x) >= 0) ? 1 : -1)
#endif
#define FLI_MAGIC 0xAF11 // file header magic number
#define FLC_MAGIC 0xAF12 // file magic number (Pro)
#define FLI_FRAME_MAGIC 0xF1FA // frame header magic number
#define FLI_FRAME_PREFIX 0xF100 // FLC's prefix info
#define FLI_FRAME_USELESS 0x00A1 // FLC's garbage frame
typedef struct {
int w,h;
byte *data;
} fliBITMAP;
#define FLI_OK 0 /* FLI player return values */
#define FLI_EOF -1
#define FLI_ERROR -2
#define FLI_NOT_OPEN -3
#ifdef __WATCOMC__
#define PACKED
#pragma pack(1);
#endif
#ifdef __DJGPP__
#define PACKED __attribute__ ((packed))
#endif
typedef struct {
long size PACKED;
word type PACKED;
word frame_count PACKED;
word width PACKED;
word height PACKED;
word bpp PACKED;
word flags PACKED;
word speed PACKED;
long next_head PACKED;
long frames_in_table PACKED;
char reserved[102] PACKED;
} FLI_HEADER;
typedef struct {
quad size PACKED;
word type PACKED;
word chunks PACKED;
char pad[8] PACKED;
} FLI_FRAME;
typedef struct {
quad size PACKED;
word type PACKED;
} FLI_CHUNK;
#ifdef __WATCOMC__
#pragma pack();
#endif
static int fli_status = FLI_NOT_OPEN; // current state of the FLI player
int fli_fliBITMAP_width = 0;
int fli_fliBITMAP_height = 0;
byte *fli_fliBITMAP_data = NULL; // current frame of the FLI
byte fli_palette[768]; // current palette the FLI is using
int fli_bmp_dirty_from = INT_MAX; // what part of fli_fliBITMAP is dirty
int fli_bmp_dirty_to = INT_MIN;
int fli_pal_dirty_from = INT_MAX; // what part of fli_palette is dirty
int fli_pal_dirty_to = INT_MIN;
int fli_frame = 0; // current frame number in the FLI
long speed;
static int fli_file = 0; // the file we are reading
static byte *fli_mem_data = NULL; // the memory FLI we are playing
static int fli_mem_pos = 0; // position in the memory FLI
static FLI_HEADER fli_header; // header structure
static FLI_FRAME frame_header; // frame header structure
// a block of temporary working memory
byte *_scratch_mem = NULL;
int _scratch_mem_size = 0;
// ================================= Code ====================================
void reset_fli_variables(void);
void close_fli(void);
int next_fli_frame(int);
int open_fli(char *);
int open_memory_fli(byte *);
void _grow_scratch_mem(int size) {
if (size > _scratch_mem_size) {
size = (size+1023) & 0xFFFFFC00;
_scratch_mem = (byte *)realloc(_scratch_mem, size);
_scratch_mem_size = size;
}
}
static void vsyncer() {
while (!(inp(986) & 8));
}
void set_palette_range(byte *pal, int from, int to, int sync) {
int i;
// swap?
if (to<from) {
int t=from;
from=to;
to=t;
}
// keep valid
if (from<0) {
from=0;
}
if (to>=256) {
to=255;
}
if (sync) {
vsyncer();
}
outp(968, from);
for (i=from; i<=to; i++) {
outp(969, pal[(i*3)+0]);
outp(969, pal[(i*3)+1]);
outp(969, pal[(i*3)+2]);
}
}
static byte *fli_read(byte *buf, int size) {
int result=0;
if (fli_mem_data) {
if (buf) {
V_memcpy(buf, fli_mem_data+fli_mem_pos, size);
} else {
buf = fli_mem_data+fli_mem_pos;
}
fli_mem_pos += size;
} else {
if (!buf) {
_grow_scratch_mem(size);
buf = _scratch_mem;
}
result = read(fli_file, buf, size);
if (result != size) {
return NULL;
}
}
return buf;
}
static void fli_seek(int offset, int mode) {
if (fli_mem_data) {
if (mode == SEEK_CUR) {
fli_mem_pos += offset;
} else {
fli_mem_pos = offset;
}
} else {
lseek(fli_file, offset, mode);
}
}
byte *__glob_alias=0;
int *__glob_sz=0;
// helpers for reading FLI chunk data
unsigned char READ_BYTE() {
register unsigned char v=*__glob_alias;
if ((*__glob_sz)-- > 0) {
__glob_alias++;
}
return v;
}
signed char READ_CHAR() {
register signed char v=*__glob_alias;
if ((*__glob_sz)-- > 0) {
__glob_alias++;
}
return v;
}
signed short READ_SHORT() {
register signed short v=*((signed short *)__glob_alias);
if ((*__glob_sz)-- > 0) {
__glob_alias+=2;
}
return v;
}
#define READ_BLOCK(pos, size) \
if (sz >= (size)) { \
V_memcpy(pos, __glob_alias, size); \
__glob_alias += (size); \
sz -= (size); \
} else { \
char *_p = (char *)(pos); \
int _c=0; \
for (_c=0; _c < (size); _c++) { \
*(_p++) = READ_BYTE(); \
} \
}
#define READ_RLE_BYTE(pos, size) { \
V_memset(pos, READ_BYTE(), size); \
}
#define READ_RLE_WORD(pos, size) { \
short *_p = (short *)(pos); \
short _v = READ_SHORT(); \
int _c=0; \
for (_c=0; _c < (size); _c++) { \
*(_p++) = _v; \
} \
}
static void do_fli_256_color(byte *p, int sz) {
int packets=0;
int c=0, c2=0;
int offset=0;
int length=0;
__glob_alias=p; // - aen
__glob_sz=&sz;
offset = 0;
packets = READ_SHORT();
for (c=0; c<packets; c++) {
offset += READ_BYTE();
length = READ_BYTE();
if (length == 0) length = 256;
for (c2=0; c2<length; c2++) {
fli_palette[((offset+c2)*3)+0] = (byte)(READ_BYTE() / 4);
fli_palette[((offset+c2)*3)+1] = (byte)(READ_BYTE() / 4);
fli_palette[((offset+c2)*3)+2] = (byte)(READ_BYTE() / 4);
}
fli_pal_dirty_from = MIN(fli_pal_dirty_from, offset);
fli_pal_dirty_to = MAX(fli_pal_dirty_to, offset+length-1);
offset += length;
}
}
static void do_fli_delta(byte *p, int sz) {
int lines=0;
int packets=0;
int size=0;
int x=0, y=0;
__glob_alias=p; // - aen
__glob_sz=&sz;
y = 0;
lines = READ_SHORT();
// for each line...
while (lines-- > 0) {
packets = READ_SHORT();
while (packets < 0) {
if (packets & 0x4000) {
y -= packets;
} else {
fli_fliBITMAP_data[(y*fli_fliBITMAP_width)+fli_fliBITMAP_width-1]=(byte)(packets&0xff);
}
//fli_fliBITMAP->line[y][fli_fliBITMAP->w-1] = packets & 0xFF;
packets = READ_SHORT();
}
x=0;
while (packets-- > 0) {
// skip bytes
x += READ_BYTE();
size = READ_CHAR();
// copy size words
if (size > 0) {
READ_BLOCK(fli_fliBITMAP_data+(y*fli_fliBITMAP_width)+x, size*2);
//READ_BLOCK(fli_fliBITMAP->line[y]+x, size*2);
x += size*2;
// repeat word -size times
} else if (size < 0) {
READ_RLE_WORD(fli_fliBITMAP_data+(y*fli_fliBITMAP_width)+x, -size);
//READ_RLE_WORD(fli_fliBITMAP->line[y]+x, -size);
x -= size*2;
}
}
fli_bmp_dirty_from = MIN(fli_bmp_dirty_from, y);
fli_bmp_dirty_to = MAX(fli_bmp_dirty_to, y);
y++;
}
}
static void do_fli_color(byte *p, int sz) {
int packets=0;
int c=0, c2=0;
int offset=0;
int length=0;
__glob_alias=p; // - aen
__glob_sz=&sz;
offset = 0;
packets = READ_SHORT();
for (c=0; c<packets; c++) {
offset += READ_BYTE();
length = READ_BYTE();
if (0 == length) {
length = 256;
}
for (c2=0; c2<length; c2++) {
fli_palette[((offset+c2)*3)+0] = READ_BYTE();
fli_palette[((offset+c2)*3)+1] = READ_BYTE();
fli_palette[((offset+c2)*3)+2] = READ_BYTE();
}
fli_pal_dirty_from = MIN(fli_pal_dirty_from, offset);
fli_pal_dirty_to = MAX(fli_pal_dirty_to, offset+length-1);
offset += length;
}
}
static void do_fli_lc(byte *p, int sz) {
int lines=0;
int packets=0;
int size=0;
int x=0, y=0;
__glob_alias=p; // - aen
__glob_sz=&sz;
y = READ_SHORT();
lines = READ_SHORT();
fli_bmp_dirty_from = MIN(fli_bmp_dirty_from, y);
fli_bmp_dirty_to = MAX(fli_bmp_dirty_to, y+lines-1);
// for each line...
while (lines-- > 0) {
packets = READ_BYTE();
x = 0;
while (packets-- > 0) {
x += READ_BYTE();
size = READ_CHAR();
// copy size bytes
if (size > 0) {
READ_BLOCK(fli_fliBITMAP_data+(y*fli_fliBITMAP_width)+x, size);
//READ_BLOCK(fli_fliBITMAP->line[y]+x, size);
x += size;
} else if (size < 0) {
READ_RLE_BYTE(fli_fliBITMAP_data+(y*fli_fliBITMAP_width)+x, -size);
//READ_RLE_BYTE(fli_fliBITMAP->line[y]+x, -size);
x -= size;
}
}
y++;
}
}
static void do_fli_black(byte *p, int sz) {
__glob_alias=p; // - aen
__glob_sz=&sz;
V_memset(fli_fliBITMAP_data, 0, fli_fliBITMAP_width*fli_fliBITMAP_height);
//clear(fli_fliBITMAP);
fli_bmp_dirty_from = 0;
fli_bmp_dirty_to = fli_fliBITMAP_height-1;
}
static void do_fli_brun(byte *p, int sz) {
int packets=0;
int size=0;
int x=0, y=0;
__glob_alias=p; // - aen
__glob_sz=&sz;
// for each line...
for (y=0; y<fli_fliBITMAP_height; y++) {
packets = READ_BYTE();
x = 0;
while (packets-- > 0) {
size = READ_CHAR();
if (size < 0) {
READ_BLOCK(fli_fliBITMAP_data+(y*fli_fliBITMAP_width)+x, -size);
//READ_BLOCK(fli_fliBITMAP->line[y]+x, -size);
x -= size;
// repeat byte size times
} else if (size > 0) {
READ_RLE_BYTE(fli_fliBITMAP_data+(y*fli_fliBITMAP_width)+x, size);
//READ_RLE_BYTE(fli_fliBITMAP->line[y]+x, size);
x += size;
}
}
}
fli_bmp_dirty_from = 0;
fli_bmp_dirty_to = fli_fliBITMAP_height-1;
}
static void do_fli_copy(byte *p, int sz) {
__glob_alias=p; // - aen
__glob_sz=&sz;
READ_BLOCK(fli_fliBITMAP_data, fli_fliBITMAP_width * fli_fliBITMAP_height);
//READ_BLOCK(fli_fliBITMAP->dat, fli_fliBITMAP->w * fli_fliBITMAP->h);
fli_bmp_dirty_from = 0;
fli_bmp_dirty_to = fli_fliBITMAP_height-1;
}
static void read_frame() {
byte *p;
FLI_CHUNK *chunk;
int c, sz;
if (fli_status != FLI_OK) {
return;
}
get_another_frame:
if (!fli_read((byte *)&frame_header, sizeof(FLI_FRAME))) {
fli_status = FLI_ERROR;
return;
}
if ((frame_header.type == FLI_FRAME_PREFIX)
|| (frame_header.type == FLI_FRAME_USELESS)) {
fli_seek(frame_header.size-sizeof(FLI_FRAME), SEEK_CUR);
fli_frame++;
goto get_another_frame;
}
if (frame_header.type != FLI_FRAME_MAGIC) {
fli_status = FLI_ERROR;
return;
}
if (frame_header.size == sizeof(FLI_FRAME)) {
fli_frame++;
return;
}
p = fli_read(NULL, frame_header.size-sizeof(FLI_FRAME));
if (!p) {
fli_status = FLI_ERROR;
return;
}
for (c=0; c<frame_header.chunks; c++) {
chunk = (FLI_CHUNK *)p;
sz = chunk->size - sizeof(FLI_CHUNK);
p += sizeof(FLI_CHUNK);
switch (chunk->type) {
case 4: do_fli_256_color(p, sz); break;
case 7: do_fli_delta(p, sz); break;
case 11: do_fli_color(p, sz); break;
case 12: do_fli_lc(p, sz); break;
case 13: do_fli_black(p, sz); break;
case 15: do_fli_brun(p, sz); break;
case 16: do_fli_copy(p, sz); break;
default:
Sys_Error("Corrupt FLI chunk.");
}
p = ((byte *)chunk) + chunk->size;
}
fli_frame++;
}
static int do_play_fli(fliBITMAP *bmp, int loop, int (*callback)()) {
int ret;
ret = next_fli_frame(loop);
while (ret == FLI_OK) {
if (fli_pal_dirty_from <= fli_pal_dirty_to) {
set_palette_range(fli_palette, fli_pal_dirty_from, fli_pal_dirty_to, 1);
}
if (fli_bmp_dirty_from <= fli_bmp_dirty_to) {
int i,to=fli_bmp_dirty_to;
if (to>=bmp->h) {
to=bmp->h-1;
}
// vsync();
for (i=fli_bmp_dirty_from; i<=to; i++) {
V_memcpy(bmp->data+(i*bmp->w), fli_fliBITMAP_data+(i*fli_fliBITMAP_width),
(fli_fliBITMAP_width>bmp->w) ? bmp->w : fli_fliBITMAP_width);
}
//blit(
// fli_fliBITMAP, bmp, 0, fli_bmp_dirty_from, 0, fli_bmp_dirty_from,
// fli_fliBITMAP->w, 1+fli_bmp_dirty_to-fli_bmp_dirty_from);
}
reset_fli_variables();
if (callback) {
ret = (*callback)();
if (ret != FLI_OK) {
break;
}
}
ret = next_fli_frame(loop);
while (timer_count<speed) {
ShowPage();
}
timer_count=0;
}
close_fli();
return (ret == FLI_EOF) ? FLI_OK : ret;
}
int play_fli(char *filename, fliBITMAP *bmp, int loop, int (*callback)()) {
if (open_fli(filename) != FLI_OK) {
return FLI_ERROR;
}
return do_play_fli(bmp, loop, callback);
}
int play_memory_fli(byte *fli_data, fliBITMAP *bmp, int loop, int (*callback)()) {
if (open_memory_fli(fli_data) != FLI_OK) {
return FLI_ERROR;
}
return do_play_fli(bmp, loop, callback);
}
static int do_open_fli() {
// read the header
if (!fli_read((byte *)&fli_header, sizeof(FLI_HEADER))) {
close_fli();
return FLI_ERROR;
}
// check magic numbers
if (((fli_header.bpp != 8) && (fli_header.bpp != 0))
|| ((fli_header.type != FLI_MAGIC) && (fli_header.type != FLC_MAGIC))) {
close_fli();
return FLI_ERROR;
}
if (fli_header.width == 0) {
fli_header.width = 320;
}
if (fli_header.height == 0) {
fli_header.height = 200;
}
// create the frame fliBITMAP
fli_fliBITMAP_width=fli_header.width;
fli_fliBITMAP_height=fli_header.height;
fli_fliBITMAP_data=(byte *)valloc(fli_fliBITMAP_width*fli_fliBITMAP_height, "fli_fliBITMAP_data", 0);
if (!fli_fliBITMAP_data) {
close_fli();
return FLI_ERROR;
}
reset_fli_variables();
fli_frame = 0;
fli_status = FLI_OK;
if (fli_header.type == FLI_MAGIC) {
speed = (long)fli_header.speed * 100 / 70;
} else {
speed = (long)fli_header.speed / 10;
}
if (0 == speed) {
speed = 100;
}
return fli_status;
}
int open_fli(char *filename) {
if (fli_status != FLI_NOT_OPEN) {
return FLI_ERROR;
}
fli_file = open(filename, O_RDONLY | O_BINARY, S_IRUSR | S_IWUSR);
if (fli_file < 0) {
fli_file = 0;
return FLI_ERROR;
}
return do_open_fli();
}
int open_memory_fli(byte *fli_data) {
if (fli_status != FLI_NOT_OPEN) {
return FLI_ERROR;
}
fli_mem_data = fli_data;
fli_mem_pos = 0;
return do_open_fli();
}
void close_fli() {
//remove_int(fli_timer_callback);
if (fli_file) {
close(fli_file);
fli_file = 0;
}
if (fli_fliBITMAP_data) {
vfree(fli_fliBITMAP_data);
fli_fliBITMAP_data = NULL;
}
fli_mem_data = NULL;
fli_mem_pos = 0;
reset_fli_variables();
fli_status = FLI_NOT_OPEN;
}
int next_fli_frame(int loop) {
if (fli_status != FLI_OK) {
return fli_status;
}
//fli_timer--;
// end of file? should we loop?
if (fli_frame >= fli_header.frame_count) {
if (loop) {
fli_seek(sizeof(FLI_HEADER), SEEK_SET);
fli_frame = 0;
} else {
fli_status = FLI_EOF;
return fli_status;
}
}
read_frame();
return fli_status;
}
void reset_fli_variables() {
fli_bmp_dirty_from = INT_MAX;
fli_bmp_dirty_to = INT_MIN;
fli_pal_dirty_from = INT_MAX;
fli_pal_dirty_to = INT_MIN;
}