diff options
-rw-r--r-- | engines/chewy/resource.cpp | 44 | ||||
-rw-r--r-- | engines/chewy/resource.h | 7 |
2 files changed, 35 insertions, 16 deletions
diff --git a/engines/chewy/resource.cpp b/engines/chewy/resource.cpp index 1d8764a979..40db53abe6 100644 --- a/engines/chewy/resource.cpp +++ b/engines/chewy/resource.cpp @@ -20,8 +20,8 @@ * */ -#include "common/stream.h" #include "common/debug.h" +#include "common/stream.h" #include "common/textconsole.h" #include "chewy/chewy.h" @@ -41,9 +41,8 @@ Resource::Resource(Common::String filename) { for (uint i = 0; i < _chunkCount; i++) { TBFChunk cur; - cur.packedSize = _stream.readUint32LE(); + cur.packedSize = _stream.readUint32LE() - TBF_CHUNK_HEADER_SIZE; cur.type = _stream.readUint16LE(); - cur.pos = _stream.pos(); if (_stream.readUint32BE() != tbfID) error("Corrupt resource %s", filename.c_str()); @@ -52,15 +51,13 @@ Resource::Resource(Common::String filename) { cur.unpackedSize = _stream.readUint32LE(); cur.width = _stream.readUint16LE(); cur.height = _stream.readUint16LE(); - _stream.read(cur.palette, 3 * 256); - _stream.skip(cur.packedSize - TBF_CHUNK_HEADER_SIZE); + for (int j = 0; j < 3 * 256; j++) + cur.palette[j] = _stream.readByte() << 2; + cur.pos = _stream.pos(); - _tbfChunkList.push_back(cur); + _stream.skip(cur.packedSize); - /*debug("Chunk %d: packed %d, type %d, pos %d, mode %d, comp %d, unpacked %d, width %d, height %d", - i, cur.packedSize, cur.type, cur.pos, cur.screenMode, cur.compressionFlag, cur.unpackedSize, - cur.width, cur.height - );*/ + _tbfChunkList.push_back(cur); } } @@ -74,9 +71,30 @@ TBFChunk *Resource::getChunk(int num) { return &_tbfChunkList[num]; } -Common::SeekableReadStream *Resource::getChunkData(int num) { - // TODO - return nullptr; +byte *Resource::getChunkData(int num) { + TBFChunk *chunk = &_tbfChunkList[num]; + byte *data = new byte[chunk->unpackedSize]; + + _stream.seek(chunk->pos, SEEK_SET); + + if (!chunk->compressionFlag) { + _stream.read(data, chunk->packedSize); + } else { + // Compressed resources are packed using a very simple RLE compression + byte count; + byte value; + uint32 outPos = 0; + + for (uint i = 0; i < (chunk->packedSize) / 2 && outPos < chunk->unpackedSize; i++) { + count = _stream.readByte(); + value = _stream.readByte(); + for (byte j = 0; j < count; j++) { + data[outPos++] = value; + } + } + } + + return data; } } // End of namespace Chewy diff --git a/engines/chewy/resource.h b/engines/chewy/resource.h index 5432e3c160..622ffa4084 100644 --- a/engines/chewy/resource.h +++ b/engines/chewy/resource.h @@ -40,7 +40,6 @@ namespace Chewy { struct TBFChunk { uint32 packedSize; // includes header uint16 type; - uint32 pos; // extra field // TBF chunk header // ID (TBF, followed by a zero) @@ -49,7 +48,9 @@ struct TBFChunk { uint32 unpackedSize; uint16 width; uint16 height; - char palette[3 * 256]; + byte palette[3 * 256]; + + uint32 pos; // position of the actual data }; typedef Common::Array<TBFChunk> TBFChunkList; @@ -60,7 +61,7 @@ public: ~Resource(); TBFChunk *getChunk(int num); - Common::SeekableReadStream *getChunkData(int num); + byte *getChunkData(int num); private: Common::File _stream; |