From b1fc7852253e372c931d674cd7e2c4cb56bfd5b6 Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Fri, 18 Dec 2015 03:11:29 +0200 Subject: LAB: Use templates for the undiff functions --- engines/lab/anim.cpp | 2 +- engines/lab/utils.cpp | 148 ++++++++++++-------------------------------------- engines/lab/utils.h | 14 ++--- 3 files changed, 43 insertions(+), 121 deletions(-) (limited to 'engines/lab') diff --git a/engines/lab/anim.cpp b/engines/lab/anim.cpp index 648634bc8f..4b060965e2 100644 --- a/engines/lab/anim.cpp +++ b/engines/lab/anim.cpp @@ -160,7 +160,7 @@ void Anim::diffNextFrame(bool onlyDiffData) { case 12: curPos = _diffFile->pos(); _diffFile->skip(4); - _vm->_utils->VRunLengthDecode(DrawBitMap->_planes[_curBit], _diffFile, DrawBitMap->_bytesPerRow); + _vm->_utils->verticalRunLengthDecode(DrawBitMap->_planes[_curBit], _diffFile, DrawBitMap->_bytesPerRow); _curBit++; _diffFile->seek(curPos + _size, SEEK_SET); break; diff --git a/engines/lab/utils.cpp b/engines/lab/utils.cpp index 71fce8154b..1ddb454895 100644 --- a/engines/lab/utils.cpp +++ b/engines/lab/utils.cpp @@ -121,32 +121,12 @@ Common::Point Utils::vgaUnscale(Common::Point pos) { /** * Undiffs a piece of memory when header size is a byte, and copy/skip size - * is also a byte. + * is also a byte or a word. */ -void Utils::unDiffByteByte(byte *dest, Common::File *sourceFile) { - while (1) { - uint16 skip = sourceFile->readByte(); - uint16 copy = sourceFile->readByte(); - - if (skip == 255) { - if (copy == 0) { - skip = sourceFile->readUint16LE(); - copy = sourceFile->readUint16LE(); - } else if (copy == 255) - return; - } +template +void Utils::unDiff(T *dest, Common::File *sourceFile) { + byte bytesPerWord = sizeof(T); - dest += skip; - sourceFile->read(dest, copy); - dest += copy; - } -} - -/** - * Undiffs a piece of memory when header size is a byte, and copy/skip size - * is a word. - */ -void Utils::unDiffByteWord(uint16 *dest, Common::File *sourceFile) { while (1) { uint16 skip = sourceFile->readByte(); uint16 copy = sourceFile->readByte(); @@ -161,18 +141,15 @@ void Utils::unDiffByteWord(uint16 *dest, Common::File *sourceFile) { dest += skip; - while (copy > 3) { - for (int i = 0; i < 4; i++) { + if (bytesPerWord == 1) { + sourceFile->read(dest, copy); + dest += copy; + } else { + while (copy) { *dest = sourceFile->readUint16LE(); dest++; + copy--; } - - copy -= 4; - } - - while (copy) { - *dest++ = sourceFile->readUint16LE(); - copy--; } } } @@ -181,42 +158,16 @@ void Utils::unDiffByteWord(uint16 *dest, Common::File *sourceFile) { /** * Undiffs a piece of memory when header size is a byte, and copy/skip size - * is a byte. + * is a byte or a word or a double word. */ -void Utils::VUnDiffByteByte(byte *dest, Common::File *sourceFile, uint16 bytesPerRow) { - for (uint16 counter = 0; counter < _dataBytesPerRow; ) { - byte *curPtr = dest + counter; - - for (;;) { - uint16 skip = sourceFile->readByte(); - uint16 copy = sourceFile->readByte(); - - if (skip == 255) { - counter += copy; - break; - } else { - curPtr += (skip * bytesPerRow); - - while (copy) { - copy--; - *curPtr = sourceFile->readByte(); - curPtr += bytesPerRow; - } - } - } - } -} - -/** - * Undiffs a piece of memory when header size is a byte, and copy/skip size - * is a word. - */ -void Utils::VUnDiffByteWord(uint16 *dest, Common::File *sourceFile, uint16 bytesPerRow) { +template +void Utils::verticalUnDiff(T *dest, Common::File *sourceFile, uint16 bytesPerRow) { uint16 counter = 0; - uint16 wordsPerRow = bytesPerRow / 2; + byte bytesPerWord = sizeof(T); + uint16 wordsPerRow = bytesPerRow / bytesPerWord; - while (counter < (_dataBytesPerRow >> 1)) { - uint16 *curPtr = dest + counter; + while (counter < wordsPerRow) { + T *curPtr = dest + counter; for (;;) { uint16 skip = sourceFile->readByte(); @@ -229,7 +180,14 @@ void Utils::VUnDiffByteWord(uint16 *dest, Common::File *sourceFile, uint16 bytes curPtr += (skip * wordsPerRow); while (copy) { - *curPtr = sourceFile->readUint16LE(); + if (bytesPerWord == 1) + *curPtr++ = sourceFile->readByte(); + else if (bytesPerWord == 2) + *curPtr = sourceFile->readUint16LE(); + else if (bytesPerWord == 4) + *curPtr = sourceFile->readUint32LE(); + else + error("verticalUnDiff: Invalid bytesPerWord (%d)", bytesPerWord); curPtr += wordsPerRow; copy--; } @@ -238,40 +196,6 @@ void Utils::VUnDiffByteWord(uint16 *dest, Common::File *sourceFile, uint16 bytes } } -/** - * Undiffs a piece of memory when header size is a byte, and copy/skip size - * is a long. - */ -void Utils::VUnDiffByteLong(uint32 *dest, Common::File *sourceFile, uint16 bytesPerRow) { - uint16 counter = 0; - uint16 longsperrow = bytesPerRow / 4; - - while (counter < (_dataBytesPerRow >> 2)) { - uint32 *curPtr = dest + counter; - - for (;;) { - uint16 skip = sourceFile->readByte(); - uint16 copy = sourceFile->readByte(); - - if (skip == 255) { - counter += copy; - break; - } else { - curPtr += (skip * longsperrow); - - while (copy) { - *curPtr = sourceFile->readUint32LE(); - curPtr += longsperrow; - copy--; - } - } - } - } -} - -/** - * Runlength decodes a chunk of memory. - */ void Utils::runLengthDecode(byte *dest, Common::File *sourceFile) { int8 num; int16 count; @@ -289,17 +213,15 @@ void Utils::runLengthDecode(byte *dest, Common::File *sourceFile) { num = sourceFile->readSByte(); while (count) { - *dest++ = num; + *dest = num; + dest++; count--; } } } } -/** - * Does a vertical run length decode. - */ -void Utils::VRunLengthDecode(byte *dest, Common::File *sourceFile, uint16 bytesPerRow) { +void Utils::verticalRunLengthDecode(byte *dest, Common::File *sourceFile, uint16 bytesPerRow) { int16 count; byte *top = dest; @@ -335,24 +257,24 @@ void Utils::VRunLengthDecode(byte *dest, Common::File *sourceFile, uint16 bytesP /** * Does the undiffing between the bitmaps. */ -void Utils::unDiff(byte *newBuf, byte *oldBuf, Common::File *sourceFile, uint16 bytesPerRow, bool isV) { +void Utils::unDiff(byte *newBuf, byte *oldBuf, Common::File *sourceFile, uint16 bytesPerRow, bool isVertical) { sourceFile->skip(1); byte bufType = sourceFile->readByte(); - if (isV) { + if (isVertical) { if (bufType == 0) - VUnDiffByteByte(newBuf, sourceFile, bytesPerRow); + verticalUnDiff(newBuf, sourceFile, bytesPerRow); else if (bufType == 1) - VUnDiffByteWord((uint16 *)newBuf, sourceFile, bytesPerRow); + verticalUnDiff((uint16 *)newBuf, sourceFile, bytesPerRow); else if (bufType == 3) - VUnDiffByteLong((uint32 *)newBuf, sourceFile, bytesPerRow); + verticalUnDiff((uint32 *)newBuf, sourceFile, bytesPerRow); else error("Unexpected variable compression scheme %d", bufType); } else { if (bufType == 0) - unDiffByteByte(newBuf, sourceFile); + unDiff(newBuf, sourceFile); else if (bufType == 1) - unDiffByteWord((uint16 *)newBuf, sourceFile); + unDiff((uint16 *)newBuf, sourceFile); else error("Unexpected compression scheme %d", bufType); } diff --git a/engines/lab/utils.h b/engines/lab/utils.h index 0d4f6496e1..4bd29b906f 100644 --- a/engines/lab/utils.h +++ b/engines/lab/utils.h @@ -38,11 +38,11 @@ private: LabEngine *_vm; uint16 _dataBytesPerRow; - void unDiffByteByte(byte *dest, Common::File *sourceFile); - void unDiffByteWord(uint16 *dest, Common::File *sourceFile); - void VUnDiffByteByte(byte *dest, Common::File *sourceFile, uint16 bytesPerRow); - void VUnDiffByteWord(uint16 *dest, Common::File *sourceFile, uint16 bytesPerRow); - void VUnDiffByteLong(uint32 *dest, Common::File *sourceFile, uint16 bytesPerRow); + template + void unDiff(T *dest, Common::File *sourceFile); + + template + void verticalUnDiff(T *dest, Common::File *sourceFile, uint16 bytesPerRow); public: Utils(LabEngine *vm); @@ -58,9 +58,9 @@ public: uint16 mapScaleX(uint16 x); uint16 mapScaleY(uint16 y); Common::Point vgaUnscale(Common::Point pos); - void unDiff(byte *newBuf, byte *oldBuf, Common::File *sourceFile, uint16 bytesPerRow, bool isV); + void unDiff(byte *newBuf, byte *oldBuf, Common::File *sourceFile, uint16 bytesPerRow, bool isVertical); void runLengthDecode(byte *dest, Common::File *sourceFile); - void VRunLengthDecode(byte *dest, Common::File *sourceFile, uint16 bytesPerRow); + void verticalRunLengthDecode(byte *dest, Common::File *sourceFile, uint16 bytesPerRow); void setBytesPerRow(int num); uint16 getRandom(uint16 max); }; -- cgit v1.2.3