2009-07-01 16:20:47 +00:00
|
|
|
/* 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$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-07-03 17:50:22 +00:00
|
|
|
#include "draci/draci.h"
|
|
|
|
#include "draci/animation.h"
|
|
|
|
|
2009-07-01 16:20:47 +00:00
|
|
|
namespace Draci {
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Animation::Animation(DraciEngine *vm) : _vm(vm) {
|
2009-07-05 03:24:46 +00:00
|
|
|
_id = kUnused;
|
|
|
|
_z = 0;
|
2009-07-18 02:53:37 +00:00
|
|
|
_relX = 0;
|
|
|
|
_relY = 0;
|
2009-07-18 02:58:11 +00:00
|
|
|
setPlaying(false);
|
2009-07-05 03:24:46 +00:00
|
|
|
_looping = false;
|
|
|
|
_tick = _vm->_system->getMillis();
|
|
|
|
_currentFrame = 0;
|
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Animation::~Animation() {
|
2009-07-05 03:24:46 +00:00
|
|
|
deleteFrames();
|
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
bool Animation::isLooping() {
|
2009-07-05 03:24:46 +00:00
|
|
|
return _looping;
|
|
|
|
}
|
|
|
|
|
2009-07-18 02:53:37 +00:00
|
|
|
void Animation::setRelative(int relx, int rely) {
|
|
|
|
|
|
|
|
// Delete the previous frame
|
|
|
|
Common::Rect frameRect = _frames[_currentFrame]->getRect();
|
|
|
|
frameRect.translate(_relX, _relY);
|
|
|
|
_vm->_screen->getSurface()->markDirtyRect(frameRect);
|
|
|
|
|
|
|
|
_relX = relx;
|
|
|
|
_relY = rely;
|
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::setLooping(bool looping) {
|
2009-07-05 03:24:46 +00:00
|
|
|
_looping = looping;
|
2009-07-07 21:11:36 +00:00
|
|
|
debugC(7, kDraciAnimationDebugLevel, "Setting looping to %d on animation %d",
|
|
|
|
looping, _id);
|
2009-07-05 03:24:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::nextFrame(bool force) {
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-14 18:11:33 +00:00
|
|
|
// If there's only one or no frames, or if the animation is not playing, return
|
|
|
|
if (getFramesNum() < 2 || !_playing)
|
2009-07-05 03:24:46 +00:00
|
|
|
return;
|
|
|
|
|
2009-07-12 19:32:01 +00:00
|
|
|
Drawable *frame = _frames[_currentFrame];
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-18 02:53:37 +00:00
|
|
|
Common::Rect frameRect = frame->getRect();
|
|
|
|
|
|
|
|
// Translate rectangle to compensate for relative coordinates
|
|
|
|
frameRect.translate(_relX, _relY);
|
|
|
|
|
2009-07-12 19:32:01 +00:00
|
|
|
if (force || (_tick + frame->getDelay() <= _vm->_system->getMillis())) {
|
2009-07-07 21:11:36 +00:00
|
|
|
// If we are at the last frame and not looping, stop the animation
|
|
|
|
// The animation is also restarted to frame zero
|
|
|
|
if ((_currentFrame == getFramesNum() - 1) && !_looping) {
|
|
|
|
_currentFrame = 0;
|
2009-07-18 02:58:11 +00:00
|
|
|
setPlaying(false);
|
2009-07-07 21:11:36 +00:00
|
|
|
} else {
|
|
|
|
_vm->_screen->getSurface()->markDirtyRect(frameRect);
|
|
|
|
_currentFrame = nextFrameNum();
|
2009-07-12 19:32:01 +00:00
|
|
|
_tick += frame->getDelay();
|
2009-07-07 21:11:36 +00:00
|
|
|
}
|
2009-07-05 03:24:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
debugC(6, kDraciAnimationDebugLevel,
|
2009-07-07 21:11:36 +00:00
|
|
|
"anim=%d tick=%d delay=%d tick+delay=%d currenttime=%d frame=%d framenum=%d",
|
2009-07-12 19:32:01 +00:00
|
|
|
_id, _tick, frame->getDelay(), _tick + frame->getDelay(), _vm->_system->getMillis(),
|
|
|
|
_currentFrame, _frames.size());
|
2009-07-05 03:24:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
uint Animation::nextFrameNum() {
|
2009-07-05 03:24:46 +00:00
|
|
|
|
|
|
|
if ((_currentFrame == getFramesNum() - 1) && _looping)
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return _currentFrame + 1;
|
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::drawFrame(Surface *surface) {
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-14 18:11:33 +00:00
|
|
|
// If there are no frames or the animation is not playing, return
|
|
|
|
if (_frames.size() == 0 || !_playing)
|
2009-07-05 03:24:46 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (_id == kOverlayImage) {
|
|
|
|
_frames[_currentFrame]->draw(surface, false);
|
|
|
|
}
|
|
|
|
else {
|
2009-07-18 02:53:37 +00:00
|
|
|
Drawable *ptr = _frames[_currentFrame];
|
|
|
|
int x = ptr->getX();
|
|
|
|
int y = ptr->getY();
|
|
|
|
int newX = x + _relX;
|
|
|
|
int newY = y + _relY;
|
|
|
|
|
|
|
|
ptr->setX(newX);
|
|
|
|
ptr->setY(newY);
|
|
|
|
ptr->draw(surface, true);
|
|
|
|
|
|
|
|
ptr->setX(x);
|
|
|
|
ptr->setY(y);
|
2009-07-05 03:24:46 +00:00
|
|
|
}
|
|
|
|
}
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::setID(int id) {
|
2009-07-05 03:24:46 +00:00
|
|
|
_id = id;
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
int Animation::getID() {
|
2009-07-05 03:24:46 +00:00
|
|
|
return _id;
|
|
|
|
}
|
2009-07-03 19:05:58 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::setZ(uint z) {
|
2009-07-05 03:24:46 +00:00
|
|
|
_z = z;
|
2009-07-03 19:05:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
uint Animation::getZ() {
|
2009-07-05 03:24:46 +00:00
|
|
|
return _z;
|
|
|
|
}
|
2009-07-03 19:05:58 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
bool Animation::isPlaying() {
|
2009-07-05 03:24:46 +00:00
|
|
|
return _playing;
|
2009-07-03 19:05:58 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::setPlaying(bool playing) {
|
2009-07-18 02:58:11 +00:00
|
|
|
_tick = _vm->_system->getMillis();
|
2009-07-05 03:24:46 +00:00
|
|
|
_playing = playing;
|
|
|
|
}
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::addFrame(Drawable *frame) {
|
2009-07-05 03:24:46 +00:00
|
|
|
_frames.push_back(frame);
|
|
|
|
}
|
|
|
|
|
2009-07-18 02:53:37 +00:00
|
|
|
Drawable *Animation::getFrame(int frameNum) {
|
|
|
|
|
|
|
|
// If no argument is passed, return the current frame
|
|
|
|
if (frameNum == kCurrentFrame) {
|
|
|
|
return _frames[_currentFrame];
|
|
|
|
} else {
|
|
|
|
return _frames[frameNum];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
uint Animation::getFramesNum() {
|
2009-07-05 03:24:46 +00:00
|
|
|
return _frames.size();
|
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void Animation::deleteFrames() {
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-18 02:53:37 +00:00
|
|
|
for (int i = getFramesNum() - 1; i >= 0; --i) {
|
2009-07-05 03:24:46 +00:00
|
|
|
delete _frames[i];
|
|
|
|
_frames.pop_back();
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
2009-07-05 03:24:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Animation *AnimationManager::addAnimation(int id, uint z, bool playing) {
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Animation *anim = new Animation(_vm);
|
|
|
|
|
|
|
|
anim->setID(id);
|
|
|
|
anim->setZ(z);
|
|
|
|
anim->setPlaying(playing);
|
|
|
|
anim->setLooping(false);
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
insertAnimation(anim);
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
return anim;
|
2009-07-05 03:24:46 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void AnimationManager::play(int id) {
|
2009-07-07 21:11:36 +00:00
|
|
|
Animation *anim = getAnimation(id);
|
|
|
|
|
2009-07-14 18:11:33 +00:00
|
|
|
if (anim) {
|
2009-07-07 21:11:36 +00:00
|
|
|
anim->setPlaying(true);
|
|
|
|
|
2009-07-14 18:11:33 +00:00
|
|
|
debugC(5, kDraciAnimationDebugLevel, "Playing animation %d...", id);
|
|
|
|
}
|
2009-07-05 03:24:46 +00:00
|
|
|
}
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void AnimationManager::stop(int id) {
|
2009-07-07 21:11:36 +00:00
|
|
|
Animation *anim = getAnimation(id);
|
|
|
|
|
2009-07-18 02:53:37 +00:00
|
|
|
// Clean up the last frame that was drawn before stopping
|
|
|
|
Common::Rect frameRect = anim->getFrame()->getRect();
|
|
|
|
frameRect.translate(anim->_relX, anim->_relY);
|
|
|
|
_vm->_screen->getSurface()->markDirtyRect(frameRect);
|
|
|
|
|
2009-07-14 18:11:33 +00:00
|
|
|
if (anim) {
|
2009-07-07 21:11:36 +00:00
|
|
|
anim->setPlaying(false);
|
2009-07-14 18:11:33 +00:00
|
|
|
|
|
|
|
debugC(5, kDraciAnimationDebugLevel, "Stopping animation %d...", id);
|
|
|
|
}
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Animation *AnimationManager::getAnimation(int id) {
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Common::List<Animation *>::iterator it;
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
for (it = _animations.begin(); it != _animations.end(); ++it) {
|
2009-07-05 03:24:46 +00:00
|
|
|
if ((*it)->getID() == id) {
|
|
|
|
return *it;
|
|
|
|
}
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-07 15:39:18 +00:00
|
|
|
return NULL;
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void AnimationManager::insertAnimation(Animation *animObj) {
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Common::List<Animation *>::iterator it;
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
for (it = _animations.begin(); it != _animations.end(); ++it) {
|
2009-07-05 03:24:46 +00:00
|
|
|
if (animObj->getZ() < (*it)->getZ())
|
|
|
|
break;
|
|
|
|
}
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
_animations.insert(it, animObj);
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void AnimationManager::addOverlay(Drawable *overlay, uint z) {
|
|
|
|
Animation *anim = new Animation(_vm);
|
|
|
|
anim->setID(kOverlayImage);
|
|
|
|
anim->setZ(z);
|
|
|
|
anim->setPlaying(true);
|
|
|
|
anim->addFrame(overlay);
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
insertAnimation(anim);
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void AnimationManager::drawScene(Surface *surf) {
|
2009-07-13 19:11:24 +00:00
|
|
|
|
|
|
|
// Fill the screen with colour zero since some rooms may rely on the screen being black
|
|
|
|
_vm->_screen->getSurface()->fill(0);
|
|
|
|
|
2009-07-18 02:53:37 +00:00
|
|
|
sortAnimations();
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Common::List<Animation *>::iterator it;
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
for (it = _animations.begin(); it != _animations.end(); ++it) {
|
2009-07-05 03:24:46 +00:00
|
|
|
if (! ((*it)->isPlaying()) ) {
|
2009-07-03 19:05:58 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-07-05 03:24:46 +00:00
|
|
|
(*it)->nextFrame();
|
|
|
|
(*it)->drawFrame(surf);
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-18 02:53:37 +00:00
|
|
|
void AnimationManager::sortAnimations() {
|
|
|
|
Common::List<Animation *>::iterator cur;
|
|
|
|
Common::List<Animation *>::iterator next;
|
|
|
|
|
|
|
|
cur = _animations.begin();
|
|
|
|
|
|
|
|
// If the list is empty, we're done
|
|
|
|
if (cur == _animations.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
next = cur;
|
|
|
|
next++;
|
|
|
|
|
|
|
|
// If we are at the last element, we're done
|
|
|
|
if (next == _animations.end())
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If we find an animation out of order, reinsert it
|
|
|
|
if ((*next)->getZ() < (*cur)->getZ()) {
|
|
|
|
|
|
|
|
Animation *anim = *cur;
|
|
|
|
_animations.erase(cur);
|
|
|
|
|
|
|
|
insertAnimation(anim);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advance to next animation
|
|
|
|
cur = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void AnimationManager::deleteAnimation(int id) {
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Common::List<Animation *>::iterator it;
|
2009-07-18 02:53:37 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
for (it = _animations.begin(); it != _animations.end(); ++it) {
|
2009-07-07 14:52:36 +00:00
|
|
|
if ((*it)->getID() == id) {
|
|
|
|
(*it)->deleteFrames();
|
|
|
|
_animations.erase(it);
|
2009-07-05 03:24:46 +00:00
|
|
|
break;
|
2009-07-07 14:52:36 +00:00
|
|
|
}
|
2009-07-03 18:19:51 +00:00
|
|
|
}
|
2009-07-07 14:52:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AnimationManager::deleteOverlays() {
|
2009-07-05 03:24:46 +00:00
|
|
|
|
2009-07-07 14:52:36 +00:00
|
|
|
Common::List<Animation *>::iterator it;
|
2009-07-03 18:19:51 +00:00
|
|
|
|
2009-07-07 14:52:36 +00:00
|
|
|
for (it = _animations.begin(); it != _animations.end(); ++it) {
|
2009-07-18 02:53:37 +00:00
|
|
|
if ((*it)->getID() == kOverlayImage) {
|
2009-07-07 14:52:36 +00:00
|
|
|
(*it)->deleteFrames();
|
2009-07-18 02:53:37 +00:00
|
|
|
_animations.erase(it);
|
|
|
|
}
|
2009-07-07 14:52:36 +00:00
|
|
|
}
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|
2009-07-07 14:52:36 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
void AnimationManager::deleteAll() {
|
2009-07-03 17:50:22 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
Common::List<Animation *>::iterator it;
|
2009-07-03 18:19:51 +00:00
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
for (it = _animations.begin(); it != _animations.end(); ++it) {
|
2009-07-05 03:24:46 +00:00
|
|
|
(*it)->deleteFrames();
|
2009-07-03 18:19:51 +00:00
|
|
|
}
|
|
|
|
|
2009-07-05 11:52:17 +00:00
|
|
|
_animations.clear();
|
2009-07-01 16:20:47 +00:00
|
|
|
}
|
|
|
|
|
2009-07-03 17:50:22 +00:00
|
|
|
}
|