2014-02-22 16:13:35 +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 "common/scummsys.h"
|
|
|
|
#include "mads/mads.h"
|
2014-02-22 17:17:37 +00:00
|
|
|
#include "mads/assets.h"
|
|
|
|
#include "mads/compression.h"
|
2014-02-22 16:13:35 +00:00
|
|
|
#include "mads/events.h"
|
2014-03-04 01:53:27 +00:00
|
|
|
#include "mads/palette.h"
|
2014-02-22 16:13:35 +00:00
|
|
|
|
|
|
|
namespace MADS {
|
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
SpriteAsset::SpriteAsset(MADSEngine *vm, const Common::String &resourceName, int flags) :
|
|
|
|
_vm(vm) {
|
2014-02-22 17:17:37 +00:00
|
|
|
Common::String resName = resourceName;
|
2014-04-11 21:23:36 +00:00
|
|
|
if (!resName.hasSuffix(".SS") && !resName.hasSuffix(".ss"))
|
2014-02-22 17:17:37 +00:00
|
|
|
resName += ".SS";
|
2014-03-04 01:53:27 +00:00
|
|
|
|
2014-02-22 17:17:37 +00:00
|
|
|
File file(resName);
|
2014-03-04 01:53:27 +00:00
|
|
|
load(&file, flags);
|
|
|
|
|
|
|
|
file.close();
|
|
|
|
}
|
2014-05-08 08:43:23 +00:00
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
SpriteAsset::SpriteAsset(MADSEngine *vm, Common::SeekableReadStream *stream, int flags) :
|
|
|
|
_vm(vm) {
|
|
|
|
load(stream, flags);
|
|
|
|
}
|
2014-02-22 17:17:37 +00:00
|
|
|
|
2014-04-12 20:14:57 +00:00
|
|
|
SpriteAsset::~SpriteAsset() {
|
|
|
|
if (_usageIndex)
|
|
|
|
_vm->_palette->_paletteUsage.resetPalFlags(_usageIndex);
|
2014-04-18 13:16:47 +00:00
|
|
|
|
|
|
|
for (uint i = 0; i < _frames.size(); ++i)
|
|
|
|
delete _frames[i]._frame;
|
|
|
|
|
|
|
|
delete _charInfo;
|
2014-04-12 20:14:57 +00:00
|
|
|
}
|
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
void SpriteAsset::load(Common::SeekableReadStream *stream, int flags) {
|
2014-02-22 17:17:37 +00:00
|
|
|
int curFrame = 0;
|
|
|
|
uint32 frameOffset = 0;
|
2014-03-04 01:53:27 +00:00
|
|
|
MadsPack sprite(stream);
|
2014-02-22 17:17:37 +00:00
|
|
|
_frameRate = 0;
|
|
|
|
_pixelSpeed = 0;
|
|
|
|
_maxWidth = 0;
|
|
|
|
_maxHeight = 0;
|
2014-03-10 05:00:11 +00:00
|
|
|
_usageIndex = -1;
|
2014-02-22 17:17:37 +00:00
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
Common::SeekableReadStream *spriteStream = sprite.getItemStream(0);
|
|
|
|
_mode = spriteStream->readByte();
|
|
|
|
spriteStream->skip(1);
|
|
|
|
int type1 = spriteStream->readUint16LE();
|
|
|
|
int type2 = spriteStream->readUint16LE();
|
|
|
|
_isBackground = (type1 != 0) && (type2 < 4);
|
|
|
|
spriteStream->skip(32);
|
2014-02-22 17:17:37 +00:00
|
|
|
_frameCount = spriteStream->readUint16LE();
|
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
if ((flags & SPRITE_SET_CHAR_INFO) == 0)
|
|
|
|
_charInfo = nullptr;
|
|
|
|
else
|
|
|
|
_charInfo = new SpriteSetCharInfo(spriteStream);
|
|
|
|
|
|
|
|
delete spriteStream;
|
|
|
|
|
2014-02-22 17:17:37 +00:00
|
|
|
// Get the palette data
|
2014-03-15 15:12:31 +00:00
|
|
|
Common::SeekableReadStream *palStream = sprite.getItemStream(2);
|
|
|
|
Common::Array<RGB6> palette;
|
2014-03-11 02:08:55 +00:00
|
|
|
|
2014-03-15 15:12:31 +00:00
|
|
|
int numColors = palStream->readUint16LE();
|
|
|
|
assert(numColors <= 252);
|
2014-03-22 02:47:31 +00:00
|
|
|
_colorCount = numColors;
|
2014-03-15 15:12:31 +00:00
|
|
|
|
|
|
|
// Load in the palette
|
|
|
|
palette.resize(numColors);
|
|
|
|
for (int i = 0; i < numColors; ++i)
|
|
|
|
palette[i].load(palStream);
|
2014-03-26 01:20:44 +00:00
|
|
|
delete palStream;
|
2014-03-15 15:12:31 +00:00
|
|
|
|
|
|
|
// Process the palette data
|
2014-03-26 01:20:44 +00:00
|
|
|
if (flags & 9) {
|
2014-03-27 00:35:18 +00:00
|
|
|
_usageIndex = 0;
|
|
|
|
|
|
|
|
if (flags & 8) {
|
|
|
|
int newPalCtr = 0;
|
|
|
|
|
|
|
|
for (uint i = 0; i < palette.size(); ++i) {
|
|
|
|
RGB6 &rgb = palette[i];
|
|
|
|
|
|
|
|
// Scan for existing rgb at beginning of the main palette
|
|
|
|
bool found = false;
|
|
|
|
for (int pIndex = 0; pIndex < 4 && !found; ++pIndex) {
|
|
|
|
byte *palP = &_vm->_palette->_mainPalette[pIndex * 3];
|
|
|
|
if (palP[0] == rgb.r && palP[1] == rgb.g && palP[2] == rgb.b) {
|
|
|
|
rgb._palIndex = pIndex;
|
|
|
|
found = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found) {
|
|
|
|
// Existing palette entry not found, so need to add it in
|
|
|
|
int palIndex = (0xF7F607 >> (8 * newPalCtr)) & 0xff;
|
|
|
|
byte *palP = &_vm->_palette->_mainPalette[palIndex * 3];
|
|
|
|
palP[0] = rgb.r;
|
|
|
|
palP[1] = rgb.g;
|
|
|
|
palP[2] = rgb.b;
|
|
|
|
rgb._palIndex = palIndex;
|
|
|
|
|
|
|
|
newPalCtr = MIN(newPalCtr + 1, 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-26 01:20:44 +00:00
|
|
|
} else {
|
|
|
|
_usageIndex = _vm->_palette->_paletteUsage.process(palette, flags);
|
|
|
|
assert(_usageIndex >= 0);
|
|
|
|
}
|
2014-02-22 17:17:37 +00:00
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
spriteStream = sprite.getItemStream(1);
|
|
|
|
Common::SeekableReadStream *spriteDataStream = sprite.getItemStream(3);
|
2014-02-22 17:17:37 +00:00
|
|
|
SpriteAssetFrame frame;
|
2014-03-04 01:53:27 +00:00
|
|
|
Common::Array<int> frameSizes;
|
2014-02-22 17:17:37 +00:00
|
|
|
for (curFrame = 0; curFrame < _frameCount; curFrame++) {
|
2014-03-04 01:53:27 +00:00
|
|
|
frame._stream = 0;
|
|
|
|
frame._comp = 0;
|
2014-02-22 17:17:37 +00:00
|
|
|
frameOffset = spriteStream->readUint32LE();
|
|
|
|
_frameOffsets.push_back(frameOffset);
|
2014-03-04 01:53:27 +00:00
|
|
|
uint32 frameSize = spriteStream->readUint32LE();
|
|
|
|
frameSizes.push_back(frameSize);
|
|
|
|
|
|
|
|
frame._bounds.left = spriteStream->readSint16LE();
|
|
|
|
frame._bounds.top = spriteStream->readSint16LE();
|
|
|
|
frame._bounds.setWidth(spriteStream->readUint16LE());
|
|
|
|
frame._bounds.setHeight(spriteStream->readUint16LE());
|
|
|
|
|
|
|
|
if (curFrame == 0)
|
2014-05-08 08:43:23 +00:00
|
|
|
debugC(1, kDebugGraphics, "%i frames, x = %i, y = %i, w = %i, h = %i\n",
|
|
|
|
_frameCount, frame._bounds.left, frame._bounds.top,
|
2014-03-04 01:53:27 +00:00
|
|
|
frame._bounds.width(), frame._bounds.height());
|
|
|
|
|
|
|
|
if (_mode == 0) {
|
|
|
|
// Create a frame and decompress the raw pixel data
|
|
|
|
uint32 currPos = (uint32)spriteDataStream->pos();
|
2014-03-15 15:12:31 +00:00
|
|
|
frame._frame = new MSprite(spriteDataStream, palette, frame._bounds);
|
2014-03-04 01:53:27 +00:00
|
|
|
assert((uint32)spriteDataStream->pos() == (currPos + frameSize));
|
2014-02-22 17:17:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_frames.push_back(frame);
|
|
|
|
}
|
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
if (_mode != 0) {
|
|
|
|
// Handle decompressing Fab encoded data
|
|
|
|
for (curFrame = 0; curFrame < _frameCount; curFrame++) {
|
|
|
|
FabDecompressor fab;
|
|
|
|
|
|
|
|
int srcSize = (curFrame == (_frameCount - 1)) ? spriteDataStream->size() - _frameOffsets[curFrame] :
|
|
|
|
_frameOffsets[curFrame + 1] - _frameOffsets[curFrame];
|
|
|
|
byte *srcData = new byte[srcSize];
|
|
|
|
assert(srcData);
|
|
|
|
spriteDataStream->read(srcData, srcSize);
|
|
|
|
|
|
|
|
byte *destData = new byte[frameSizes[curFrame]];
|
|
|
|
assert(destData);
|
|
|
|
|
|
|
|
fab.decompress(srcData, srcSize, destData, frameSizes[curFrame]);
|
|
|
|
|
2014-03-11 02:08:55 +00:00
|
|
|
// Load the frames
|
2014-03-04 01:53:27 +00:00
|
|
|
Common::MemoryReadStream *rs = new Common::MemoryReadStream(destData, frameSizes[curFrame]);
|
2014-03-15 15:12:31 +00:00
|
|
|
_frames[curFrame]._frame = new MSprite(rs, palette, _frames[curFrame]._bounds);
|
2014-03-04 01:53:27 +00:00
|
|
|
delete rs;
|
|
|
|
|
|
|
|
delete[] srcData;
|
|
|
|
delete[] destData;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
delete spriteStream;
|
2014-02-22 17:17:37 +00:00
|
|
|
delete spriteDataStream;
|
|
|
|
}
|
2014-02-22 16:13:35 +00:00
|
|
|
|
2014-03-04 01:53:27 +00:00
|
|
|
MSprite *SpriteAsset::getFrame(int frameIndex) {
|
|
|
|
if ((uint)frameIndex < _frames.size()) {
|
|
|
|
return _frames[frameIndex]._frame;
|
|
|
|
} else {
|
|
|
|
debugC(kDebugGraphics, "SpriteAsset::getFrame: Invalid frame %d, out of %d", frameIndex, _frames.size());
|
|
|
|
return _frames[_frames.size() - 1]._frame;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
SpriteSetCharInfo::SpriteSetCharInfo(Common::SeekableReadStream *s) {
|
|
|
|
_totalFrames = s->readByte();
|
|
|
|
s->skip(1);
|
|
|
|
_numEntries = s->readUint16LE();
|
|
|
|
|
|
|
|
for (int i = 0; i < 16; ++i)
|
2014-03-29 00:23:25 +00:00
|
|
|
_startFrames[i] = s->readUint16LE();
|
2014-03-04 01:53:27 +00:00
|
|
|
for (int i = 0; i < 16; ++i)
|
2014-03-29 00:23:25 +00:00
|
|
|
_stopFrames[i] = s->readUint16LE();
|
2014-03-04 01:53:27 +00:00
|
|
|
for (int i = 0; i < 16; ++i)
|
|
|
|
_ticksList[i] = s->readUint16LE();
|
|
|
|
|
2014-03-28 13:19:55 +00:00
|
|
|
_velocity = s->readUint16LE();
|
2014-03-04 01:53:27 +00:00
|
|
|
_ticksAmount = s->readByte();
|
2014-03-28 13:19:55 +00:00
|
|
|
_centerOfGravity = s->readByte();
|
2014-03-04 01:53:27 +00:00
|
|
|
}
|
|
|
|
|
2014-02-22 16:13:35 +00:00
|
|
|
} // End of namespace MADS
|