aboutsummaryrefslogtreecommitdiff
path: root/engines/lab/anim.cpp
blob: ef6694490afd386b37f50f26db9e5708d0f73210 (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
/* 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/lab.h"

#include "lab/anim.h"
#include "lab/dispman.h"
#include "lab/eventman.h"
#include "lab/music.h"
#include "lab/utils.h"

namespace Lab {

Anim::Anim(LabEngine *vm) : _vm(vm) {
	_lastBlockHeader = 0;
	_numChunks = 1;
	_headerdata._width = 0;
	_headerdata._height = 0;
	_headerdata._fps = 0;
	_headerdata._flags = 0;
	_delayMicros = 0;
	_continuous = false;
	_isPlaying = false;
	_isAnim = false;
	_isPal = false;
	_noPalChange = false;
	_donePal = false;
	_frameNum = 0;
	_playOnce = false;
	_diffFile = nullptr;
	_diffFileStart = 0;
	_size = 0;
	_scrollScreenBuffer = nullptr;
	_waitForEffect = false;
	_stopPlayingEnd = false;
	_sampleSpeed = 0;
	_doBlack = false;
	_diffWidth = 0;
	_diffHeight = 0;

	for (int i = 0; i < 3 * 256; i++)
		_diffPalette[i] = 0;

	_outputBuffer = nullptr; // output to screen
}

Anim::~Anim() {
	delete[] _vm->_anim->_scrollScreenBuffer;
	_vm->_anim->_scrollScreenBuffer = nullptr;
}

void Anim::setOutputBuffer(byte *memoryBuffer) {
	_outputBuffer = memoryBuffer;
}

uint16 Anim::getDIFFHeight() {
	return _headerdata._height;
}

void Anim::diffNextFrame(bool onlyDiffData) {
	if (_lastBlockHeader == 65535)
		// Already done.
		return;

	bool drawOnScreen = false;
	byte *startOfBuf = _outputBuffer;
	int bufPitch = _vm->_graphics->_screenWidth;

	if (!startOfBuf) {
		startOfBuf = _vm->_graphics->getCurrentDrawingBuffer();
		drawOnScreen = true;
	}
	byte *endOfBuf = startOfBuf + (int)_diffWidth * _diffHeight;

	int curBit = 0;

	while (1) {
		byte *buf = startOfBuf + 0x10000 * curBit;

		if (buf >= endOfBuf) {
			_vm->_event->mouseShow();

			if (!onlyDiffData) {
				if (_headerdata._fps) {
					uint32 targetMillis = _vm->_system->getMillis() + _delayMicros;
					while (_vm->_system->getMillis() < targetMillis)
						_vm->_system->delayMillis(10);
				}

				if (_isPal && !_noPalChange) {
					_vm->_graphics->setPalette(_diffPalette, 256);
					_isPal = false;
				}

				_donePal = true;
			}

			if (_isPal && !_noPalChange && !onlyDiffData && !_donePal) {
				_vm->_graphics->setPalette(_diffPalette, 256);
				_isPal = false;
			}

			_donePal = false;

			_frameNum++;

			if ((_frameNum == 1) && (_continuous || !_playOnce))
				_diffFileStart = _diffFile->pos();

			_isAnim = (_frameNum >= 3) && (!_playOnce);

			if (drawOnScreen)
				_vm->_graphics->screenUpdate();

			// done with the next frame.
			return;
		}

		_vm->updateEvents();
		_lastBlockHeader = _diffFile->readUint32LE();
		_size = _diffFile->readUint32LE();

		uint32 curPos = 0;

		switch (_lastBlockHeader) {
		case 8:
			_diffFile->read(_diffPalette, _size);
			_isPal = true;
			break;

		case 10:
			if (onlyDiffData) {
				if (curBit > 0)
					error("diffNextFrame: attempt to read screen to non-zero plane (%d)", curBit);
				delete[] _scrollScreenBuffer;
				_scrollScreenBuffer = new byte[_headerdata._width * _headerdata._height];
				_diffFile->read(_scrollScreenBuffer, _size);
			} else {
				_diffFile->read(buf, _size);
			}
			curBit++;
			break;

		case 11:
			curPos = _diffFile->pos();
			_diffFile->skip(4);
			_vm->_utils->runLengthDecode(buf, _diffFile);
			curBit++;
			_diffFile->seek(curPos + _size, SEEK_SET);
			break;

		case 12:
			curPos = _diffFile->pos();
			_diffFile->skip(4);
			_vm->_utils->verticalRunLengthDecode(buf, _diffFile, bufPitch);
			curBit++;
			_diffFile->seek(curPos + _size, SEEK_SET);
			break;

		case 20:
			curPos = _diffFile->pos();
			_vm->_utils->unDiff(buf, buf, _diffFile, bufPitch, false);
			curBit++;
			_diffFile->seek(curPos + _size, SEEK_SET);
			break;

		case 21:
			curPos = _diffFile->pos();
			_vm->_utils->unDiff(buf, buf, _diffFile, bufPitch, true);
			curBit++;
			_diffFile->seek(curPos + _size, SEEK_SET);
			break;

		case 25:
		case 26:
			curBit++;
			break;

		case 30:
		case 31:
			if (_waitForEffect) {
				while (_vm->_music->isSoundEffectActive()) {
					_vm->updateEvents();
					_vm->waitTOF();
				}
			}

			_size -= 8;

			_diffFile->skip(4);
			_sampleSpeed = _diffFile->readUint16LE();
			_diffFile->skip(2);

			// Sound effects embedded in animations are started here. These are
			// usually animation-specific, like door opening sounds, and are not looped
			_vm->_music->playSoundEffect(_sampleSpeed, _size, false, _diffFile);
			break;

		case 65535:
			if ((_frameNum == 1) || _playOnce || _stopPlayingEnd) {
				bool didTOF = false;

				if (_waitForEffect) {
					while (_vm->_music->isSoundEffectActive()) {
						_vm->updateEvents();
						_vm->waitTOF();

						if (drawOnScreen)
							didTOF = true;
					}
				}

				_isPlaying = false;
				_vm->_event->mouseShow();

				if (!didTOF)
					_vm->_graphics->screenUpdate();

				return;
			}

			// Random frame number so it never gets back to 2
			_frameNum = 4;
			_diffFile->seek(_diffFileStart, SEEK_SET);
			break;

		default:
			_diffFile->skip(_size);
			break;
		}
	}
}

void Anim::stopDiff() {
	if (_isPlaying && _isAnim)
		_vm->_graphics->blackScreen();
}

void Anim::stopDiffEnd() {
	if (!_isPlaying)
		return;

	_stopPlayingEnd = true;
	while (_isPlaying) {
		_vm->updateEvents();
		diffNextFrame();
	}
}

void Anim::readDiff(Common::File *diffFile, bool playOnce, bool onlyDiffData) {
	_playOnce = playOnce;
	_delayMicros = 0;
	_frameNum = 0;
	_numChunks = 1;
	_donePal = false;
	_stopPlayingEnd = false;
	_isPlaying = true;

	if (_doBlack) {
		_doBlack = false;
		_vm->_graphics->blackScreen();
	}

	_diffFile = diffFile;

	_continuous = false;
	uint32 signature1 = _diffFile->readUint32BE();
	uint32 signature2 = _diffFile->readUint32LE();

	if ((signature1 != MKTAG('D', 'I', 'F', 'F')) || (signature2 != 1219009121)) {
		_isPlaying = false;
		return;
	}

	uint32 signature3 = _diffFile->readUint32LE();
	_size = _diffFile->readUint32LE();

	if (signature3 != 0)
		return;

	// sizeof(headerdata) != 18, but the padding might be at the end
	// 2 bytes, version, unused.
	_diffFile->skip(2);
	_headerdata._width = _diffFile->readUint16LE();
	_headerdata._height = _diffFile->readUint16LE();
	// 1 byte, depth, unused
	_diffFile->skip(1);
	_headerdata._fps = _diffFile->readByte();

	// HACK: The original game defines a 1 second delay when changing screens, which is
	// very annoying. We first removed the delay, but it looked wrong when changing screens
	// as it was possible to see that something was displayed, without being able to tell
	// what it was. A shorter delay (150ms) makes it acceptable during gameplay and
	// readable. The big question is: do we need that message?
	_vm->_system->delayMillis(150);

	if (_headerdata._fps == 1)
		_headerdata._fps = 0;

	// 4 + 2 bytes, buffer size and machine, unused
	_diffFile->skip(6);
	_headerdata._flags = _diffFile->readUint32LE();

	_diffFile->skip(_size - 18);

	_continuous = CONTINUOUS & _headerdata._flags;
	_diffWidth = _headerdata._width;
	_diffHeight = _headerdata._height;
	_vm->_utils->setBytesPerRow(_diffWidth);

	delete[] _scrollScreenBuffer;
	_scrollScreenBuffer = nullptr;

	if (_headerdata._fps)
		_delayMicros = 1000 / _headerdata._fps;

	_lastBlockHeader = signature3;
	if (_playOnce) {
		while (_lastBlockHeader != 65535)
			diffNextFrame(onlyDiffData);
	} else
		diffNextFrame(onlyDiffData);
}

} // End of namespace Lab