scummvm/engines/prince/animation.cpp

184 lines
4.9 KiB
C++
Raw Normal View History

2013-12-05 00:02:31 +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.
*
*/
#include "prince/animation.h"
2014-04-02 01:35:07 +02:00
#include "prince/decompress.h"
2013-12-05 00:02:31 +00:00
2014-04-02 01:35:07 +02:00
#include "common/debug.h"
#include "common/endian.h"
2013-12-05 00:02:31 +00:00
namespace Prince {
bool Animation::loadFromStream(Common::SeekableReadStream &stream) {
2014-07-31 05:29:03 +02:00
_idXDiff = stream.readByte();
_idYDiff = stream.readByte();
_loopCount = stream.readUint16LE();
_phaseCount = stream.readUint16LE();
stream.skip(2); // skip _frameCount here
_baseX = stream.readUint16LE();
_baseY = stream.readUint16LE();
uint32 phaseTableOffset = stream.readUint32LE();
uint32 tableOfFrameOffsets = stream.pos();
stream.seek(phaseTableOffset);
Phase tempPhase;
_frameCount = 0;
for (int phase = 0; phase < _phaseCount; phase++) {
tempPhase._phaseOffsetX = stream.readSint16LE();
tempPhase._phaseOffsetY = stream.readSint16LE();
tempPhase._phaseToFrameIndex = stream.readUint16LE();
if (tempPhase._phaseToFrameIndex > _frameCount) {
_frameCount = tempPhase._phaseToFrameIndex;
}
_phaseList.push_back(tempPhase);
stream.skip(2);
}
if (_phaseCount) {
_frameCount++;
2013-12-05 00:02:31 +00:00
}
2014-07-31 05:29:03 +02:00
for (int frame = 0; frame < _frameCount; frame++) {
stream.seek(tableOfFrameOffsets + frame * 4);
uint32 frameInfoOffset = stream.readUint32LE();
stream.seek(frameInfoOffset);
uint16 frameWidth = stream.readUint16LE();
uint16 frameHeight = stream.readUint16LE();
uint32 frameDataPos = stream.pos();
uint32 frameDataOffset = stream.readUint32BE();
Graphics::Surface *surf = new Graphics::Surface();
surf->create(frameWidth, frameHeight, Graphics::PixelFormat::createFormatCLUT8());
if (frameDataOffset == MKTAG('m', 'a', 's', 'm')) {
// Compressed
Decompressor dec;
uint32 ddataSize = stream.readUint32LE();
byte *data = (byte *)malloc(ddataSize);
byte *ddata = (byte *)malloc(ddataSize);
stream.read(data, ddataSize);
dec.decompress(data, ddata, ddataSize);
for (uint16 i = 0; i < frameHeight; i++) {
memcpy(surf->getBasePtr(0, i), ddata + frameWidth * i, frameWidth);
}
free(ddata);
free(data);
} else {
stream.seek(frameDataPos);
// Uncompressed
for (uint16 i = 0; i < frameHeight; i++) {
stream.read(surf->getBasePtr(0, i), frameWidth);
}
}
_frameList.push_back(surf);
}
2014-04-02 01:35:07 +02:00
2014-07-31 05:29:03 +02:00
return true;
2014-04-02 01:35:07 +02:00
}
2014-07-31 05:29:03 +02:00
Animation::Animation() : _idXDiff(0), _idYDiff(0), _loopCount(0), _phaseCount(0), _frameCount(0), _baseX(0), _baseY(0)
{
2014-04-02 01:35:07 +02:00
}
Animation::~Animation() {
2014-07-31 05:29:03 +02:00
clear();
2014-04-02 01:35:07 +02:00
}
void Animation::clear() {
2014-07-31 05:29:03 +02:00
_phaseList.clear();
for (int i = 0; i < _frameCount; i++) {
_frameList[i]->free();
delete _frameList[i];
_frameList[i] = nullptr;
}
}
bool Animation::testId() const {
2014-07-31 05:29:03 +02:00
if (_idXDiff == 'A' && _idYDiff == 'N') {
return true;
}
return false;
}
int8 Animation::getIdXDiff() const {
2014-07-31 05:29:03 +02:00
return _idXDiff;
}
int8 Animation::getIdYDiff() const {
2014-07-31 05:29:03 +02:00
return _idYDiff;
}
2014-04-02 01:35:07 +02:00
int16 Animation::getLoopCount() const {
2014-07-31 05:29:03 +02:00
return _loopCount;
2014-04-02 01:35:07 +02:00
}
int32 Animation::getPhaseCount() const {
2014-07-31 05:29:03 +02:00
return _phaseCount;
2014-04-28 12:15:54 +02:00
}
int32 Animation::getFrameCount() const {
2014-07-31 05:29:03 +02:00
return _frameCount;
2014-04-28 12:15:54 +02:00
}
2014-04-02 01:35:07 +02:00
int16 Animation::getBaseX() const {
2014-07-31 05:29:03 +02:00
return _baseX;
2014-04-02 01:35:07 +02:00
}
int16 Animation::getBaseY() const {
2014-07-31 05:29:03 +02:00
return _baseY;
2014-04-02 01:35:07 +02:00
}
2014-07-31 05:29:03 +02:00
int16 Animation::getPhaseOffsetX(int phaseIndex) const {
if (phaseIndex < _phaseCount) {
return _phaseList[phaseIndex]._phaseOffsetX;
} else {
error("getPhaseOffsetX() phaseIndex: %d, phaseCount: %d", phaseIndex, _phaseCount);
}
2014-04-02 01:35:07 +02:00
}
2014-07-31 05:29:03 +02:00
int16 Animation::getPhaseOffsetY(int phaseIndex) const {
if (phaseIndex < _phaseCount) {
return _phaseList[phaseIndex]._phaseOffsetY;
} else {
error("getPhaseOffsetY() phaseIndex: %d, phaseCount: %d", phaseIndex, _phaseCount);
}
}
2014-07-31 05:29:03 +02:00
int16 Animation::getPhaseFrameIndex(int phaseIndex) const {
if (phaseIndex < _phaseCount) {
return _phaseList[phaseIndex]._phaseToFrameIndex;
} else {
error("getPhaseFrameIndex() phaseIndex: %d, phaseCount: %d", phaseIndex, _phaseCount);
}
}
2014-07-31 05:29:03 +02:00
Graphics::Surface *Animation::getFrame(int frameIndex) {
if (frameIndex < _frameCount) {
return _frameList[frameIndex];
2014-04-02 01:35:07 +02:00
} else {
2014-07-31 05:29:03 +02:00
error("getFrame() frameIndex: %d, frameCount: %d", frameIndex, _frameCount);
2014-04-02 01:35:07 +02:00
}
}
2013-12-05 00:02:31 +00:00
2014-07-31 05:29:03 +02:00
} // End of namespace Prince
2013-12-05 00:02:31 +00:00
/* vim: set tabstop=4 noexpandtab: */