Skip to content

Commit

Permalink
Merge pull request #516 from lukaslw/prince-lukaslw
Browse files Browse the repository at this point in the history
PRINCE: Adding The Prince and the Coward
  • Loading branch information
sev- committed Oct 10, 2014
2 parents c128365 + c769c3b commit bb8f66c
Show file tree
Hide file tree
Showing 45 changed files with 13,939 additions and 0 deletions.
178 changes: 178 additions & 0 deletions engines/prince/animation.cpp
@@ -0,0 +1,178 @@
/* 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"
#include "prince/decompress.h"

#include "common/debug.h"
#include "common/endian.h"

namespace Prince {

Animation::Animation() : _loopCount(0), _phaseCount(0), _frameCount(0), _baseX(0), _baseY(0)
{
}

Animation::~Animation() {
clear();
}

void Animation::clear() {
_phaseList.clear();
for (int i = 0; i < _frameCount; i++) {
_frameList[i]._surface->free();
delete _frameList[i]._surface;
_frameList[i]._surface = nullptr;
if (_frameList[i]._compressedData != nullptr) {
free(_frameList[i]._compressedData);
_frameList[i]._compressedData = nullptr;
}
}
}

bool Animation::loadStream(Common::SeekableReadStream &stream) {
stream.skip(2); // skip not used x and y coord diff
_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++;
}

Frame tempFrame;
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();

tempFrame._surface = new Graphics::Surface();
tempFrame._surface->create(frameWidth, frameHeight, Graphics::PixelFormat::createFormatCLUT8());
if (frameDataOffset == MKTAG('m', 'a', 's', 'm')) {
tempFrame._isCompressed = true;
tempFrame._dataSize = stream.readUint32LE();
tempFrame._compressedData = (byte *)malloc(tempFrame._dataSize);
stream.read(tempFrame._compressedData, tempFrame._dataSize);
} else {
tempFrame._isCompressed = false;
tempFrame._dataSize = 0;
tempFrame._compressedData = nullptr;
stream.seek(frameDataPos);
for (uint16 i = 0; i < frameHeight; i++) {
stream.read(tempFrame._surface->getBasePtr(0, i), frameWidth);
}
}
_frameList.push_back(tempFrame);
}

return true;
}

int16 Animation::getLoopCount() const {
return _loopCount;
}

int32 Animation::getPhaseCount() const {
return _phaseCount;
}

int32 Animation::getFrameCount() const {
return _frameCount;
}

int16 Animation::getBaseX() const {
return _baseX;
}

int16 Animation::getBaseY() const {
return _baseY;
}

int16 Animation::getPhaseOffsetX(int phaseIndex) const {
if (phaseIndex < _phaseCount) {
return _phaseList[phaseIndex]._phaseOffsetX;
} else {
error("getPhaseOffsetX() phaseIndex: %d, phaseCount: %d", phaseIndex, _phaseCount);
}
}

int16 Animation::getPhaseOffsetY(int phaseIndex) const {
if (phaseIndex < _phaseCount) {
return _phaseList[phaseIndex]._phaseOffsetY;
} else {
error("getPhaseOffsetY() phaseIndex: %d, phaseCount: %d", phaseIndex, _phaseCount);
}
}

int16 Animation::getPhaseFrameIndex(int phaseIndex) const {
if (phaseIndex < _phaseCount) {
return _phaseList[phaseIndex]._phaseToFrameIndex;
} else {
error("getPhaseFrameIndex() phaseIndex: %d, phaseCount: %d", phaseIndex, _phaseCount);
}
}

Graphics::Surface *Animation::getFrame(int frameIndex) {
if (frameIndex < _frameCount) {
if (_frameList[frameIndex]._isCompressed) {
Decompressor dec;
byte *ddata = (byte *)malloc(_frameList[frameIndex]._dataSize);
dec.decompress(_frameList[frameIndex]._compressedData, ddata, _frameList[frameIndex]._dataSize);
int frameHeight = _frameList[frameIndex]._surface->h;
int frameWidth = _frameList[frameIndex]._surface->w;
for (uint16 i = 0; i < frameHeight; i++) {
memcpy(_frameList[frameIndex]._surface->getBasePtr(0, i), ddata + frameWidth * i, frameWidth);
}
free(ddata);
free(_frameList[frameIndex]._compressedData);
_frameList[frameIndex]._compressedData = nullptr;
_frameList[frameIndex]._dataSize = 0;
_frameList[frameIndex]._isCompressed = false;
}
return _frameList[frameIndex]._surface;
} else {
error("getFrame() frameIndex: %d, frameCount: %d", frameIndex, _frameCount);
}
}

} // End of namespace Prince
73 changes: 73 additions & 0 deletions engines/prince/animation.h
@@ -0,0 +1,73 @@
/* 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.
*
*/

#ifndef PRINCE_ANIMATION_H
#define PRINCE_ANIMATION_H

#include "common/array.h"
#include "common/stream.h"

