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
|
/* 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.
*
* $URL$
* $Id$
*
*/
#include "common/endian.h"
#include "common/system.h"
#include "kyra/kyra_v1.h"
#include "kyra/kyra_v2.h"
#include "kyra/screen.h"
#include "kyra/screen_v2.h"
#include "kyra/wsamovie.h"
#include "kyra/resource.h"
namespace Kyra {
WSAMovie_v1::WSAMovie_v1(KyraEngine_v1 *vm) : Movie(vm) {}
WSAMovie_v1::~WSAMovie_v1() { close(); }
int WSAMovie_v1::open(const char *filename, int offscreenDecode, uint8 *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 + 8 + (_numFrames << 2), 0x300);
}
if (offscreenDecode) {
_flags |= WF_OFFSCREEN_DECODE;
const int offscreenBufferSize = _width * _height;
_offscreenBuffer = new uint8[offscreenBufferSize];
memset(_offscreenBuffer, 0, offscreenBufferSize);
}
if (_numFrames & 0x8000) {
// This is used in the Amiga version, the wsa playing code
// doesn't include any handling of it though, so we disable
// this warning for now.
//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 WSAMovie_v1::close() {
if (_opened) {
delete[] _deltaBuffer;
delete[] _offscreenBuffer;
delete[] _frameOffsTable;
delete[] _frameData;
_opened = false;
}
}
void WSAMovie_v1::displayFrame(int frameNum, int pageNum, int x, int y, ...) {
if (frameNum >= _numFrames || !_opened)
return;
_x = x;
_y = y;
_drawPage = pageNum;
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, false);
}
_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 WSAMovie_v1::processFrame(int frameNum, uint8 *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 -
WSAMovieAmiga::WSAMovieAmiga(KyraEngine_v1 *vm) : WSAMovie_v1(vm), _buffer(0) {}
int WSAMovieAmiga::open(const char *filename, int offscreenDecode, uint8 *palBuf) {
int res = WSAMovie_v1::open(filename, offscreenDecode, palBuf);
if (!res)
return 0;
_buffer = new uint8[_width * _height];
assert(_buffer);
return res;
}
void WSAMovieAmiga::close() {
if (_opened) {
delete[] _buffer;
_buffer = 0;
}
WSAMovie_v1::close();
}
void WSAMovieAmiga::displayFrame(int frameNum, int pageNum, int x, int y, ...) {
if (frameNum >= _numFrames || frameNum < 0 || !_opened)
return;
_x = x;
_y = y;
_drawPage = pageNum;
uint8 *dst;
dst = _buffer;
memset(_buffer, 0, _width*_height);
if (_currentFrame == _numFrames) {
if (!(_flags & WF_NO_FIRST_FRAME)) {
Screen::decodeFrameDelta(dst, _deltaBuffer, true);
Screen::convertAmigaGfx(dst, _width, _height);
if (_flags & WF_OFFSCREEN_DECODE) {
dst = _offscreenBuffer;
const uint8 *src = _buffer;
int size = _width * _height;
for (int i = 0; i < size; ++i)
*dst++ ^= *src++;
dst = _buffer;
} else {
_vm->screen()->copyBlockToPage(_drawPage, _x, _y, _width, _height, _buffer);
}
}
_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 WSAMovieAmiga::processFrame(int frameNum, uint8 *dst) {
if (!_opened)
return;
assert(frameNum <= _numFrames);
memset(dst, 0, _width*_height);
const uint8 *src = _frameData + _frameOffsTable[frameNum];
Screen::decodeFrame4(src, _deltaBuffer, _deltaBufferSize);
Screen::decodeFrameDelta(dst, _deltaBuffer, true);
Screen::convertAmigaGfx(dst, _width, _height);
src = dst;
dst = 0;
int dstPitch = 0;
if (_flags & WF_OFFSCREEN_DECODE) {
dst = _offscreenBuffer;
dstPitch = _width;
} else {
dst = _vm->screen()->getPageRect(_drawPage, _x, _y, _width, _height);
dstPitch = Screen::SCREEN_W;
}
for (int y = 0; y < _height; ++y) {
for (int x = 0; x < _width; ++x)
*dst++ ^= *src++;
dst += dstPitch - _width;
}
}
#pragma mark -
WSAMovie_v2::WSAMovie_v2(KyraEngine_v1 *vm, Screen_v2 *screen) : WSAMovie_v1(vm), _screen(screen), _xAdd(0), _yAdd(0) {}
int WSAMovie_v2::open(const char *filename, int unk1, uint8 *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)
_vm->screen()->loadPalette(wsaData + 8 + ((_numFrames << 2) & 0xFFFF), palBuf, 0x300);
}
if (flags & 2) {
if (_vm->gameFlags().use16ColorMode) {
offsPal = 0x30;
_flags |= WF_HAS_PALETTE;
if (palBuf)
_vm->screen()->loadPalette(wsaData + 8 + ((_numFrames << 2) & 0xFFFF), palBuf, 0x30);
}
_flags |= WF_XOR;
}
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;
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);
wsaData += 4;
}
for (int i = 1; i < _numFrames; ++i)
_frameOffsTable[i] -= frameDataOffs;
// WSA movies without last frame offset need special treatment
if (_frameOffsTable[_numFrames + 1])
_frameOffsTable[_numFrames] -= frameDataOffs;
else
_frameOffsTable[_numFrames] = 0;
// 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 WSAMovie_v2::displayFrame(int frameNum, int pageNum, int x, int y, ...) {
if (frameNum >= _numFrames || frameNum < 0 || !_opened)
return;
_x = x + _xAdd;
_y = y + _yAdd;
_drawPage = pageNum;
uint8 *dst = 0;
if (_flags & WF_OFFSCREEN_DECODE)
dst = _offscreenBuffer;
else
dst = _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, (_flags & WF_XOR) == 0);
}
_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) {
int pageBackUp = _screen->_curPage;
_screen->_curPage = _drawPage;
va_list args;
va_start(args, y);
int copyParam = va_arg(args, int);
int plotFunc = (copyParam & 0xFF00) >> 12;
int unk1 = copyParam & 0xFF;
const uint8 *unkPtr1 = va_arg(args, const uint8*);
const uint8 *unkPtr2 = va_arg(args, const uint8*);
va_end(args);
_screen->copyWsaRect(_x, _y, _width, _height, 0, plotFunc, _offscreenBuffer, unk1, unkPtr1, unkPtr2);
_screen->_curPage = pageBackUp;
}
}
} // end of namespace Kyra
|