Skip to content

Commit dc14250

Browse files
PatriceJiangdrelaptop
authored andcommitted
[TiledMap] locate image path relative to tsx file (#19147)
* fix image locator * update assertion
1 parent 8a142cb commit dc14250

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

cocos/2d/CCTMXXMLParser.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,9 @@ bool TMXMapInfo::parseXMLFile(const std::string& xmlFilename)
211211
}
212212

213213
parser.setDelegator(this);
214-
215-
return parser.parse(FileUtils::getInstance()->fullPathForFilename(xmlFilename));
214+
auto fullPath = FileUtils::getInstance()->fullPathForFilename(xmlFilename);
215+
CCASSERT(FileUtils::getInstance()->isFileExist(fullPath), "TMXMapInfo::parseXMLFile xml file not exists");
216+
return parser.parse(fullPath);
216217
}
217218

218219
// the XML parser calls here with all the elements
@@ -314,7 +315,7 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char *name, const char **atts
314315
_currentFirstGID = 0;
315316
}
316317
_recordFirstGID = false;
317-
318+
_externalTilesetFullPath = externalTilesetFilename;
318319
tmxMapInfo->parseXMLFile(externalTilesetFilename);
319320
}
320321
else
@@ -431,7 +432,12 @@ void TMXMapInfo::startElement(void* /*ctx*/, const char *name, const char **atts
431432
std::string imagename = attributeDict["source"].asString();
432433
tileset->_originSourceImage = imagename;
433434

434-
if (_TMXFileName.find_last_of("/") != string::npos)
435+
if (!_externalTilesetFullPath.empty())
436+
{
437+
string dir = _externalTilesetFullPath.substr(0, _externalTilesetFullPath.find_last_of("/") + 1);
438+
tileset->_sourceImage = dir + imagename;
439+
}
440+
else if (_TMXFileName.find_last_of("/") != string::npos)
435441
{
436442
string dir = _TMXFileName.substr(0, _TMXFileName.find_last_of("/") + 1);
437443
tileset->_sourceImage = dir + imagename;

cocos/2d/CCTMXXMLParser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@ class CC_DLL TMXMapInfo : public Ref, public SAXDelegator
343343
int _currentFirstGID;
344344
bool _recordFirstGID;
345345
std::string _externalTilesetFilename;
346+
std::string _externalTilesetFullPath;
346347
};
347348

348349
// end of tilemap_parallax_nodes group

0 commit comments

Comments
 (0)