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
|
/* 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/scummsys.h"
#include "graphics/palette.h"
#include "audio/audiostream.h"
#include "audio/decoders/raw.h"
#include "composer/composer.h"
#include "composer/graphics.h"
#include "composer/resource.h"
namespace Composer {
bool Sprite::contains(const Common::Point &pos) const {
Common::Point adjustedPos = pos - _pos;
if (adjustedPos.x < 0 || adjustedPos.x >= _surface.w)
return false;
if (adjustedPos.y < 0 || adjustedPos.y >= _surface.h)
return false;
byte *pixels = (byte *)_surface.pixels;
return (pixels[(_surface.h - adjustedPos.y - 1) * _surface.w + adjustedPos.x] != 0);
}
enum {
kAnimOpEvent = 1,
kAnimOpPlayWave = 2,
kAnimOpPlayAnim = 3,
kAnimOpDrawSprite = 4
};
Animation::Animation(Common::SeekableReadStream *stream, uint16 id, Common::Point basePos, uint32 eventParam)
: _stream(stream), _id(id), _basePos(basePos), _eventParam(eventParam) {
uint32 size = _stream->readUint32LE();
_state = _stream->readUint32LE() + 1;
// probably total size?
uint32 unknown = _stream->readUint32LE();
debug(8, "anim: size %d, state %08x, unknown %08x", size, _state, unknown);
for (uint i = 0; i < size; i++) {
AnimationEntry entry;
entry.op = _stream->readUint16LE();
entry.priority = _stream->readUint16LE();
entry.state = _stream->readUint16LE();
entry.counter = 0;
entry.prevValue = 0;
debug(8, "anim entry: %04x, %04x, %04x", entry.op, entry.priority, entry.state);
_entries.push_back(entry);
}
_offset = _stream->pos();
}
Animation::~Animation() {
delete _stream;
}
void Animation::seekToCurrPos() {
_stream->seek(_offset, SEEK_SET);
}
void ComposerEngine::playAnimation(uint16 animId, int16 x, int16 y, int16 eventParam) {
// First, we check if this animation is already playing,
// and if it is, we sabotage that running one first.
for (Common::List<Animation *>::iterator i = _anims.begin(); i != _anims.end(); i++) {
Animation *anim = *i;
if (anim->_id != animId)
continue;
stopAnimation(*i);
}
Common::SeekableReadStream *stream = NULL;
Pipe *newPipe = NULL;
// First, check the existing pipes.
for (Common::List<Pipe *>::iterator j = _pipes.begin(); j != _pipes.end(); j++) {
Pipe *pipe = *j;
if (!pipe->hasResource(ID_ANIM, animId))
continue;
stream = pipe->getResource(ID_ANIM, animId, false);
break;
}
// If we didn't find it, try the libraries.
if (!stream) {
if (!hasResource(ID_ANIM, animId)) {
warning("ignoring attempt to play invalid anim %d", animId);
return;
}
stream = getResource(ID_ANIM, animId);
uint32 type = 0;
for (Common::List<Library>::iterator i = _libraries.begin(); i != _libraries.end(); i++)
if (i->_archive->hasResource(ID_ANIM, animId)) {
type = i->_archive->getResourceFlags(ID_ANIM, animId);
break;
}
// If the resource is a pipe itself, then load the pipe
// and then fish the requested animation out of it.
if (type != 1) {
newPipe = new Pipe(stream);
_pipes.push_front(newPipe);
stream = newPipe->getResource(ID_ANIM, animId, false);
}
}
Animation *anim = new Animation(stream, animId, Common::Point(x, y), eventParam);
_anims.push_back(anim);
runEvent(1, animId, eventParam, 0);
if (newPipe)
newPipe->_anim = anim;
}
void ComposerEngine::stopAnimation(Animation *anim, bool localOnly, bool pipesOnly) {
// disable the animation
anim->_state = 0;
// stop any animations it may have spawned
for (uint j = 0; j < anim->_entries.size(); j++) {
AnimationEntry &entry = anim->_entries[j];
if (!entry.prevValue)
continue;
if (localOnly) {
if (pipesOnly)
continue;
if (entry.op == kAnimOpDrawSprite) {
removeSprite(entry.prevValue, anim->_id);
} else if (entry.op == kAnimOpPlayWave) {
if (_currSoundPriority >= entry.priority) {
_mixer->stopAll();
_audioStream = NULL;
}
}
} else {
if (entry.op != kAnimOpPlayAnim)
continue;
for (Common::List<Animation *>::iterator i = _anims.begin(); i != _anims.end(); i++) {
if ((*i)->_id == entry.prevValue)
stopAnimation(*i);
}
}
}
// kill any pipe owned by the animation
for (Common::List<Pipe *>::iterator j = _pipes.begin(); j != _pipes.end(); j++) {
Pipe *pipe = *j;
if (pipe->_anim != anim)
continue;
j = _pipes.reverse_erase(j);
delete pipe;
break;
}
}
void ComposerEngine::playWaveForAnim(uint16 id, uint16 priority, bool bufferingOnly) {
if (_audioStream && _audioStream->numQueuedStreams() != 0) {
if (_currSoundPriority < priority)
return;
if (_currSoundPriority > priority) {
_mixer->stopAll();
_audioStream = NULL;
}
}
Common::SeekableReadStream *stream = NULL;
if (!bufferingOnly && hasResource(ID_WAVE, id)) {
stream = getResource(ID_WAVE, id);
} else {
for (Common::List<Pipe *>::iterator k = _pipes.begin(); k != _pipes.end(); k++) {
Pipe *pipe = *k;
if (!pipe->hasResource(ID_WAVE, id))
continue;
stream = pipe->getResource(ID_WAVE, id, true);
break;
}
}
if (!stream)
return;
// FIXME: non-pipe buffers have fixed wav header (data at +44, size at +40)
byte *buffer = (byte *)malloc(stream->size());
stream->read(buffer, stream->size());
if (!_audioStream)
_audioStream = Audio::makeQueuingAudioStream(22050, false);
_audioStream->queueBuffer(buffer, stream->size(), DisposeAfterUse::YES, Audio::FLAG_UNSIGNED);
_currSoundPriority = priority;
delete stream;
if (!_mixer->isSoundHandleActive(_soundHandle))
_mixer->playStream(Audio::Mixer::kSFXSoundType, &_soundHandle, _audioStream);
}
void ComposerEngine::processAnimFrame() {
for (Common::List<Animation *>::iterator i = _anims.begin(); i != _anims.end(); i++) {
Animation *anim = *i;
anim->seekToCurrPos();
if (anim->_state <= 1) {
bool normalEnd = (anim->_state == 1);
if (normalEnd) {
runEvent(2, anim->_id, anim->_eventParam, 0);
}
stopAnimation(anim, true, normalEnd);
delete anim;
i = _anims.reverse_erase(i);
continue;
}
for (uint j = 0; j < anim->_entries.size(); j++) {
AnimationEntry &entry = anim->_entries[j];
if (entry.op != kAnimOpEvent)
break;
if (entry.counter) {
entry.counter--;
} else {
if ((anim->_state > 1) && (anim->_stream->pos() + 2 > anim->_stream->size())) {
warning("anim with id %d ended too soon", anim->_id);
anim->_state = 0;
break;
}
uint16 event = anim->_stream->readUint16LE();
anim->_offset += 2;
if (event == 0xffff) {
entry.counter = anim->_stream->readUint16LE() - 1;
anim->_offset += 2;
} else {
debug(4, "anim: event %d", event);
runEvent(event, anim->_id, 0, 0);
}
}
}
}
for (Common::List<Animation *>::iterator i = _anims.begin(); i != _anims.end(); i++) {
Animation *anim = *i;
// did the anim get disabled?
if (anim->_state == 0) {
stopAnimation(anim, true, false);
delete anim;
i = _anims.reverse_erase(i);
continue;
}
anim->_state--;
bool foundWait = false;
for (uint j = 0; j < anim->_entries.size(); j++) {
AnimationEntry &entry = anim->_entries[j];
// only skip these at the start
if (!foundWait && (entry.op == kAnimOpEvent))
continue;
foundWait = true;
if (entry.counter) {
entry.counter--;
if ((entry.op == kAnimOpPlayWave) && entry.prevValue) {
debug(4, "anim: continue play wave %d", entry.prevValue);
playWaveForAnim(entry.prevValue, entry.priority, true);
}
} else {
anim->seekToCurrPos();
if ((anim->_state > 1) && (anim->_stream->pos() + 2 > anim->_stream->size())) {
warning("anim with id %d ended too soon", anim->_id);
anim->_state = 0;
break;
}
uint16 data = anim->_stream->readUint16LE();
anim->_offset += 2;
if (data == 0xffff) {
entry.counter = anim->_stream->readUint16LE() - 1;
anim->_offset += 2;
} else {
switch (entry.op) {
case kAnimOpEvent:
debug(4, "anim: event %d", data);
runEvent(data, anim->_id, 0, 0);
break;
case kAnimOpPlayWave:
debug(4, "anim: play wave %d", data);
playWaveForAnim(data, entry.priority, false);
break;
case kAnimOpPlayAnim:
debug(4, "anim: play anim %d", data);
playAnimation(data, anim->_basePos.x, anim->_basePos.y, 1);
break;
case kAnimOpDrawSprite:
if (!data || (entry.prevValue && (data != entry.prevValue))) {
debug(4, "anim: erase sprite %d", entry.prevValue);
removeSprite(entry.prevValue, anim->_id);
}
if (data) {
int16 x = anim->_stream->readSint16LE();
int16 y = anim->_stream->readSint16LE();
Common::Point pos(x, y);
anim->_offset += 4;
uint16 animId = anim->_id;
if (anim->_state == entry.state)
animId = 0;
debug(4, "anim: draw sprite %d at (relative) %d,%d", data, x, y);
bool wasVisible = spriteVisible(data, animId);
addSprite(data, animId, entry.priority, anim->_basePos + pos);
if (wasVisible) {
// make sure modified sprite isn't removed by another entry
for (uint k = 0; k < anim->_entries.size(); k++) {
if (anim->_entries[k].op != kAnimOpDrawSprite)
continue;
if (anim->_entries[k].prevValue == data)
anim->_entries[k].prevValue = 1;
}
}
}
break;
default:
warning("unknown anim op %d", entry.op);
}
entry.prevValue = data;
}
}
}
}
for (Common::List<Pipe *>::iterator j = _pipes.begin(); j != _pipes.end(); j++) {
Pipe *pipe = *j;
pipe->nextFrame();
}
}
bool ComposerEngine::spriteVisible(uint16 id, uint16 animId) {
for (Common::List<Sprite>::iterator i = _sprites.begin(); i != _sprites.end(); i++) {
if (i->_id != id)
continue;
if (i->_animId && animId && (i->_animId != animId))
continue;
return true;
}
return false;
}
void ComposerEngine::addSprite(uint16 id, uint16 animId, uint16 zorder, const Common::Point &pos) {
Sprite sprite;
bool foundSprite = false;
// re-use old sprite, if any (the BMAP for this id might well have
// changed in the meantime, but the scripts depend on that!)
for (Common::List<Sprite>::iterator i = _sprites.begin(); i != _sprites.end(); i++) {
if (i->_id != id)
continue;
if (i->_animId && animId && (i->_animId != animId))
continue;
// if the zordering is identical, modify it in-place
if (i->_zorder == zorder) {
i->_animId = animId;
i->_pos = pos;
return;
}
// otherwise, take a copy and remove it from the list
sprite = *i;
foundSprite = true;
_sprites.erase(i);
break;
}
sprite._animId = animId;
sprite._zorder = zorder;
sprite._pos = pos;
if (!foundSprite) {
sprite._id = id;
if (!initSprite(sprite)) {
warning("ignoring addSprite on invalid sprite %d", id);
return;
}
}
for (Common::List<Sprite>::iterator i = _sprites.begin(); i != _sprites.end(); i++) {
if (sprite._zorder <= i->_zorder)
continue;
// insert *before* this sprite
_sprites.insert(i, sprite);
return;
}
_sprites.push_back(sprite);
}
void ComposerEngine::removeSprite(uint16 id, uint16 animId) {
for (Common::List<Sprite>::iterator i = _sprites.begin(); i != _sprites.end(); i++) {
if (id && i->_id != id)
continue;
if (i->_animId && animId && (i->_animId != animId))
continue;
i->_surface.free();
i = _sprites.reverse_erase(i);
if (id)
break;
}
}
const Sprite *ComposerEngine::getSpriteAtPos(const Common::Point &pos) {
for (Common::List<Sprite>::iterator i = _sprites.reverse_begin(); i != _sprites.end(); --i) {
// avoid highest-level objects (e.g. the cursor)
if (!i->_zorder)
continue;
if (i->contains(pos))
return &(*i);
}
return NULL;
}
void ComposerEngine::redraw() {
for (Common::List<Sprite>::iterator i = _sprites.begin(); i != _sprites.end(); i++) {
drawSprite(*i);
}
_system->copyRectToScreen((byte *)_surface.pixels, _surface.pitch, 0, 0, _surface.w, _surface.h);
_system->updateScreen();
_needsUpdate = false;
}
void ComposerEngine::loadCTBL(uint id, uint fadePercent) {
Common::SeekableReadStream *stream = getResource(ID_CTBL, id);
uint16 numEntries = stream->readUint16LE();
debug(1, "CTBL: %d entries", numEntries);
assert(numEntries <= 256);
assert(stream->size() == 2 + (numEntries * 3));
byte buffer[256 * 3];
stream->read(buffer, numEntries * 3);
delete stream;
for (uint i = 0; i < numEntries * 3; i++)
buffer[i] = ((unsigned int)buffer[i] * fadePercent) / 100;
_system->getPaletteManager()->setPalette(buffer, 0, numEntries);
}
static void decompressSLWM(byte *buffer, Common::SeekableReadStream *stream) {
uint bitsLeft = 0;
uint16 lastBits;
byte currBit;
while (true) {
if (bitsLeft == 0) { bitsLeft = 16; lastBits = stream->readUint16LE(); }
currBit = (lastBits & 1); lastBits >>= 1; bitsLeft--;
if (currBit) {
// single byte
*buffer++ = stream->readByte();
continue;
}
if (bitsLeft == 0) { bitsLeft = 16; lastBits = stream->readUint16LE(); }
currBit = (lastBits & 1); lastBits >>= 1; bitsLeft--;
uint start;
uint count;
if (currBit) {
uint orMask = stream->readByte();
uint in = stream->readByte();
count = in & 7;
start = ((in & ~7) << 5) | orMask;
if (!count) {
count = stream->readByte();
if (!count)
break;
count -= 2;
}
} else {
// count encoded in the next two bits
count = 0;
if (bitsLeft == 0) { bitsLeft = 16; lastBits = stream->readUint16LE(); }
currBit = (lastBits & 1); lastBits >>= 1; bitsLeft--;
count = (count << 1) | currBit;
if (bitsLeft == 0) { bitsLeft = 16; lastBits = stream->readUint16LE(); }
currBit = (lastBits & 1); lastBits >>= 1; bitsLeft--;
count = (count << 1) | currBit;
start = stream->readByte();
}
count += 2;
start++;
for (uint i = 0; i < count; i++) {
*buffer = *(buffer - start);
buffer++;
}
}
}
// bitmap compression types
enum {
kBitmapUncompressed = 0,
kBitmapSpp32 = 1,
kBitmapSLW8 = 3,
kBitmapRLESLWM = 4,
kBitmapSLWM = 5
};
void ComposerEngine::decompressBitmap(uint16 type, Common::SeekableReadStream *stream, byte *buffer, uint32 size, uint width, uint height) {
switch (type) {
case kBitmapUncompressed:
assert(stream->size() - (uint)stream->pos() == size);
assert(size == width * height);
stream->read(buffer, size);
break;
case kBitmapSpp32:
byte lookup[16];
stream->read(lookup, 16);
while (size--) {
uint in = stream->readByte();
byte lowBits = in & 0xF;
byte highBits = (in & 0xF0) >> 4;
if (highBits == 0xf) {
// run of a single color
uint count = (uint)stream->readByte() + 3;
size--;
memset(buffer, lookup[lowBits], count);
buffer += count;
} else {
// two pixels
*buffer++ = lookup[highBits];
*buffer++ = lookup[lowBits];
}
}
break;
case kBitmapSLW8:
while (size--) {
byte val = stream->readByte();
if (val != 0xff) {
*buffer++ = val;
continue;
}
uint count = stream->readByte();
size--;
uint16 step;
if (!(count & 0x80)) {
step = stream->readByte();
size--;
} else {
count = (count ^ 0x80);
step = stream->readUint16LE();
size -= 2;
}
count += 4;
// this is often overlapping (for repeating patterns)
for (uint i = 0; i < count; i++) {
*buffer = *(buffer - step - 1);
buffer++;
}
}
break;
case kBitmapRLESLWM:
{
uint32 bufSize = stream->readUint32LE();
byte *tempBuf = new byte[bufSize];
decompressSLWM(tempBuf, stream);
uint instrPos = tempBuf[0] + 1;
instrPos += READ_LE_UINT16(tempBuf + instrPos) + 2;
byte *instr = tempBuf + instrPos;
uint line = 0;
while (line++ < height) {
uint pixels = 0;
while (pixels < width) {
byte data = *instr++;
byte color = tempBuf[(data & 0x7F) + 1];
if (!(data & 0x80)) {
*buffer++ = color;
pixels++;
} else {
byte count = *instr++;
if (!count) {
while (pixels++ < width)
*buffer++ = color;
break;
}
for (uint i = 0; i < count; i++) {
*buffer++ = color;
pixels++;
}
}
}
}
delete[] tempBuf;
}
break;
case kBitmapSLWM:
decompressSLWM(buffer, stream);
break;
default:
error("decompressBitmap can't handle type %d", type);
}
}
Common::SeekableReadStream *ComposerEngine::getStreamForSprite(uint16 id) {
for (Common::List<Pipe *>::iterator k = _pipes.begin(); k != _pipes.end(); k++) {
Pipe *pipe = *k;
if (!pipe->hasResource(ID_BMAP, id))
continue;
return pipe->getResource(ID_BMAP, id, true);
}
if (hasResource(ID_BMAP, id))
return getResource(ID_BMAP, id);
return NULL;
}
bool ComposerEngine::initSprite(Sprite &sprite) {
Common::SeekableReadStream *stream = getStreamForSprite(sprite._id);
if (!stream)
return false;
uint16 type = stream->readUint16LE();
int16 height = stream->readSint16LE();
int16 width = stream->readSint16LE();
uint32 size = stream->readUint32LE();
debug(1, "loading BMAP: type %d, width %d, height %d, size %d", type, width, height, size);
if (width > 0 && height > 0) {
sprite._surface.create(width, height, Graphics::PixelFormat::createFormatCLUT8());
decompressBitmap(type, stream, (byte *)sprite._surface.pixels, size, width, height);
} else {
// there are some sprites (e.g. a -998x-998 one in Gregory's title screen)
// which have an invalid size, but the original engine doesn't notice for
// RLE sprites since the width/height is ignored until the actual draw
if (type != kBitmapRLESLWM)
error("sprite (type %d) had invalid size %dx%d", type, width, height);
}
delete stream;
return true;
}
void ComposerEngine::drawSprite(const Sprite &sprite) {
int x = sprite._pos.x;
int y = sprite._pos.y;
// incoming data is BMP-style (bottom-up), so flip it
byte *pixels = (byte *)_surface.pixels;
for (int j = 0; j < sprite._surface.h; j++) {
if (j + y < 0)
continue;
if (j + y >= _surface.h)
break;
byte *in = (byte *)sprite._surface.pixels + (sprite._surface.h - j - 1) * sprite._surface.w;
byte *out = pixels + ((j + y) * _surface.w) + x;
for (int i = 0; i < sprite._surface.w; i++)
if ((x + i >= 0) && (x + i < _surface.w) && in[i])
out[i] = in[i];
}
}
} // End of namespace Composer
|