Skip to content

Commit

Permalink
texture's path in sprite can be relative to sprite's path
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Jun 16, 2024
1 parent de84289 commit 46fcc4b
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/gui/sprite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,13 @@ void Sprite::setTexture(const Path& path)
if (path.isEmpty()) {
m_texture = nullptr;
} else {
m_texture = (Texture*)getResourceManager().getOwner().load<Texture>(path);
if (path.c_str()[0] == '\\' || path.c_str()[0] == '/') {
m_texture = (Texture*)getResourceManager().getOwner().load<Texture>(path);
} else {
StringView dir = Path::getDir(getPath());
StaticString<MAX_PATH> tmp(dir, "/", path);
m_texture = (Texture*)getResourceManager().getOwner().load<Texture>(Path(tmp));
}
}
}

Expand All @@ -52,7 +58,18 @@ void Sprite::serialize(OutputMemoryStream& out)
out << "bottom(" << bottom << ")\n";
out << "left(" << left << ")\n";
out << "right(" << right << ")\n";
out << "texture \"" << (m_texture ? m_texture->getPath() : Path()) << "\"";
if (m_texture) {
StringView dir = Path::getDir(getPath());
if (startsWith(m_texture->getPath(), dir)) {
StringView path = m_texture->getPath();
path.removePrefix(dir.size());
out << "texture \"" << path << "\"";
} else {
out << "texture \"/" << m_texture->getPath() << "\"";
}
} else {
out << "texture \"\"";
}
}

namespace LuaSpriteAPI {
Expand Down

0 comments on commit 46fcc4b

Please sign in to comment.