aboutsummaryrefslogtreecommitdiff
path: root/engines/kyra/wsamovie.cpp
blob: a6d90aa7aa0d43e9503476bbdd54566b53f362ba (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
/* ScummVM - Scumm Interpreter
 * Copyright (C) 2004-2006 The ScummVM project
 *
 * 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.
 *
 * $URL$
 * $Id$
 *
 */

#include "common/stdafx.h"
#include "common/endian.h"
#include "common/system.h"

#include "kyra/kyra.h"
#include "kyra/kyra3.h"
#include "kyra/screen.h"
#include "kyra/wsamovie.h"


namespace Kyra {
WSAMovieV1::WSAMovieV1(KyraEngine *vm) : Movie(vm) {}
WSAMovieV1::~WSAMovieV1() { close(); }

int WSAMovieV1::open(const char *filename, int offscreenDecode, uint8 *palBuf) {
	debugC(9, kDebugLevelMovie, "WSAMovieV1::open('%s', %d, %p)", filename, offscreenDecode, (const void *)palBuf);
	close();

	uint32 flags = 0;
	uint32 fileSize;
	uint8 *p = _vm->resource()->fileData(filename, &fileSize);
	if (!p)
		return 0;
	
	const uint8 *wsaData = p;
	_numFrames = READ_LE_UINT16(wsaData); wsaData += 2;
	_width = READ_LE_UINT16(wsaData); wsaData += 2;
	_height = READ_LE_UINT16(wsaData); wsaData += 2;
	_deltaBufferSize = READ_LE_UINT16(wsaData); wsaData += 2;
	_offscreenBuffer = NULL;
	_flags = 0;
	if (_vm->gameFlags().useAltShapeHeader) {
		flags = READ_LE_UINT16(wsaData); wsaData += 2;
	}
	
	uint32 offsPal = 0;
	if (flags & 1) {
		offsPal = 0x300;
		_flags |= WF_HAS_PALETTE;
		if (palBuf) {
			memcpy(palBuf, wsaData + (_numFrames + 2) * 4, 0x300);
		}
	}
	
	if (offscreenDecode) {
		_flags |= WF_OFFSCREEN_DECODE;
		const int offscreenBufferSize = _width * _height;
		_offscreenBuffer = new uint8[offscreenBufferSize];
		memset(_offscreenBuffer, 0, offscreenBufferSize);
	}

	if (_numFrames & 0x8000) {
		warning("Unhandled wsa flags 0x80");
		_flags |= 0x80;
		_numFrames &= 0x7FFF;
	}
	_currentFrame = _numFrames;

	_deltaBuffer = new uint8[_deltaBufferSize];
	memset(_deltaBuffer, 0, _deltaBufferSize);
	
	// read frame offsets
	_frameOffsTable = new uint32[_numFrames + 2];
	_frameOffsTable[0] = 0;
	uint32 frameDataOffs = READ_LE_UINT32(wsaData); wsaData += 4;
	bool firstFrame = true;
	if (frameDataOffs == 0) {
		firstFrame = false;
		frameDataOffs = READ_LE_UINT32(wsaData);
		_flags |= WF_NO_FIRST_FRAME;
	}
	for (int i = 1; i < _numFrames + 2; ++i) {
		_frameOffsTable[i] = READ_LE_UINT32(wsaData) - frameDataOffs;
		wsaData += 4;
	}
	
	// skip palette
	wsaData += offsPal;
	
	// read frame data
	const int frameDataSize = p + fileSize - wsaData;
	_frameData = new uint8[frameDataSize];
	memcpy(_frameData, wsaData, frameDataSize);
	
	// decode first frame
	if (firstFrame) {
		Screen::decodeFrame4(_frameData, _deltaBuffer, _deltaBufferSize);
	}
	
	delete [] p;
	_opened = true;
	
	return _numFrames;
}

void WSAMovieV1::close() {
	debugC(9, kDebugLevelMovie, "WSAMovieV1::close()");
	if (_opened) {
		delete [] _deltaBuffer;
		delete [] _offscreenBuffer;
		delete [] _frameOffsTable;
		delete [] _frameData;
		_opened = false;
	}
}

void WSAMovieV1::displayFrame(int frameNum) {
	debugC(9, kDebugLevelMovie, "WSAMovieV1::displayFrame(%d)", frameNum);
	if (frameNum >= _numFrames || !_opened)
		return;

	uint8 *dst;
	if (_flags & WF_OFFSCREEN_DECODE) {
		dst = _offscreenBuffer;
	} else {
		dst = _vm->screen()->getPageRect(_drawPage, _x, _y, _width, _height);
	}
		
	if (_currentFrame == _numFrames) {
		if (!(_flags & WF_NO_FIRST_FRAME)) {
			if (_flags & WF_OFFSCREEN_DECODE) {
				Screen::decodeFrameDelta(dst, _deltaBuffer);
			} else {
				Screen::decodeFrameDeltaPage(dst, _deltaBuffer, _width, true);
			}
		}
		_currentFrame = 0;
	}

	// try to reduce the number of needed frame operations
	int diffCount = ABS(_currentFrame - frameNum);
	int frameStep = 1;
	int frameCount;
	if (_currentFrame < frameNum) {
		frameCount = _numFrames - frameNum + _currentFrame;
		if (diffCount > frameCount) {
			frameStep = -1;
		} else {
			frameCount = diffCount;
		}
	} else {
		frameCount = _numFrames - _currentFrame + frameNum;
		if (frameCount >= diffCount) {
			frameStep = -1;
			frameCount = diffCount;
		}
	}
	
	// process
	if (frameStep > 0) {
		uint16 cf = _currentFrame;
		while (frameCount--) {
			cf += frameStep;
			processFrame(cf, dst);
			if (cf == _numFrames) {
				cf = 0;
			}
		}
	} else {
		uint16 cf = _currentFrame;
		while (frameCount--) {
			if (cf == 0) {
				cf = _numFrames;
			}
			processFrame(cf, dst);
			cf += frameStep;
		}
	}
	
	// display
	_currentFrame = frameNum;
	if (_flags & WF_OFFSCREEN_DECODE) {
		_vm->screen()->copyBlockToPage(_drawPage, _x, _y, _width, _height, _offscreenBuffer);
	}
}

void WSAMovieV1::processFrame(int frameNum, uint8 *dst) {
	debugC(9, kDebugLevelMovie, "WSAMovieV1::processFrame(%d, %p)", frameNum, (const void *)dst);
	if (!_opened)
		return;
	assert(frameNum <= _numFrames);
	const uint8 *src = _frameData + _frameOffsTable[frameNum];
	Screen::decodeFrame4(src, _deltaBuffer, _deltaBufferSize);
	if (_flags & WF_OFFSCREEN_DECODE) {
		Screen::decodeFrameDelta(dst, _deltaBuffer);
	} else {
		Screen::decodeFrameDeltaPage(dst, _deltaBuffer, _width, false);
	}
}

#pragma mark -

WSAMovieV2::WSAMovieV2(KyraEngine *vm) : WSAMovieV1(vm), _xAdd(0), _yAdd(0) {}

int WSAMovieV2::open(const char *filename, int unk1, uint8 *palBuf) {
	debugC(9, kDebugLevelMovie, "WSAMovieV3::open('%s', %d, %p)", filename, unk1, (const void *)palBuf);
	close();

	uint32 flags = 0;
	uint32 fileSize;
	uint8 *p = _vm->resource()->fileData(filename, &fileSize);
	if (!p) {
		warning("couldn't load wsa file: '%s'", filename);
		return 0;
	}
	
	const uint8 *wsaData = p;
	_numFrames = READ_LE_UINT16(wsaData); wsaData += 2;
	_xAdd = (int16)(READ_LE_UINT16(wsaData)); wsaData += 2;
	_yAdd = (int16)(READ_LE_UINT16(wsaData)); wsaData += 2;
	_width = READ_LE_UINT16(wsaData); wsaData += 2;
	_height = READ_LE_UINT16(wsaData); wsaData += 2;
	_deltaBufferSize = READ_LE_UINT16(wsaData); wsaData += 2;
	_offscreenBuffer = NULL;
	_flags = 0;
	flags = READ_LE_UINT16(wsaData); wsaData += 2;
	
	uint32 offsPal = 0;
	if (flags & 1) {
		offsPal = 0x300;
		_flags |= WF_HAS_PALETTE;
		if (palBuf) {
			memcpy(palBuf, wsaData + 8 + ((_numFrames << 2) & 0xFFFF), 0x300);
		}
	}
	
	if (flags & 2) {
		_flags |= WF_MASKED_BLIT;
	}
	
	if (!(unk1 & 2)) {
		_flags |= WF_OFFSCREEN_DECODE;
		const int offscreenBufferSize = _width * _height;
		_offscreenBuffer = new uint8[offscreenBufferSize];
		memset(_offscreenBuffer, 0, offscreenBufferSize);
	}

	if (_numFrames & 0x8000) {
		warning("Unhandled wsa flags 0x80");
		_flags |= 0x80;
		_numFrames &= 0x7FFF;
	}
	_currentFrame = _numFrames;

	_deltaBuffer = new uint8[_deltaBufferSize];
	memset(_deltaBuffer, 0, _deltaBufferSize);
	
	// read frame offsets
	_frameOffsTable = new uint32[_numFrames + 2];
	_frameOffsTable[0] = 0;
	uint32 frameDataOffs = READ_LE_UINT32(wsaData); wsaData += 4;
	for (int i = 1; i < _numFrames + 2; ++i) {
		_frameOffsTable[i] = READ_LE_UINT32(wsaData) - frameDataOffs;
		wsaData += 4;
	}
	
	// skip palette
	wsaData += offsPal;
	
	// read frame data
	const int frameDataSize = p + fileSize - wsaData;
	_frameData = new uint8[frameDataSize];
	memcpy(_frameData, wsaData, frameDataSize);
	
	// decode first frame
	Screen::decodeFrame4(_frameData, _deltaBuffer, _deltaBufferSize);
	
	delete [] p;
	_opened = true;
	
	return _numFrames;
}

} // end of namespace Kyra