2009-06-14 13:31:54 +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.
|
|
|
|
*
|
|
|
|
* $URL$
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2009-06-15 14:24:55 +00:00
|
|
|
#include "asylum/respack.h"
|
2009-06-14 13:31:54 +00:00
|
|
|
|
|
|
|
namespace Asylum {
|
|
|
|
|
|
|
|
ResourcePack::ResourcePack(const char *resourceFile) {
|
2009-06-14 18:07:34 +00:00
|
|
|
init(resourceFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourcePack::ResourcePack(int resourceIndex) {
|
|
|
|
// We don't use the file number part of resource IDs
|
|
|
|
//uint32 fileNum = (resourceID >> 16) & 0x7FFF;
|
|
|
|
char filename[20];
|
|
|
|
sprintf(filename, "res.%03d", resourceIndex);
|
|
|
|
init(filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
ResourcePack::~ResourcePack() {
|
|
|
|
for (uint32 i = 0; i < _resources.size(); i++) {
|
|
|
|
delete _resources[i].data;
|
|
|
|
}
|
|
|
|
|
|
|
|
_resources.clear();
|
|
|
|
|
|
|
|
_packFile.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ResourcePack::init(const char *resourceFile) {
|
2009-06-14 13:31:54 +00:00
|
|
|
_packFile.open(resourceFile);
|
|
|
|
|
|
|
|
uint32 entryCount = _packFile.readUint32LE();
|
|
|
|
_resources.resize(entryCount);
|
2009-06-13 19:05:11 +00:00
|
|
|
|
|
|
|
uint32 prevOffset = _packFile.readUint32LE();
|
|
|
|
uint32 nextOffset = 0;
|
|
|
|
|
|
|
|
for (uint32 i = 0; i < entryCount; i++) {
|
|
|
|
ResourceEntry entry;
|
|
|
|
entry.offset = prevOffset;
|
|
|
|
|
|
|
|
// Read the offset of the next entry to determine the size of this one
|
|
|
|
nextOffset = (i < entryCount - 1) ? _packFile.readUint32LE() : _packFile.size();
|
|
|
|
entry.size = (nextOffset > 0) ? nextOffset - prevOffset : _packFile.size() - prevOffset;
|
|
|
|
entry.data = 0;
|
|
|
|
|
|
|
|
_resources[i] = entry;
|
|
|
|
|
|
|
|
prevOffset = nextOffset;
|
2009-06-14 13:31:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-15 10:39:22 +00:00
|
|
|
ResourceEntry *ResourcePack::getResource(uint32 resourceId) {
|
|
|
|
uint16 index = resourceId & 0xFFFF;
|
2009-06-14 13:31:54 +00:00
|
|
|
if (!_resources[index].data) {
|
|
|
|
// Load the requested resource if it's not loaded already
|
|
|
|
_packFile.seek(_resources[index].offset, SEEK_SET);
|
|
|
|
_resources[index].data = new byte[_resources[index].size];
|
|
|
|
_packFile.read(_resources[index].data, _resources[index].size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return &_resources[index];
|
|
|
|
}
|
2009-06-14 15:46:31 +00:00
|
|
|
|
2009-06-14 13:31:54 +00:00
|
|
|
} // end of namespace Asylum
|