Checking whether tileset is valid during parses TMXTiledMap in GameMapReader.

This commit is contained in:
pipu 2015-01-26 13:14:17 +08:00
parent 0d872ae1d7
commit 35c1e73b8f
1 changed files with 73 additions and 0 deletions

View File

@ -24,6 +24,8 @@
#include "GameMapReader.h" #include "GameMapReader.h"
#include "2d/CCTMXXMLParser.h"
#include "cocostudio/CSParseBinary_generated.h" #include "cocostudio/CSParseBinary_generated.h"
#include "cocostudio/WidgetReader/NodeReader/NodeReader.h" #include "cocostudio/WidgetReader/NodeReader/NodeReader.h"
@ -159,6 +161,77 @@ namespace cocostudio
} }
if (fileExist) if (fileExist)
{ {
/* Whether tileset is valid. */
auto mapInfo = TMXMapInfo::create(path);
auto& layers = mapInfo->getLayers();
bool valid = false;
std::string layerName = "";
for (const auto &layerInfo : layers)
{
valid = false;
if (layerInfo->_visible)
{
Size size = layerInfo->_layerSize;
auto& tilesets = mapInfo->getTilesets();
if (tilesets.size()>0)
{
TMXTilesetInfo* tileset = nullptr;
for (auto iter = tilesets.crbegin(); iter != tilesets.crend(); ++iter)
{
tileset = *iter;
if (tileset)
{
for( int y=0; y < size.height; y++ )
{
for( int x=0; x < size.width; x++ )
{
int pos = static_cast<int>(x + size.width * y);
int gid = layerInfo->_tiles[ pos ];
if( gid != 0 )
{
if( (gid & kTMXFlippedMask) >= tileset->_firstGid )
{
valid = true;
break;
}
}
}
if (valid)
{
break;
}
}
}
}
}
if (!valid)
{
layerName = layerInfo->_name;
break;
}
}
else
{
valid = true;
}
}
if (!valid)
{
Node* node = Node::create();
setPropsWithFlatBuffers(node, (Table*)gameMapOptions);
auto label = Label::create();
label->setString(__String::createWithFormat("Some error of gid are in TMX Layer '%s'", layerName.c_str())->getCString());
node->setScale(1.0f);
node->addChild(label);
return node;
}
/**/
tmx = TMXTiledMap::create(path); tmx = TMXTiledMap::create(path);
if (tmx) if (tmx)
{ {