From 6c6cc9c83ca6eea899bac557a67c05db54058f49 Mon Sep 17 00:00:00 2001 From: Lactozilla Date: Fri, 26 Apr 2024 17:10:41 -0300 Subject: [PATCH] Support relative spritesheet paths --- source/Engine/ResourceTypes/ISprite.cpp | 47 +++++++++++++++++++------ source/Engine/Utilities/StringUtils.cpp | 10 ++++++ 2 files changed, 47 insertions(+), 10 deletions(-) diff --git a/source/Engine/ResourceTypes/ISprite.cpp b/source/Engine/ResourceTypes/ISprite.cpp index 23ff2d7f..385a2750 100644 --- a/source/Engine/ResourceTypes/ISprite.cpp +++ b/source/Engine/ResourceTypes/ISprite.cpp @@ -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); @@ -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 @@ -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++) { diff --git a/source/Engine/Utilities/StringUtils.cpp b/source/Engine/Utilities/StringUtils.cpp index 7210f5d8..71549c39 100644 --- a/source/Engine/Utilities/StringUtils.cpp +++ b/source/Engine/Utilities/StringUtils.cpp @@ -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++; + } +}