1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "draci/draci.h"
24 #include "draci/animation.h"
25 #include "draci/barchive.h"
26 #include "draci/game.h"
27 #include "draci/screen.h"
28 #include "draci/sound.h"
29 #include "draci/surface.h"
30
31 #include "common/memstream.h"
32 #include "common/system.h"
33
34 namespace Draci {
35
Animation(DraciEngine * vm,int id,uint z,bool playing)36 Animation::Animation(DraciEngine *vm, int id, uint z, bool playing) : _vm(vm) {
37 _id = id;
38 _index = kIgnoreIndex;
39 _z = z;
40 clearShift();
41 _displacement = kNoDisplacement;
42 _playing = playing;
43 _looping = false;
44 _paused = false;
45 _canBeQuick = false;
46 _tick = _vm->_system->getMillis();
47 _currentFrame = 0;
48 _hasChangedFrame = true;
49 _callback = &Animation::doNothing;
50 _isRelative = false;
51 }
52
~Animation()53 Animation::~Animation() {
54 deleteFrames();
55 }
56
setRelative(int relx,int rely)57 void Animation::setRelative(int relx, int rely) {
58 // Delete the previous frame if there is one
59 if (_frames.size() > 0)
60 markDirtyRect(_vm->_screen->getSurface());
61
62 _displacement.relX = relx;
63 _displacement.relY = rely;
64 }
65
getCurrentFrameDisplacement() const66 Displacement Animation::getCurrentFrameDisplacement() const {
67 Displacement dis = _displacement;
68 dis.relX += scummvm_lround(dis.extraScaleX * _shift.x);
69 dis.relY += scummvm_lround(dis.extraScaleY * _shift.y);
70 return dis;
71 }
72
getCurrentFramePosition() const73 Common::Point Animation::getCurrentFramePosition() const {
74 Displacement dis = getCurrentFrameDisplacement();
75 return Common::Point(dis.relX, dis.relY);
76 }
77
setLooping(bool looping)78 void Animation::setLooping(bool looping) {
79 _looping = looping;
80 debugC(7, kDraciAnimationDebugLevel, "Setting looping to %d on animation %d",
81 looping, _id);
82 }
83
markDirtyRect(Surface * surface) const84 void Animation::markDirtyRect(Surface *surface) const {
85 if (getFrameCount() == 0)
86 return;
87
88 // Fetch the current frame's rectangle
89 const Drawable *frame = getConstCurrentFrame();
90 Common::Rect frameRect = frame->getRect(getCurrentFrameDisplacement());
91
92 // Mark the rectangle dirty on the surface
93 surface->markDirtyRect(frameRect);
94 }
95
nextFrame(bool force)96 void Animation::nextFrame(bool force) {
97 // If there are no frames or if the animation is not playing, return
98 if (getFrameCount() == 0 || !_playing)
99 return;
100
101 const Drawable *frame = getConstCurrentFrame();
102 Surface *surface = _vm->_screen->getSurface();
103
104 if (force || (_tick + frame->getDelay() <= _vm->_system->getMillis()) ||
105 (_canBeQuick && _vm->_game->getEnableQuickHero() && _vm->_game->getWantQuickHero())) {
106 // If we are at the last frame and not looping, stop the animation
107 // The animation is also restarted to frame zero
108 if ((_currentFrame == getFrameCount() - 1) && !_looping) {
109 // When the animation reaches its end, call the preset callback
110 (this->*_callback)();
111 } else {
112 // Mark old frame dirty so it gets deleted
113 markDirtyRect(surface);
114
115 _shift.x += _relativeShifts[_currentFrame].x;
116 _shift.y += _relativeShifts[_currentFrame].y;
117 _currentFrame = nextFrameNum();
118 _tick = _vm->_system->getMillis();
119
120 // Fetch new frame and mark it dirty
121 markDirtyRect(surface);
122
123 // If the animation is paused, then nextFrameNum()
124 // returns the same frame number even though the time
125 // has elapsed to switch to another frame. We must not
126 // flip _hasChangedFrame to true, otherwise the sample
127 // assigned to this frame will be re-started over and
128 // over until all sound handles are exhausted (happens,
129 // e.g., when switching to the inventory which pauses
130 // all animations).
131 _hasChangedFrame = !_paused;
132 }
133 }
134
135 debugC(6, kDraciAnimationDebugLevel,
136 "anim=%d tick=%d delay=%d tick+delay=%d currenttime=%d frame=%d framenum=%d x=%d y=%d z=%d",
137 _id, _tick, frame->getDelay(), _tick + frame->getDelay(), _vm->_system->getMillis(),
138 _currentFrame, _frames.size(), frame->getX() + getRelativeX(), frame->getY() + getRelativeY(), _z);
139 }
140
nextFrameNum() const141 uint Animation::nextFrameNum() const {
142 if (_paused)
143 return _currentFrame;
144
145 if ((_currentFrame == getFrameCount() - 1) && _looping)
146 return 0;
147 else
148 return _currentFrame + 1;
149 }
150
drawFrame(Surface * surface)151 void Animation::drawFrame(Surface *surface) {
152 // If there are no frames or the animation is not playing, return
153 if (_frames.size() == 0 || !_playing)
154 return;
155
156 const Drawable *frame = getConstCurrentFrame();
157
158 if (_id == kOverlayImage) {
159 // No displacement or relative animations is supported.
160 frame->draw(surface, false, 0, 0);
161 } else {
162 // Draw frame: first shifted by the relative shift and then
163 // scaled/shifted by the given displacement.
164 frame->drawReScaled(surface, false, getCurrentFrameDisplacement());
165 }
166
167 const SoundSample *sample = _samples[_currentFrame];
168 if (_hasChangedFrame && sample) {
169 uint duration = _vm->_sound->playSound(sample, Audio::Mixer::kMaxChannelVolume, false);
170 debugC(3, kDraciSoundDebugLevel,
171 "Playing sample on animation %d, frame %d: %d+%d at %dHz: %dms",
172 _id, _currentFrame, sample->_offset, sample->_length, sample->_frequency, duration);
173 }
174 _hasChangedFrame = false;
175 }
176
setPlaying(bool playing)177 void Animation::setPlaying(bool playing) {
178 _tick = _vm->_system->getMillis();
179 _playing = playing;
180
181 // When restarting an animation, allow playing sounds.
182 _hasChangedFrame |= playing;
183 }
184
setScaleFactors(double scaleX,double scaleY)185 void Animation::setScaleFactors(double scaleX, double scaleY) {
186 debugC(5, kDraciAnimationDebugLevel,
187 "Setting scaling factors on anim %d (scaleX: %.3f scaleY: %.3f)",
188 _id, scaleX, scaleY);
189
190 markDirtyRect(_vm->_screen->getSurface());
191
192 _displacement.extraScaleX = scaleX;
193 _displacement.extraScaleY = scaleY;
194 }
195
addFrame(Drawable * frame,const SoundSample * sample)196 void Animation::addFrame(Drawable *frame, const SoundSample *sample) {
197 _frames.push_back(frame);
198 _samples.push_back(sample);
199 _relativeShifts.push_back(Common::Point(0, 0));
200 }
201
makeLastFrameRelative(int x,int y)202 void Animation::makeLastFrameRelative(int x, int y) {
203 _relativeShifts.back() = Common::Point(x, y);
204 }
205
clearShift()206 void Animation::clearShift() {
207 _shift = Common::Point(0, 0);
208 }
209
replaceFrame(int i,Drawable * frame,const SoundSample * sample)210 void Animation::replaceFrame(int i, Drawable *frame, const SoundSample *sample) {
211 _frames[i] = frame;
212 _samples[i] = sample;
213 }
214
getConstCurrentFrame() const215 const Drawable *Animation::getConstCurrentFrame() const {
216 // If there are no frames stored, return NULL
217 return _frames.size() > 0 ? _frames[_currentFrame] : NULL;
218 }
219
getCurrentFrame()220 Drawable *Animation::getCurrentFrame() {
221 // If there are no frames stored, return NULL
222 return _frames.size() > 0 ? _frames[_currentFrame] : NULL;
223 }
224
getFrame(int frameNum)225 Drawable *Animation::getFrame(int frameNum) {
226 // If there are no frames stored, return NULL
227 return _frames.size() > 0 ? _frames[frameNum] : NULL;
228 }
229
setCurrentFrame(uint frame)230 void Animation::setCurrentFrame(uint frame) {
231 // Check whether the value is sane
232 if (frame >= _frames.size()) {
233 return;
234 }
235
236 _currentFrame = frame;
237 }
238
deleteFrames()239 void Animation::deleteFrames() {
240 // If there are no frames to delete, return
241 if (_frames.size() == 0) {
242 return;
243 }
244
245 markDirtyRect(_vm->_screen->getSurface());
246
247 for (int i = getFrameCount() - 1; i >= 0; --i) {
248 delete _frames[i];
249 _frames.pop_back();
250 }
251 _relativeShifts.clear();
252 _samples.clear();
253 }
254
exitGameLoop()255 void Animation::exitGameLoop() {
256 _vm->_game->setExitLoop(true);
257 }
258
tellWalkingState()259 void Animation::tellWalkingState() {
260 _vm->_game->heroAnimationFinished();
261 }
262
play()263 void Animation::play() {
264 if (isPlaying()) {
265 return;
266 }
267
268 // Mark the first frame dirty so it gets displayed
269 markDirtyRect(_vm->_screen->getSurface());
270
271 setPlaying(true);
272
273 debugC(3, kDraciAnimationDebugLevel, "Playing animation %d...", getID());
274 }
275
stop()276 void Animation::stop() {
277 if (!isPlaying()) {
278 return;
279 }
280
281 // Clean up the last frame that was drawn before stopping
282 markDirtyRect(_vm->_screen->getSurface());
283
284 setPlaying(false);
285
286 // Reset the animation to the beginning
287 setCurrentFrame(0);
288 clearShift();
289
290 debugC(3, kDraciAnimationDebugLevel, "Stopping animation %d...", getID());
291 }
292
del()293 void Animation::del() {
294 _vm->_anims->deleteAnimation(this);
295 }
296
pauseAnimations()297 void AnimationManager::pauseAnimations() {
298 if (_animationPauseCounter++) {
299 // Already paused
300 return;
301 }
302
303 Common::List<Animation *>::iterator it;
304
305 for (it = _animations.begin(); it != _animations.end(); ++it) {
306 if ((*it)->getID() > 0 || (*it)->getID() == kTitleText) {
307 // Clean up the last frame that was drawn before stopping
308 (*it)->markDirtyRect(_vm->_screen->getSurface());
309
310 (*it)->setPaused(true);
311 }
312 }
313 }
314
unpauseAnimations()315 void AnimationManager::unpauseAnimations() {
316 if (--_animationPauseCounter) {
317 // Still paused
318 return;
319 }
320
321 Common::List<Animation *>::iterator it;
322
323 for (it = _animations.begin(); it != _animations.end(); ++it) {
324 if ((*it)->isPaused()) {
325 // Clean up the last frame that was drawn before stopping
326 (*it)->markDirtyRect(_vm->_screen->getSurface());
327
328 (*it)->setPaused(false);
329 }
330 }
331 }
332
getAnimation(int id)333 Animation *AnimationManager::getAnimation(int id) {
334 Common::List<Animation *>::iterator it;
335
336 for (it = _animations.begin(); it != _animations.end(); ++it) {
337 if ((*it)->getID() == id) {
338 return *it;
339 }
340 }
341
342 return NULL;
343 }
344
insert(Animation * anim,bool allocateIndex)345 void AnimationManager::insert(Animation *anim, bool allocateIndex) {
346 if (allocateIndex)
347 anim->setIndex(++_lastIndex);
348
349 Common::List<Animation *>::iterator it;
350
351 for (it = _animations.begin(); it != _animations.end(); ++it) {
352 if (anim->getZ() < (*it)->getZ())
353 break;
354 }
355
356 _animations.insert(it, anim);
357 }
358
drawScene(Surface * surf)359 void AnimationManager::drawScene(Surface *surf) {
360 // Fill the screen with color zero since some rooms may rely on the screen being black
361 _vm->_screen->getSurface()->fill(0);
362
363 sortAnimations();
364
365 Common::List<Animation *>::iterator it;
366
367 for (it = _animations.begin(); it != _animations.end(); ++it) {
368 if (! ((*it)->isPlaying()) ) {
369 continue;
370 }
371
372 (*it)->nextFrame(false);
373 (*it)->drawFrame(surf);
374 }
375 }
376
sortAnimations()377 void AnimationManager::sortAnimations() {
378 Common::List<Animation *>::iterator cur;
379 Common::List<Animation *>::iterator next;
380
381 cur = _animations.begin();
382
383 // If the list is empty, we're done
384 if (cur == _animations.end())
385 return;
386
387 bool hasChanged;
388
389 do {
390 hasChanged = false;
391 cur = _animations.begin();
392 next = cur;
393
394 while (true) {
395 next++;
396
397 // If we are at the last element, we're done
398 if (next == _animations.end())
399 break;
400
401 // If we find an animation out of order, reinsert it
402 if ((*next)->getZ() < (*cur)->getZ()) {
403
404 Animation *anim = *next;
405 next = _animations.reverse_erase(next);
406
407 insert(anim, false);
408 hasChanged = true;
409 }
410
411 // Advance to next animation
412 cur = next;
413 }
414 } while (hasChanged);
415 }
416
deleteAnimation(Animation * anim)417 void AnimationManager::deleteAnimation(Animation *anim) {
418 if (!anim) {
419 return;
420 }
421 Common::List<Animation *>::iterator it;
422
423 int index = -1;
424
425 // Iterate for the first time to delete the animation
426 for (it = _animations.begin(); it != _animations.end(); ++it) {
427 if (*it == anim) {
428 // Remember index of the deleted animation
429 index = (*it)->getIndex();
430
431 debugC(3, kDraciAnimationDebugLevel, "Deleting animation %d...", anim->getID());
432
433 delete *it;
434 _animations.erase(it);
435
436 break;
437 }
438 }
439
440 // Iterate the second time to decrease indexes greater than the deleted animation index
441 for (it = _animations.begin(); it != _animations.end(); ++it) {
442 if ((*it)->getIndex() > index && (*it)->getIndex() != kIgnoreIndex) {
443 (*it)->setIndex((*it)->getIndex() - 1);
444 }
445 }
446
447 // Decrement index of last animation
448 _lastIndex -= 1;
449 }
450
deleteOverlays()451 void AnimationManager::deleteOverlays() {
452 debugC(3, kDraciAnimationDebugLevel, "Deleting overlays...");
453
454 Common::List<Animation *>::iterator it;
455
456 for (it = _animations.begin(); it != _animations.end(); ++it) {
457 if ((*it)->getID() == kOverlayImage) {
458 delete *it;
459 it = _animations.reverse_erase(it);
460 }
461 }
462 }
463
deleteAll()464 void AnimationManager::deleteAll() {
465 debugC(3, kDraciAnimationDebugLevel, "Deleting all animations...");
466
467 Common::List<Animation *>::iterator it;
468
469 for (it = _animations.begin(); it != _animations.end(); ++it) {
470 delete *it;
471 }
472
473 _animations.clear();
474
475 _lastIndex = -1;
476 }
477
deleteAfterIndex(int index)478 void AnimationManager::deleteAfterIndex(int index) {
479 Common::List<Animation *>::iterator it;
480
481 for (it = _animations.begin(); it != _animations.end(); ++it) {
482 if ((*it)->getIndex() > index) {
483
484 debugC(3, kDraciAnimationDebugLevel, "Deleting animation %d...", (*it)->getID());
485
486 delete *it;
487 it = _animations.reverse_erase(it);
488 }
489 }
490
491 _lastIndex = index;
492 }
493
getTopAnimation(int x,int y) const494 const Animation *AnimationManager::getTopAnimation(int x, int y) const {
495 Common::List<Animation *>::const_iterator it;
496
497 Animation *retval = NULL;
498
499 // Get transparent color for the current screen
500 const int transparent = _vm->_screen->getSurface()->getTransparentColor();
501
502 for (it = _animations.reverse_begin(); it != _animations.end(); --it) {
503
504 Animation *anim = *it;
505
506 // If the animation is not playing, ignore it
507 if (!anim->isPlaying() || anim->isPaused()) {
508 continue;
509 }
510
511 const Drawable *frame = anim->getConstCurrentFrame();
512
513 if (frame == NULL) {
514 continue;
515 }
516
517 bool matches = false;
518 if (frame->getRect(anim->getCurrentFrameDisplacement()).contains(x, y)) {
519 if (frame->getType() == kDrawableText) {
520
521 matches = true;
522
523 } else if (frame->getType() == kDrawableSprite &&
524 reinterpret_cast<const Sprite *>(frame)->getPixel(x, y, anim->getCurrentFrameDisplacement()) != transparent) {
525
526 matches = true;
527 }
528 }
529
530 // Return the top-most animation object, unless it is a
531 // non-clickable sprite (overlay, debugging sprites for
532 // walking, or title/speech text) and there is an actual object
533 // underneath it.
534 if (matches) {
535 if (anim->getID() > kOverlayImage || anim->getID() < kSpeechText) {
536 return anim;
537 } else if (retval == NULL) {
538 retval = anim;
539 }
540 }
541 }
542
543 // The default return value if no animations were found on these coordinates (not even overlays)
544 return retval;
545 }
546
load(uint animNum)547 Animation *AnimationManager::load(uint animNum) {
548 // Make double-sure that an animation isn't loaded more than twice,
549 // otherwise horrible things happen in the AnimationManager, because
550 // they use a simple link-list without duplicate checking. This should
551 // never happen unless there is a bug in the game, because all GPL2
552 // commands are guarded.
553 assert(!getAnimation(animNum));
554
555 const BAFile *animFile = _vm->_animationsArchive->getFile(animNum);
556 Common::MemoryReadStream animationReader(animFile->_data, animFile->_length);
557
558 uint numFrames = animationReader.readByte();
559
560 // The following two flags are ignored by the played. Memory logic was
561 // a hint to the old player whether it should cache the sprites or load
562 // them on demand. We have 1 memory manager and ignore these hints.
563 animationReader.readByte();
564 // The disable erasing field is just a (poor) optimization flag that
565 // turns of drawing the background underneath the sprite. By reading
566 // the source code of the old player, I'm not sure if that would ever
567 // have worked. There are only 6 animations in the game with this flag
568 // true. All of them have just 1 animation phase and they are used to
569 // patch a part of the original background by a new sprite. This
570 // should work with the default logic as well---just play this
571 // animation on top of the background. Since the only meaning of the
572 // flag was optimization, ignoring should be OK even without dipping
573 // into details.
574 animationReader.readByte();
575 const bool cyclic = animationReader.readByte();
576 const bool relative = animationReader.readByte();
577
578 Animation *anim = new Animation(_vm, animNum, 0, false);
579 insert(anim, true);
580
581 anim->setLooping(cyclic);
582 anim->setIsRelative(relative);
583
584 for (uint i = 0; i < numFrames; ++i) {
585 uint spriteNum = animationReader.readUint16LE() - 1;
586 int x = animationReader.readSint16LE();
587 int y = animationReader.readSint16LE();
588 uint scaledWidth = animationReader.readUint16LE();
589 uint scaledHeight = animationReader.readUint16LE();
590 byte mirror = animationReader.readByte();
591 int sample = animationReader.readUint16LE() - 1;
592 uint freq = animationReader.readUint16LE();
593 uint delay = animationReader.readUint16LE();
594
595 // _spritesArchive is flushed when entering a room. All
596 // scripts in a room are responsible for loading their animations.
597 const BAFile *spriteFile = _vm->_spritesArchive->getFile(spriteNum);
598 Sprite *sp = new Sprite(spriteFile->_data, spriteFile->_length,
599 relative ? 0 : x, relative ? 0 : y, true);
600
601 // Some frames set the scaled dimensions to 0 even though other frames
602 // from the same animations have them set to normal values
603 // We work around this by assuming it means no scaling is necessary
604 if (scaledWidth == 0) {
605 scaledWidth = sp->getWidth();
606 }
607
608 if (scaledHeight == 0) {
609 scaledHeight = sp->getHeight();
610 }
611
612 sp->setScaled(scaledWidth, scaledHeight);
613
614 if (mirror)
615 sp->setMirrorOn();
616
617 sp->setDelay(delay * 10);
618
619 anim->addFrame(sp, _vm->_soundsArchive->getSample(sample, freq));
620 if (relative) {
621 anim->makeLastFrameRelative(x, y);
622 }
623 }
624
625 return anim;
626 }
627
628 } // End of namespace Draci
629