#include "graphics/surface.h"

namespace Prince {

class Animation {
public:
Animation();
~Animation();
bool loadStream(Common::SeekableReadStream &stream);

int16 getLoopCount() const;
int32 getPhaseCount() const;
int32 getFrameCount() const;
int16 getBaseX() const;
int16 getBaseY() const;
int16 getPhaseOffsetX(int phaseIndex) const;
int16 getPhaseOffsetY(int phaseIndex) const;
int16 getPhaseFrameIndex(int phaseIndex) const;
Graphics::Surface *getFrame(int frameIndex);
void clear();

private:
struct Phase {
int16 _phaseOffsetX;
int16 _phaseOffsetY;
uint16 _phaseToFrameIndex;
};
struct Frame {
bool _isCompressed;
uint32 _dataSize;
byte *_compressedData;
Graphics::Surface *_surface;
};
Common::Array<Frame> _frameList;
Common::Array<Phase> _phaseList;
int16 _loopCount;
int16 _phaseCount;
int32 _frameCount;
int16 _baseX;
int16 _baseY;
};

} // End of namespace Prince

#endif
144 changes: 144 additions & 0 deletions engines/prince/archive.cpp
@@ -0,0 +1,144 @@
/* 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/archive.h"
#include "prince/decompress.h"

#include "common/stream.h"
#include "common/debug.h"
#include "common/memstream.h"

namespace Prince {

PtcArchive::PtcArchive() : _stream(nullptr) {
}

PtcArchive::~PtcArchive() {
close();
}

static void decrypt(byte *buffer, uint32 size) {
uint32 key = 0xDEADF00D;
while (size--) {
*buffer++ += key & 0xFF;
key ^= 0x2E84299A;
key += MKTAG('B', 'L', 'A', 'H');
key = ((key & 1) << 31) | (key >> 1);
}
}

bool PtcArchive::open(const Common::String &filename) {
_stream = SearchMan.createReadStreamForMember(filename);
if (!_stream)
return false;

_stream->readUint32LE(); // magic
uint32 fileTableOffset = _stream->readUint32LE() ^ 0x4D4F4B2D; // MOK-
uint32 fileTableSize = _stream->readUint32LE() ^ 0x534F4654; // SOFT

//debug("fileTableOffset : %08X", fileTableOffset);
//debug("fileTableSize: %08X", fileTableSize);

_stream->seek(fileTableOffset);

byte *fileTable = (byte *)malloc(fileTableSize);
byte *fileTableEnd = fileTable + fileTableSize;
_stream->read(fileTable, fileTableSize);
decrypt(fileTable, fileTableSize);

for (byte *fileItem = fileTable; fileItem < fileTableEnd; fileItem += 32) {
FileEntry item;
Common::String name = (const char*)fileItem;
item._offset = READ_LE_UINT32(fileItem + 24);
item._size = READ_LE_UINT32(fileItem + 28);
//debug("%12s %8X %d", name.c_str(), item._offset, item._size);
_items[name] = item;
}

free(fileTable);

return true;
}

void PtcArchive::close() {
delete _stream;
_stream = nullptr;
_items.clear();
}

bool PtcArchive::hasFile(const Common::String &name) const {
// TODO: check if path matching should be added
return _items.contains(name);
}

int PtcArchive::listMembers(Common::ArchiveMemberList &list) const {
int matches = 0;

for (FileMap::const_iterator it = _items.begin(); it != _items.end(); ++it) {
list.push_back(Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(it->_key, this)));
matches++;
}

return matches;
}

const Common::ArchiveMemberPtr PtcArchive::getMember(const Common::String &name) const {
if (!_items.contains(name)) {
Common::ArchiveMemberPtr();
}
return Common::ArchiveMemberList::value_type(new Common::GenericArchiveMember(name, this));
}

Common::SeekableReadStream *PtcArchive::createReadStreamForMember(const Common::String &name) const {
if (!_items.contains(name)) {
return 0;
}

const FileEntry &entryHeader = _items[name];

if (entryHeader._size < 4)
return 0;

uint32 size = entryHeader._size;

_stream->seek(entryHeader._offset);

// This *HAS* to be malloc (not new[]) because MemoryReadStream uses free() to free the memory
byte *buffer = (byte *)malloc(size);
_stream->read(buffer, size);

if (READ_BE_UINT32(buffer) == MKTAG('M', 'A', 'S', 'M')) {
Decompressor dec;
uint32 decompLen = READ_BE_UINT32(buffer + 14);
byte *decompData = (byte *)malloc(decompLen);
dec.decompress(buffer + 18, decompData, decompLen);
free(buffer);
size = decompLen;
buffer = decompData;
}

//debug("PtcArchive::createReadStreamForMember name %s", name.c_str());

return new Common::MemoryReadStream(buffer, size, DisposeAfterUse::YES);
}

} // End of namespace Prince

0 comments on commit bb8f66c

Please sign in to comment.