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;
|
|
|
|
_playing = false;
|
|
|
|
_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-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
|
|
|
|
|
|
|
// If there's only one or no frames, return
|
|
|
|
if (getFramesNum() < 2)
|
|
|
|
return;
|
|
|
|
|
|
|
|
Common::Rect frameRect = _frames[_currentFrame]->getRect();
|
2009-07-12 19:32:01 +00:00
|
|
|
Drawable *frame = _frames[_currentFrame];
|
2009-07-05 03:24:46 +00:00
|
|
|
|
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;
|
|
|
|
_playing = false;
|
|
|
|
} 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-05 03:24:46 +00:00
|
|
|
if (_frames.size() == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (_id == kOverlayImage) {
|
|
|
|
_frames[_currentFrame]->draw(surface, false);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_frames[_currentFrame]->draw(surface, true);
|
|
|
|
}
|
|
|
|
}
|
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-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-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
|
|
|
|
|
|
|
for (uint i = 0; i < getFramesNum(); ++i) {
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (anim)
|
|
|
|
anim->setPlaying(true);
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (anim)
|
|
|
|
anim->setPlaying(false);
|
|
|
|
|
|
|
|
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-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-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-03 17:50:22 +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) {
|
|
|
|
if((*it)->getID() == kOverlayImage)
|
|
|
|
(*it)->deleteFrames();
|
|
|
|
|
|
|
|
_animations.erase(it);
|
|
|
|
}
|
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
|
|
|
}
|