Skip to content

Commit

Permalink
SWORD25: Moved the thumbnail handling code to its appropriate place
Browse files Browse the repository at this point in the history
PNGLoader is able to load images embedded in saved games already. This
helps remove some duplicate code
  • Loading branch information
bluegr committed May 5, 2011
1 parent 51136ac commit d733462
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 58 deletions.
24 changes: 23 additions & 1 deletion engines/sword25/gfx/image/renderedimage.cpp
Expand Up @@ -36,6 +36,7 @@
// INCLUDES
// -----------------------------------------------------------------------------

#include "common/savefile.h"
#include "sword25/package/packagemanager.h"
#include "sword25/gfx/image/pngloader.h"
#include "sword25/gfx/image/renderedimage.h"
Expand All @@ -44,6 +45,14 @@

namespace Sword25 {

// Duplicated from kernel/persistenceservice.cpp
static Common::String generateSavegameFilename(uint slotID) {
char buffer[100];
// NOTE: This is hardcoded to sword25
snprintf(buffer, 100, "%s.%.3d", "sword25", slotID);
return Common::String(buffer);
}

// -----------------------------------------------------------------------------
// CONSTRUCTION / DESTRUCTION
// -----------------------------------------------------------------------------
Expand All @@ -62,7 +71,20 @@ RenderedImage::RenderedImage(const Common::String &filename, bool &result) :
// Load file
byte *pFileData;
uint fileSize;
pFileData = pPackage->getFile(filename, &fileSize);

if (filename.hasPrefix("/saves")) {
// A savegame thumbnail
Common::SaveFileManager *sfm = g_system->getSavefileManager();
int slotNum = atoi(filename.c_str() + filename.size() - 3);
Common::InSaveFile *file = sfm->openForLoading(generateSavegameFilename(slotNum));
fileSize = file->size();
pFileData = new byte[fileSize];
file->read(pFileData, fileSize);
delete file;
} else {
pFileData = pPackage->getFile(filename, &fileSize);
}

if (!pFileData) {
error("File \"%s\" could not be loaded.", filename.c_str());
return;
Expand Down
57 changes: 0 additions & 57 deletions engines/sword25/package/packagemanager.cpp
Expand Up @@ -141,29 +141,6 @@ bool PackageManager::loadDirectoryAsPackage(const Common::String &directoryName,
}
}

// Duplicated from kernel/persistenceservice.cpp
static Common::String generateSavegameFilename(uint slotID) {
char buffer[100];
// NOTE: This is hardcoded to sword25
snprintf(buffer, 100, "%s.%.3d", "sword25", slotID);
return Common::String(buffer);
}

// Duplicated from kernel/persistenceservice.cpp
static Common::String loadString(Common::InSaveFile *in, uint maxSize = 999) {
Common::String result;

char ch = (char)in->readByte();
while (ch != '\0') {
result += ch;
if (result.size() >= maxSize)
break;
ch = (char)in->readByte();
}

return result;
}

byte *PackageManager::getFile(const Common::String &fileName, uint *fileSizePtr) {
const Common::String B25S_EXTENSION(".b25s");
Common::SeekableReadStream *in;
Expand All @@ -188,40 +165,6 @@ byte *PackageManager::getFile(const Common::String &fileName, uint *fileSizePtr)
return buffer;
}

if (fileName.hasPrefix("/saves")) {
// A savegame thumbnail
Common::SaveFileManager *sfm = g_system->getSavefileManager();
int slotNum = atoi(fileName.c_str() + fileName.size() - 3);
Common::InSaveFile *file = sfm->openForLoading(generateSavegameFilename(slotNum));

if (file) {
loadString(file); // storedMarker
loadString(file); // storedVersionID
loadString(file); // gameDescription
int gameDataLength = atoi(loadString(file).c_str());
loadString(file); // gamedataUncompressedLength
// Skip the savegame data
file->skip(gameDataLength);

int thumbnailSize = file->size() - file->pos();

if (thumbnailSize <= 0) {
warning("Saved game at slot %d does not contain a thumbnail", slotNum);
delete file;
return 0;
}

if (fileSizePtr)
*fileSizePtr = thumbnailSize;

byte *thumbnail = new byte[thumbnailSize];
file->read(thumbnail, thumbnailSize);

delete file;
return thumbnail;
}
}

Common::ArchiveMemberPtr fileNode = getArchiveMember(normalizePath(fileName, _currentDirectory));
if (!fileNode)
return 0;
Expand Down

0 comments on commit d733462

Please sign in to comment.