2011-09-03 10:41:06 +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.
|
2014-02-18 01:34:20 +00:00
|
|
|
*
|
2011-09-03 10:41:06 +00:00
|
|
|
* 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.
|
2014-02-18 01:34:20 +00:00
|
|
|
*
|
2011-09-03 10:41:06 +00:00
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "gob/surface.h"
|
|
|
|
#include "gob/anifile.h"
|
2012-03-11 19:41:57 +00:00
|
|
|
#include "gob/cmpfile.h"
|
2011-09-03 10:41:06 +00:00
|
|
|
#include "gob/aniobject.h"
|
|
|
|
|
|
|
|
namespace Gob {
|
|
|
|
|
2012-03-11 19:41:57 +00:00
|
|
|
ANIObject::ANIObject(const ANIFile &ani) : _ani(&ani), _cmp(0),
|
2012-07-06 04:50:04 +00:00
|
|
|
_visible(false), _paused(false), _mode(kModeContinuous), _x(0), _y(0) {
|
2012-03-11 19:41:57 +00:00
|
|
|
|
|
|
|
setAnimation(0);
|
|
|
|
setPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
ANIObject::ANIObject(const CMPFile &cmp) : _ani(0), _cmp(&cmp),
|
2012-07-06 04:50:04 +00:00
|
|
|
_visible(false), _paused(false), _mode(kModeContinuous), _x(0), _y(0) {
|
2011-09-03 10:41:06 +00:00
|
|
|
|
|
|
|
setAnimation(0);
|
|
|
|
setPosition();
|
|
|
|
}
|
|
|
|
|
|
|
|
ANIObject::~ANIObject() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANIObject::setVisible(bool visible) {
|
|
|
|
_visible = visible;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ANIObject::isVisible() const {
|
|
|
|
return _visible;
|
|
|
|
}
|
|
|
|
|
2011-09-03 17:51:03 +00:00
|
|
|
void ANIObject::setPause(bool pause) {
|
|
|
|
_paused = pause;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ANIObject::isPaused() const {
|
|
|
|
return _paused;
|
|
|
|
}
|
|
|
|
|
2011-09-12 15:11:31 +00:00
|
|
|
void ANIObject::setMode(Mode mode) {
|
|
|
|
_mode = mode;
|
|
|
|
}
|
|
|
|
|
2011-09-03 10:41:06 +00:00
|
|
|
void ANIObject::setAnimation(uint16 animation) {
|
|
|
|
_animation = animation;
|
|
|
|
_frame = 0;
|
|
|
|
}
|
|
|
|
|
2011-09-12 16:00:11 +00:00
|
|
|
void ANIObject::rewind() {
|
|
|
|
_frame = 0;
|
|
|
|
}
|
|
|
|
|
2012-06-07 02:20:41 +00:00
|
|
|
void ANIObject::setFrame(uint16 frame) {
|
|
|
|
_frame = frame % _ani->getAnimationInfo(_animation).frameCount;
|
|
|
|
}
|
|
|
|
|
2011-09-03 10:41:06 +00:00
|
|
|
void ANIObject::setPosition() {
|
2012-03-11 19:41:57 +00:00
|
|
|
// CMP "animations" have no default position
|
|
|
|
if (_cmp)
|
|
|
|
return;
|
|
|
|
|
2011-09-03 10:41:06 +00:00
|
|
|
if (_animation >= _ani->getAnimationCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ANIFile::Animation &animation = _ani->getAnimationInfo(_animation);
|
|
|
|
|
|
|
|
_x = animation.x;
|
|
|
|
_y = animation.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANIObject::setPosition(int16 x, int16 y) {
|
|
|
|
_x = x;
|
|
|
|
_y = y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ANIObject::getPosition(int16 &x, int16 &y) const {
|
|
|
|
x = _x;
|
|
|
|
y = _y;
|
|
|
|
}
|
|
|
|
|
2012-07-05 04:11:46 +00:00
|
|
|
void ANIObject::getFramePosition(int16 &x, int16 &y, uint16 n) const {
|
2012-03-11 19:41:57 +00:00
|
|
|
// CMP "animations" have no specific frame positions
|
|
|
|
if (_cmp) {
|
|
|
|
getPosition(x, y);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-03 10:41:06 +00:00
|
|
|
if (_animation >= _ani->getAnimationCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ANIFile::Animation &animation = _ani->getAnimationInfo(_animation);
|
|
|
|
if (_frame >= animation.frameCount)
|
|
|
|
return;
|
|
|
|
|
2012-07-05 04:11:46 +00:00
|
|
|
// If we're paused, we don't advance any frames
|
|
|
|
if (_paused)
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
// Number of cycles run through after n frames
|
|
|
|
uint16 cycles = (_frame + n) / animation.frameCount;
|
|
|
|
// Frame position after n frames
|
|
|
|
uint16 frame = (_frame + n) % animation.frameCount;
|
|
|
|
|
|
|
|
// Only doing one cycle?
|
|
|
|
if (_mode == kModeOnce)
|
|
|
|
cycles = MAX<uint16>(cycles, 1);
|
|
|
|
|
|
|
|
x = _x + animation.frameAreas[frame].left + cycles * animation.deltaX;
|
|
|
|
y = _y + animation.frameAreas[frame].top + cycles * animation.deltaY;
|
2011-09-03 10:41:06 +00:00
|
|
|
}
|
|
|
|
|
2012-07-05 04:11:46 +00:00
|
|
|
void ANIObject::getFrameSize(int16 &width, int16 &height, uint16 n) const {
|
2012-03-11 19:41:57 +00:00
|
|
|
if (_cmp) {
|
|
|
|
width = _cmp->getWidth (_animation);
|
|
|
|
height = _cmp->getHeight(_animation);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-09-03 10:41:06 +00:00
|
|
|
if (_animation >= _ani->getAnimationCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ANIFile::Animation &animation = _ani->getAnimationInfo(_animation);
|
|
|
|
if (_frame >= animation.frameCount)
|
|
|
|
return;
|
|
|
|
|
2012-07-05 04:11:46 +00:00
|
|
|
// If we're paused, we don't advance any frames
|
|
|
|
if (_paused)
|
|
|
|
n = 0;
|
|
|
|
|
|
|
|
// Frame position after n frames
|
|
|
|
uint16 frame = (_frame + n) % animation.frameCount;
|
|
|
|
|
|
|
|
width = animation.frameAreas[frame].right - animation.frameAreas[frame].left + 1;
|
|
|
|
height = animation.frameAreas[frame].bottom - animation.frameAreas[frame].top + 1;
|
2011-09-03 10:41:06 +00:00
|
|
|
}
|
|
|
|
|
2012-01-28 18:45:02 +00:00
|
|
|
bool ANIObject::isIn(int16 x, int16 y) const {
|
|
|
|
if (!isVisible())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int16 frameX, frameY, frameWidth, frameHeight;
|
|
|
|
getFramePosition(frameX, frameY);
|
|
|
|
getFrameSize(frameWidth, frameHeight);
|
|
|
|
|
|
|
|
if ((x < frameX) || (y < frameY))
|
|
|
|
return false;
|
|
|
|
if ((x > (frameX + frameWidth)) || (y > (frameY + frameHeight)))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ANIObject::isIn(const ANIObject &obj) const {
|
|
|
|
if (!isVisible() || !obj.isVisible())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
int16 frameX, frameY, frameWidth, frameHeight;
|
|
|
|
getFramePosition(frameX, frameY);
|
|
|
|
getFrameSize(frameWidth, frameHeight);
|
|
|
|
|
|
|
|
return obj.isIn(frameX , frameY ) ||
|
|
|
|
obj.isIn(frameX + frameWidth - 1, frameY ) ||
|
|
|
|
obj.isIn(frameX , frameY + frameHeight - 1) ||
|
|
|
|
obj.isIn(frameX + frameWidth - 1, frameY + frameHeight - 1);
|
|
|
|
}
|
|
|
|
|
2012-06-03 16:58:03 +00:00
|
|
|
bool ANIObject::draw(Surface &dest, int16 &left, int16 &top,
|
2011-09-03 10:41:06 +00:00
|
|
|
int16 &right, int16 &bottom) {
|
|
|
|
|
|
|
|
if (!_visible)
|
2012-06-03 16:58:03 +00:00
|
|
|
return false;
|
2011-09-03 10:41:06 +00:00
|
|
|
|
2012-03-11 19:41:57 +00:00
|
|
|
if (_cmp)
|
2012-06-03 16:58:03 +00:00
|
|
|
return drawCMP(dest, left, top, right, bottom);
|
2012-03-11 19:41:57 +00:00
|
|
|
else if (_ani)
|
2012-06-03 16:58:03 +00:00
|
|
|
return drawANI(dest, left, top, right, bottom);
|
|
|
|
|
|
|
|
return false;
|
2012-03-11 19:41:57 +00:00
|
|
|
}
|
|
|
|
|
2012-06-03 16:58:03 +00:00
|
|
|
bool ANIObject::drawCMP(Surface &dest, int16 &left, int16 &top,
|
2012-03-11 19:41:57 +00:00
|
|
|
int16 &right, int16 &bottom) {
|
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
if (!hasBuffer()) {
|
2012-03-11 19:41:57 +00:00
|
|
|
uint16 width, height;
|
|
|
|
|
|
|
|
_cmp->getMaxSize(width, height);
|
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
resizeBuffer(width, height);
|
2012-03-11 19:41:57 +00:00
|
|
|
}
|
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
left = _x;
|
|
|
|
top = _y;
|
|
|
|
right = _x + _cmp->getWidth (_animation) - 1;
|
|
|
|
bottom = _y + _cmp->getHeight(_animation) - 1;
|
2012-03-11 19:41:57 +00:00
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
if (!saveScreen(dest, left, top, right, bottom))
|
|
|
|
return false;
|
2012-03-11 19:41:57 +00:00
|
|
|
|
|
|
|
_cmp->draw(dest, _animation, _x, _y, 0);
|
|
|
|
|
2012-06-03 16:58:03 +00:00
|
|
|
return true;
|
2012-03-11 19:41:57 +00:00
|
|
|
}
|
|
|
|
|
2012-06-03 16:58:03 +00:00
|
|
|
bool ANIObject::drawANI(Surface &dest, int16 &left, int16 &top,
|
2012-03-11 19:41:57 +00:00
|
|
|
int16 &right, int16 &bottom) {
|
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
if (!hasBuffer()) {
|
2011-09-03 10:41:06 +00:00
|
|
|
uint16 width, height;
|
|
|
|
|
|
|
|
_ani->getMaxSize(width, height);
|
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
resizeBuffer(width, height);
|
2011-09-03 10:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const ANIFile::Animation &animation = _ani->getAnimationInfo(_animation);
|
|
|
|
if (_frame >= animation.frameCount)
|
2012-06-03 16:58:03 +00:00
|
|
|
return false;
|
2011-09-03 10:41:06 +00:00
|
|
|
|
|
|
|
const ANIFile::FrameArea &area = animation.frameAreas[_frame];
|
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
left = _x + area.left;
|
|
|
|
top = _y + area.top;
|
|
|
|
right = _x + area.right;
|
|
|
|
bottom = _y + area.bottom;
|
2011-09-03 10:41:06 +00:00
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
if (!saveScreen(dest, left, top, right, bottom))
|
|
|
|
return false;
|
2011-09-03 10:41:06 +00:00
|
|
|
|
|
|
|
_ani->draw(dest, _animation, _frame, _x, _y);
|
|
|
|
|
2012-06-03 16:58:03 +00:00
|
|
|
return true;
|
2011-09-03 10:41:06 +00:00
|
|
|
}
|
|
|
|
|
2012-06-03 16:58:03 +00:00
|
|
|
bool ANIObject::clear(Surface &dest, int16 &left, int16 &top,
|
2011-09-03 10:41:06 +00:00
|
|
|
int16 &right, int16 &bottom) {
|
|
|
|
|
2012-07-06 04:50:04 +00:00
|
|
|
return restoreScreen(dest, left, top, right, bottom);
|
2011-09-03 10:41:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ANIObject::advance() {
|
2011-09-03 17:51:03 +00:00
|
|
|
if (_paused)
|
|
|
|
return;
|
|
|
|
|
2012-03-11 19:41:57 +00:00
|
|
|
// CMP "animations" have only one frame
|
|
|
|
if (_cmp)
|
|
|
|
return;
|
|
|
|
|
2011-09-03 10:41:06 +00:00
|
|
|
if (_animation >= _ani->getAnimationCount())
|
|
|
|
return;
|
|
|
|
|
|
|
|
const ANIFile::Animation &animation = _ani->getAnimationInfo(_animation);
|
|
|
|
|
|
|
|
_frame = (_frame + 1) % animation.frameCount;
|
|
|
|
|
|
|
|
if (_frame == 0) {
|
|
|
|
_x += animation.deltaX;
|
|
|
|
_y += animation.deltaY;
|
2011-09-12 15:11:31 +00:00
|
|
|
|
|
|
|
if (_mode == kModeOnce) {
|
|
|
|
_paused = true;
|
|
|
|
_visible = false;
|
|
|
|
}
|
2011-09-03 10:41:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 ANIObject::getAnimation() const {
|
|
|
|
return _animation;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint16 ANIObject::getFrame() const {
|
|
|
|
return _frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ANIObject::lastFrame() const {
|
2012-03-11 19:41:57 +00:00
|
|
|
// CMP "animations" have only one frame
|
|
|
|
if (_cmp)
|
|
|
|
return true;
|
|
|
|
|
2011-09-03 10:41:06 +00:00
|
|
|
if (_animation >= _ani->getAnimationCount())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const ANIFile::Animation &animation = _ani->getAnimationInfo(_animation);
|
|
|
|
|
|
|
|
return (_frame + 1) >= animation.frameCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // End of namespace Gob
|