aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/readdiff.cpp
blob: 66bfa80274425ad634a32696fd83d5d86a7cb410 (plain)
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

/*
 * This code is based on Labyrinth of Time code with assistance of
 *
 * Copyright (c) 1993 Terra Nova Development
 * Copyright (c) 2004 The Wyrmkeep Entertainment Co.
 *
 */

#include "lab/stddefines.h"
#include "lab/timing.h"
#include "lab/diff.h"
#include "lab/labfun.h"
#include "lab/vga.h"
#include "lab/mouse.h"

namespace Lab {

extern BitMap *DispBitMap, *DrawBitMap;
extern uint32 VGABytesPerPage;

/*
   extern int32             ReadSoFar;
   extern bool          ReadIsDone,
                        ReadIsError;
 */
extern byte **startoffile;

static bool PlayOnce = false, changedscreen;

bool NoFlip         = false,  /* Don't flip the new picture to front  */
     DoBlack        = false,     /* Black the screen before new picture  */
     nopalchange    = false,     /* Don't change the palette.            */
     noscreenchange = false,     /* Don't change the screen type.        */
     IsBM           = false,     /* Just fill in the RawDIFFBM structure */
     hidemouse      = false,     /* Don't set the mouse colors           */
     stopsound      = false,
     soundplaying   = false,
     screenbuffer   = false,
     waiteffect     = false, /* Wait for each sound effect to finish
                                                                                                      before coninuing.                    */
                      mwaiteffect    = false;

uint16 DataBytesPerRow;

extern bool EffectPlaying;


#define CONTINUOUS      0xFFFF

DIFFHeader headerdata;



/*------ Stuff for the animation task. -----*/

static byte *start;

static uint32 diffwidth, diffheight;

static bool continuous,
	IsPlaying      = false,
	StopPlaying    = false,
	StopPlayingEnd = false,
	IsAnim         = false,
	IsPal          = false;

char diffcmap[256 * 3], lastcmap[256 * 3];

BitMap RawDiffBM;




/*****************************************************************************/
/* Does the undiffing between the bitmaps.                                   */
/*****************************************************************************/
void unDiff(byte *NewBuf, byte *OldBuf, byte *DiffData, uint16 bytesperrow, bool IsV) {
	byte buftype;

	DiffData++;
	buftype  = *DiffData;
	DiffData++;

	if (IsV)
		VUnDIFFMemory(NewBuf, DiffData, 1, buftype + 1, bytesperrow);
	else
		unDIFFMemory(NewBuf, DiffData, 1, buftype + 1);
}


static byte blackbuffer[256 * 3];

/*****************************************************************************/
/* Changes the front screen to black.                                        */
/*****************************************************************************/
void blackScreen() {
	memset(blackbuffer, 0, 248 * 3);
	writeColorRegs(blackbuffer, 8, 248);

	g_system->delayMillis(32);
}

/*****************************************************************************/
/* Changes the front screen to white.                                        */
/*****************************************************************************/
void whiteScreen() {
	memset(blackbuffer, 255, 248 * 3);
	writeColorRegs(blackbuffer, 8, 248);
}

/*****************************************************************************/
/* Changes the entire screen to black.                                       */
/*****************************************************************************/
void blackAllScreen() {
	memset(blackbuffer, 0, 256 * 3);
	writeColorRegs(blackbuffer, 0, 256);

	g_system->delayMillis(32);
}



/* For Play Diff */
static uint32 header, size, processed = 0L, WaitSec    = 0L, WaitMicros = 0L, DelayMicros = 0L;
static uint16 CurBit = 0, framenumber = 0, samplespeed, numchunks = 1;
static byte *Buffer, temp[5];
static bool FirstThru = true, donepal   = false;
static byte *storagefordifffile, * *difffile = &storagefordifffile;

void diffNextFrame() {
	if (header == 65535)  /* Already done. */
		return;

	if (DispBitMap->Flags & BITMAPF_VIDEO) {
		DispBitMap->Planes[0] = getVGABaseAddr();
		DispBitMap->Planes[1] = DispBitMap->Planes[0] + 0x10000;
		DispBitMap->Planes[2] = DispBitMap->Planes[1] + 0x10000;
		DispBitMap->Planes[3] = DispBitMap->Planes[2] + 0x10000;
		DispBitMap->Planes[4] = DispBitMap->Planes[3] + 0x10000;
	}

	mouseHide();

	while (1) {
		if (CurBit >= numchunks) {
			mouseShow();

			if (!NoFlip && !IsBM) {
				if (headerdata.fps) {
					waitForTime(WaitSec, WaitMicros);
					addCurTime(0L, DelayMicros, &WaitSec, &WaitMicros);
				}

				if (IsPal && !nopalchange) {
					VGASetPal(diffcmap, 256);
					IsPal = false;
				}

				donepal = true;
				FirstThru = false;
			}

			if (IsPal && !nopalchange && !IsBM && !donepal) {
				VGASetPal(diffcmap, 256);
				IsPal = false;
			}

			donepal = false;

			framenumber++;

			if ((framenumber == 1) && (continuous || (!PlayOnce)))
				Buffer = *difffile;

			IsAnim = (framenumber >= 3) && (!PlayOnce);
			CurBit = 0;

			if (DispBitMap->Flags & BITMAPF_VIDEO)
				WSDL_UpdateScreen();

			return; /* done with the next frame. */
		}

		g_music->updateMusic();
		readBlock(&header, 4L, difffile);
		swapULongPtr(&header, 1);
		readBlock(&size, 4L, difffile);
		swapULongPtr(&size, 1);

		processed += 8L;

		processed += size;

		switch (header) {
		case 8L:
			memcpy(lastcmap, diffcmap, 256 * 3);

			readBlock(diffcmap, size, difffile);
			IsPal = true;
			break;

		case 10L:
			RawDiffBM.Planes[CurBit] = *difffile;

			if (IsBM)
				skip(difffile, size);
			else {
				readBlock(DrawBitMap->Planes[CurBit], size, difffile);
			}

			CurBit++;
			break;

		case 11L:
			skip(difffile, 4L);
			runLengthDecode(DrawBitMap->Planes[CurBit], *difffile);
			CurBit++;
			skip(difffile, size - 4);
			break;

		case 12L:
			skip(difffile, 4L);
			VRunLengthDecode(DrawBitMap->Planes[CurBit], *difffile, DrawBitMap->BytesPerRow);
			CurBit++;
			skip(difffile, size - 4);
			break;

		case 20L:
			unDiff(DrawBitMap->Planes[CurBit], DispBitMap->Planes[CurBit], *difffile, DrawBitMap->BytesPerRow, false);
			CurBit++;
			skip(difffile, size);
			break;

		case 21L:
			unDiff(DrawBitMap->Planes[CurBit], DispBitMap->Planes[CurBit], *difffile, DrawBitMap->BytesPerRow, true);
			CurBit++;
			skip(difffile, size);
			break;

		case 25L:
			CurBit++;
			break;

		case 26L:
			/* NYI: This don't work no more
			   memcpy((void *) DrawBitMap->Planes[CurBit],
			   (void *) DispBitMap->Planes[CurBit],  (uint16) (diffheight*diffwidth));
			 */
			CurBit++;
			break;

		case 30L:
		case 31L: {
			if (waiteffect) {
				while (EffectPlaying) {
					g_music->updateMusic();
					waitTOF();
				}
			}

			size -= 8L;

			skip(difffile, 4L);
			readBlock(&samplespeed, 2L, difffile);
			swapUShortPtr(&samplespeed, 1);
			skip(difffile, 2L);

			byte *music = *difffile;
			uint32 musicsize = size;
			skip(difffile, size);

			playSoundEffect(samplespeed, 64, musicsize, true, music);
			break;
		}
		case 65535L:
			if ((framenumber == 1) || PlayOnce || StopPlayingEnd) {
				int didTOF = 0;

				if (waiteffect) {
					while (EffectPlaying) {
						g_music->updateMusic();
						waitTOF();

						if (DispBitMap->Flags & BITMAPF_VIDEO)
							didTOF = 1;
					}
				}

				IsPlaying = false;
				mouseShow();

				if (DispBitMap->Flags & BITMAPF_VIDEO)
					ungetVGABaseAddr();

				if (!didTOF)
					WSDL_UpdateScreen();

				return;
			}

			framenumber = 4;  /* Random frame number so it never gets back to 2 */
			*difffile = Buffer;
			break;

		default:
			skip(difffile, size);
			break;
		}
	}

	if (DispBitMap->Flags & BITMAPF_VIDEO)
		ungetVGABaseAddr();

}



/*****************************************************************************/
/* A separate task launched by readDiff.  Plays the DIFF.                    */
/*****************************************************************************/
void playDiff() {
	processed = 0L;
	WaitSec   = 0L;
	WaitMicros = 0L;
	DelayMicros = 0L;
	header      = 0;
	CurBit = 0;
	framenumber = 0;
	numchunks   = 1;
	FirstThru   = true;
	donepal     = false;
	difffile    = &storagefordifffile;

	IsPlaying   = true;
	StopPlaying = false;
	StopPlayingEnd = false;

	changedscreen = false;

	if (DoBlack) {
		DoBlack = false;
		blackScreen();
	}


	start = *startoffile;            /* Make a copy of the pointer to the start of the file    */
	*difffile = start;               /* Now can modify the file without modifying the original */


	if (start == NULL) {
		IsPlaying = false;
		return;
	}

	continuous = false;
	readBlock(temp, 4L, difffile);
	temp[4] = '\0';

	readBlock(&header, 4L, difffile);
	swapULongPtr(&header, 1);

	processed += 8L;

	if (!((strcmp((char *)temp, "DIFF") == 0) && (header == 1219009121L))) {
		IsPlaying = false;
		return;
	}

	readBlock(&header, 4L, difffile);
	swapULongPtr(&header, 1);

	readBlock(&size, 4L, difffile);
	swapULongPtr(&size, 1);

	if (header == 0) {
		// sizeof(headerdata) != 18, but the padding might be at the end
		readBlock(&headerdata.Version, 2, difffile);
		readBlock(&headerdata.x, 2, difffile);
		readBlock(&headerdata.y, 2, difffile);
		readBlock(&headerdata.depth, 1, difffile);
		readBlock(&headerdata.fps, 1, difffile);
		readBlock(&headerdata.BufferSize, 4, difffile);
		readBlock(&headerdata.Machine, 2, difffile);
		readBlock(&headerdata.Flags, 4, difffile);

		skip(difffile, size - 18);

		swapUShortPtr(&headerdata.Version, 3);
		swapULongPtr(&headerdata.BufferSize, 1);
		swapUShortPtr(&headerdata.Machine, 1);
		swapULongPtr(&headerdata.Flags, 1);

		continuous = CONTINUOUS & headerdata.Flags;
		diffwidth = headerdata.x;
		diffheight = headerdata.y;
		DataBytesPerRow = diffwidth;

		numchunks = (((int32) diffwidth) * diffheight) / 0x10000;

		if ((uint32)(numchunks * 0x10000) < (uint32)(((int32) diffwidth) * diffheight))
			numchunks++;
	} else {
		return;
	}

	processed += 8L + size;

	for (header = 0; header < 8; header++)
		RawDiffBM.Planes[header] = NULL;

	if (headerdata.fps)
		DelayMicros = ONESECOND / headerdata.fps;

	if (PlayOnce) {
		while (header != 65535)
			diffNextFrame();
	} else
		diffNextFrame();
}



/*****************************************************************************/
/* Stops an animation from running.                                          */
/*****************************************************************************/
void stopDiff() {
	if (IsPlaying) {
		StopPlaying = true;

		/* NYI:
		    while (IsPlaying)
		      waitTOF();
		 */

		if (IsAnim)
			blackScreen();
	}
}



/*****************************************************************************/
/* Stops an animation from running.                                          */
/*****************************************************************************/
void stopDiffEnd() {
	if (IsPlaying) {
		StopPlayingEnd = true;

		while (IsPlaying) {
			g_music->newCheckMusic();
			diffNextFrame();
		}
	}
}



/*****************************************************************************/
/* Stops the continuous sound from playing.                                  */
/*****************************************************************************/
void stopSound() {
	stopsound = true;
}



/*****************************************************************************/
/* Reads in a DIFF file.                                                     */
/*****************************************************************************/
bool readDiff(bool playonce) {
	PlayOnce = playonce;
	playDiff();
	return true;
}



static byte *mstart;


void readSound() {
	uint32 header_ = 0, size_;
	uint16 samplespeed_;
//  uint16 numchunks = 1;
	char temp_[5];
//  bool        FirstThru = true;
	byte *storagefordifffile_, **difffile_ = &storagefordifffile_;

	mstart = *startoffile;            /* Make a copy of the pointer to the start of the file    */
	*difffile_ = mstart;               /* Now can modify the file without modifying the original */

	if (mstart == NULL)
		return;

	readBlock(temp_, 4L, difffile_);
	temp_[4] = '\0';
	readBlock(&header_, 4L, difffile_);
	swapULongPtr(&header_, 1);

	processed += 8L;

	if (!((strcmp(temp_, "DIFF") == 0) && (header_ == 1219009121L)))
		return;

	readBlock(&header_, 4L, difffile_);
	swapULongPtr(&header_, 1);

	readBlock(&size_, 4L, difffile_);
	swapULongPtr(&size_, 1);

	if (header_ == 0)
		skip(difffile_, size_);
	else
		return;

	while (header_ != 65535) {
		g_music->updateMusic();
		readBlock(&header_, 4L, difffile_);
		swapULongPtr(&header_, 1);

		readBlock(&size_, 4L, difffile_);
		swapULongPtr(&size_, 1);

		if ((header_ == 30) || (header_ == 31)) {
			if (mwaiteffect) {
				while (EffectPlaying) {
					g_music->updateMusic();
					waitTOF();
				}
			}

			size_ -= 8L;

			skip(difffile_, 4L);
			readBlock(&samplespeed_, 2L, difffile_);
			swapUShortPtr(&samplespeed_, 1);

			skip(difffile_, 2L);

			byte *music = *difffile_;
			uint32 musicsize = size_;
			skip(difffile_, size_);

			playSoundEffect(samplespeed_, 64, musicsize, true, music);
		} else if (header_ == 65535L) {
			if (mwaiteffect) {
				while (EffectPlaying) {
					g_music->updateMusic();
					waitTOF();
				}
			}
		} else
			skip(difffile_, size_);
	}
}

} // End of namespace Lab