Skip to content

Commit

Permalink
Support relative spritesheet paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Lactozilla committed Apr 26, 2024
1 parent 230e0df commit 6c6cc9c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
47 changes: 37 additions & 10 deletions source/Engine/ResourceTypes/ISprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,6 @@ PUBLIC void ISprite::ConvertToPalette(unsigned paletteNumber) {

PUBLIC bool ISprite::LoadAnimation(const char* filename) {
char* str;
char* altered;
int animationCount, previousAnimationCount;

Stream* reader = ResourceStream::New(filename);
Expand Down Expand Up @@ -256,27 +255,54 @@ PUBLIC bool ISprite::LoadAnimation(const char* filename) {

// Load textures
for (int i = 0; i < spritesheetCount; i++) {
char fullPath[4096];

str = reader->ReadHeaderedString();

char* sheetName = StringUtils::ReplacePathSeparators(str);
if (!sheetName)
abort();
// Spritesheet path is relative to where the animation file is
if (StringUtils::StartsWith(str, "./")) {
char *parentPath = StringUtils::GetPath(filename);
if (parentPath) {
snprintf(fullPath, sizeof fullPath, "%s/%s", parentPath, str + 2);
Memory::Free(parentPath);
}
else
snprintf(fullPath, sizeof fullPath, "%s", str + 2);
}
else
StringUtils::Copy(fullPath, str, sizeof fullPath);

Memory::Free(str);

// Replace '\' with '/'
StringUtils::ReplacePathSeparatorsInPlace(fullPath);

char* sheetName = StringUtils::Duplicate(fullPath);

#ifdef ISPRITE_DEBUG
Log::Print(Log::LOG_VERBOSE, " - %s", sheetName);
#endif

SpritesheetsFilenames.push_back(sheetName);

altered = StringUtils::ConcatPaths("Sprites", sheetName);
if (!altered)
abort();
bool shouldConcatSpritesPath = true;
if (StringUtils::StartsWith(sheetName, "Sprites/")) {
// don't need to concat "Sprites/" if the path already begins with that
shouldConcatSpritesPath = false;
}

Spritesheets.push_back(AddSpriteSheet(altered));
if (shouldConcatSpritesPath) {
char* altered = StringUtils::ConcatPaths("Sprites", sheetName);
if (!altered)
abort();

Memory::Free(altered);
Spritesheets.push_back(AddSpriteSheet(altered));

Memory::Free(altered);
}
else {
Spritesheets.push_back(AddSpriteSheet(sheetName));
}
}

// Get collision group count
Expand Down Expand Up @@ -310,8 +336,9 @@ PUBLIC bool ISprite::LoadAnimation(const char* filename) {
an.Flags = reader->ReadByte();

#ifdef ISPRITE_DEBUG
Log::Print(Log::LOG_VERBOSE, " \"%s\" (%d) (Flags: %02X, FtL: %d, Spd: %d, Frames: %d)", an.Name, a, an.Flags, an.FrameToLoop, an.AnimationSpeed, frameCount);
Log::Print(Log::LOG_VERBOSE, " \"%s\" (%d) (Flags: %02X, FtL: %d, Spd: %d, Frames: %d)", an.Name, a, an.Flags, an.FrameToLoop, an.AnimationSpeed, an.FrameCount);
#endif

an.Frames.resize(an.FrameCount);

for (int i = 0; i < an.FrameCount; i++) {
Expand Down
10 changes: 10 additions & 0 deletions source/Engine/Utilities/StringUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,13 @@ PUBLIC STATIC char* StringUtils::ReplacePathSeparators(const char* path) {

return newPath;
}
PUBLIC STATIC void StringUtils::ReplacePathSeparatorsInPlace(char* path) {
if (!path)
return;

while (*path) {
if (*path == '\\')
*path = '/';
path++;
}
}

0 comments on commit 6c6cc9c

Please sign in to comment.