Skip to content

Commit

Permalink
Correctly load resources that exist in a different folder to TMX file (
Browse files Browse the repository at this point in the history
…#2316)

* Correctly load resources that exist in a different folder to TMX file.
Minor code improvements

* Clear current asset path for each new TMX being loaded
  • Loading branch information
rh101 authored Jan 9, 2025
1 parent 290e4f5 commit 8df3f92
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
30 changes: 18 additions & 12 deletions core/2d/TMXXMLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace ax
{

// implementation TMXLayerInfo
TMXLayerInfo::TMXLayerInfo() : _name(""), _tiles(nullptr), _ownTiles(true) {}
TMXLayerInfo::TMXLayerInfo() : _tiles(nullptr), _ownTiles(true) {}

TMXLayerInfo::~TMXLayerInfo()
{
Expand All @@ -63,7 +63,7 @@ ValueMap& TMXLayerInfo::getProperties()

void TMXLayerInfo::setProperties(ValueMap var)
{
_properties = var;
_properties = std::move(var);
}

// implementation TMXTilesetInfo
Expand Down Expand Up @@ -139,6 +139,7 @@ void TMXMapInfo::internalInit(std::string_view tmxFileName, std::string_view res
_layerAttribs = TMXLayerAttribNone;
_parentElement = TMXPropertyNone;
_currentFirstGID = -1;
_currentAssetPath = "";
}

bool TMXMapInfo::initWithXML(std::string_view tmxString, std::string_view resourcePath)
Expand Down Expand Up @@ -287,7 +288,7 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
{
// If this is an external tileset then start parsing that
std::string externalTilesetFilename = attributeDict["source"].asString();
if (externalTilesetFilename != "")
if (!externalTilesetFilename.empty())
{
_externalTilesetFilename = externalTilesetFilename;

Expand All @@ -301,7 +302,7 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
{
externalTilesetFilename = _resources + "/" + externalTilesetFilename;
}
externalTilesetFilename = FileUtils::getInstance()->fullPathForFilename(externalTilesetFilename);
_currentAssetPath = FileUtils::getInstance()->fullPathForFilename(externalTilesetFilename);

_currentFirstGID = attributeDict["firstgid"].asInt();
if (_currentFirstGID < 0)
Expand All @@ -310,10 +311,15 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
}
_recordFirstGID = false;

tmxMapInfo->parseXMLFile(externalTilesetFilename);
tmxMapInfo->parseXMLFile(_currentAssetPath);
}
else
{
if (_currentAssetPath.empty())
{
_currentAssetPath = _TMXFileName;
}

TMXTilesetInfo* tileset = new TMXTilesetInfo();
tileset->_name = attributeDict["name"].asString();

Expand Down Expand Up @@ -425,22 +431,22 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
std::string imagename = attributeDict["source"].asString();
tileset->_originSourceImage = imagename;

if (_TMXFileName.find_last_of('/') != std::string::npos)
if (_currentAssetPath.find_last_of('/') != std::string::npos)
{
std::string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of('/') + 1);
std::string dir = _currentAssetPath.substr(0, _currentAssetPath.find_last_of('/') + 1);
tileset->_sourceImage = dir + imagename;
}
else
{
tileset->_sourceImage = _resources + (_resources.size() ? "/" : "") + imagename;
tileset->_sourceImage = _resources + (!_resources.empty() ? "/" : "") + imagename;
}
}
else if (elementName == "data")
{
std::string encoding = attributeDict["encoding"].asString();
std::string compression = attributeDict["compression"].asString();

if (encoding == "")
if (encoding.empty())
{
tmxMapInfo->setLayerAttribs(tmxMapInfo->getLayerAttribs() | TMXLayerAttribNone);

Expand All @@ -467,7 +473,7 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
layerAttribs = tmxMapInfo->getLayerAttribs();
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribZlib);
}
AXASSERT(compression == "" || compression == "gzip" || compression == "zlib",
AXASSERT(compression.empty() || compression == "gzip" || compression == "zlib",
"TMX: unsupported compression method");
}
else if (encoding == "csv")
Expand Down Expand Up @@ -678,8 +684,8 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char* name, const char** atts
TMXTilesetInfo* info = tmxMapInfo->getTilesets().back();
auto animInfo = info->_animationInfo.at(tmxMapInfo->getParentGID());
// calculate gid of frame
animInfo->_frames.emplace_back(
TMXTileAnimFrame(info->_firstGid + attributeDict["tileid"].asInt(), attributeDict["duration"].asFloat()));
animInfo->_frames.emplace_back(info->_firstGid + attributeDict["tileid"].asInt(),
attributeDict["duration"].asFloat());
}
}

Expand Down
1 change: 1 addition & 0 deletions core/2d/TMXXMLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,7 @@ class AX_DLL TMXMapInfo : public Object, public SAXDelegator
int _currentFirstGID;
bool _recordFirstGID;
std::string _externalTilesetFilename;
std::string _currentAssetPath;
//! current property key
std::string _currentPropertyKey;
};
Expand Down

0 comments on commit 8df3f92

Please sign in to comment.