csv support working test

This commit is contained in:
Steve Tranby 2016-04-12 13:51:09 -06:00
parent 5aa83ef1cf
commit 510e7b831e
5 changed files with 154 additions and 11 deletions

View File

@ -439,7 +439,13 @@ void TMXMapInfo::startElement(void *ctx, const char *name, const char **atts)
}
CCASSERT( compression == "" || compression == "gzip" || compression == "zlib", "TMX: unsupported compression method" );
}
}
else if (encoding == "csv")
{
int layerAttribs = tmxMapInfo->getLayerAttribs();
tmxMapInfo->setLayerAttribs(layerAttribs | TMXLayerAttribCSV);
tmxMapInfo->setStoringCharacters(true);
}
}
else if (elementName == "object")
{
TMXObjectGroup* objectGroup = tmxMapInfo->getObjectGroups().back();
@ -671,6 +677,45 @@ void TMXMapInfo::endElement(void *ctx, const char *name)
tmxMapInfo->setCurrentString("");
}
else if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribCSV)
{
unsigned char *buffer;
TMXLayerInfo* layer = tmxMapInfo->getLayers().back();
tmxMapInfo->setStoringCharacters(false);
std::string currentString = tmxMapInfo->getCurrentString();
vector<string> gidTokens;
istringstream filestr(currentString);
string sRow;
while(getline(filestr, sRow, '\n')) {
string sGID;
istringstream rowstr(sRow);
while (getline(rowstr, sGID, ',')) {
gidTokens.push_back(sGID);
}
}
// 32-bits per gid
buffer = (unsigned char*)malloc(gidTokens.size() * 4);
if (!buffer)
{
CCLOG("cocos2d: TiledMap: CSV buffer not allocated.");
return;
}
uint32_t* bufferPtr = reinterpret_cast<uint32_t*>(buffer);
for(auto gidToken : gidTokens) {
auto tileGid = (uint32_t)strtol(gidToken.c_str(), nullptr, 10);
*bufferPtr = tileGid;
bufferPtr++;
}
layer->_tiles = reinterpret_cast<uint32_t*>(buffer);
tmxMapInfo->setCurrentString("");
}
else if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribNone)
{
_xmlTileIndex = 0;

View File

@ -62,6 +62,7 @@ enum {
TMXLayerAttribBase64 = 1 << 1,
TMXLayerAttribGzip = 1 << 2,
TMXLayerAttribZlib = 1 << 3,
TMXLayerAttribCSV = 1 << 4,
};
enum {

View File

@ -31,6 +31,7 @@ TileMapTests::TileMapTests()
ADD_TEST_CASE(TMXIsoTest1);
ADD_TEST_CASE(TMXIsoTest2);
ADD_TEST_CASE(TMXUncompressedTest);
ADD_TEST_CASE(TMXCvsFormatTest);
ADD_TEST_CASE(TMXHexTest);
ADD_TEST_CASE(TMXReadWriteTest);
ADD_TEST_CASE(TMXTilesetTest);
@ -352,6 +353,8 @@ TMXOrthoTest4::TMXOrthoTest4()
auto layer = map->getLayer("Layer 0");
auto s = layer->getLayerSize();
layer->setOpacity(128);
Sprite* sprite;
sprite = layer->getTileAt(Vec2(0,0));
@ -668,14 +671,14 @@ TMXTilesetTest::TMXTilesetTest()
{
auto map = TMXTiledMap::create("TileMaps/orthogonal-test5.tmx");
addChild(map, 0, kTagTileMap);
Size CC_UNUSED s = map->getContentSize();
CCLOG("ContentSize: %f, %f", s.width,s.height);
TMXLayer* layer;
layer = map->getLayer("Layer 0");
layer->getTexture()->setAntiAliasTexParameters();
layer = map->getLayer("Layer 1");
layer->getTexture()->setAntiAliasTexParameters();
@ -688,6 +691,37 @@ std::string TMXTilesetTest::title() const
return "TMX Tileset test";
}
//------------------------------------------------------------------
//
// TMXCvsFormatTest
//
//------------------------------------------------------------------
TMXCvsFormatTest::TMXCvsFormatTest()
{
auto map = TMXTiledMap::create("TileMaps/orthogonal-test5-csv.tmx");
CCASSERT(map, "Map was not created. Probably failed to parse!");
addChild(map, 0, kTagTileMap);
Size CC_UNUSED s = map->getContentSize();
CCLOG("ContentSize: %f, %f", s.width,s.height);
TMXLayer* layer;
layer = map->getLayer("Layer 0");
layer->getTexture()->setAntiAliasTexParameters();
layer = map->getLayer("Layer 1");
layer->getTexture()->setAntiAliasTexParameters();
layer = map->getLayer("Layer 2");
layer->getTexture()->setAntiAliasTexParameters();
}
std::string TMXCvsFormatTest::title() const
{
return "TMX CSV Parsing test";
}
//------------------------------------------------------------------
//
// TMXOrthoObjectsTest
@ -697,10 +731,10 @@ TMXOrthoObjectsTest::TMXOrthoObjectsTest()
{
auto map = TMXTiledMap::create("TileMaps/ortho-objects.tmx");
addChild(map, -1, kTagTileMap);
Size CC_UNUSED s = map->getContentSize();
CCLOG("ContentSize: %f, %f", s.width,s.height);
auto group = map->getObjectGroup("Object Group 1");
auto& objects = group->getObjects();
@ -708,18 +742,18 @@ TMXOrthoObjectsTest::TMXOrthoObjectsTest()
CCLOG("%s", objectsVal.getDescription().c_str());
auto drawNode = DrawNode::create();
for (auto& obj : objects)
{
ValueMap& dict = obj.asValueMap();
float x = dict["x"].asFloat();
float y = dict["y"].asFloat();
float width = dict["width"].asFloat();
float height = dict["height"].asFloat();
Color4F color(1.0, 1.0, 1.0, 1.0);
drawNode->drawLine( Vec2(x, y), Vec2((x+width), y), color );
drawNode->drawLine( Vec2((x+width), y), Vec2((x+width), (y+height)), color );
drawNode->drawLine( Vec2((x+width), (y+height)), Vec2(x, (y+height)), color );

View File

@ -146,7 +146,13 @@ public:
virtual std::string title() const override;
};
class TMXCvsFormatTest : public TileDemo
{
public:
CREATE_FUNC(TMXCvsFormatTest);
TMXCvsFormatTest();
virtual std::string title() const override;
};
class TMXOrthoObjectsTest : public TileDemo
{

View File

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" renderorder="right-down" width="12" height="12" tilewidth="101" tileheight="81" nextobjectid="1">
<tileset firstgid="1" name="Untitled" tilewidth="101" tileheight="171" spacing="2" margin="2" tilecount="45" columns="9">
<image source="ortho-test1.png" width="1024" height="1024"/>
</tileset>
<tileset firstgid="46" name="tileset2" tilewidth="101" tileheight="171" spacing="2" margin="2" tilecount="45" columns="9">
<image source="ortho-test1_bw.png" width="1024" height="1024"/>
</tileset>
<layer name="Layer 0" width="12" height="12">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
14,15,16,0,34,0,0,0,0,0,0,0,
0,0,0,3,2,4,5,6,0,0,0,0,
0,0,0,0,0,0,18,19,40,37,36,0,
37,37,37,17,43,24,20,21,40,10,36,0,
37,37,37,17,43,24,20,21,40,37,36,0,
37,37,37,17,43,24,20,21,40,10,36,0
</data>
</layer>
<layer name="Layer 1" width="12" height="12">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
59,60,61,0,0,0,0,0,0,0,0,0,
0,0,0,48,47,49,50,51,0,0,0,0,
0,0,0,0,0,0,63,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,63,0,63,0,0,0,0,0,0,0,0
</data>
</layer>
<layer name="Layer 2" width="12" height="12">
<data encoding="csv">
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,
14,15,16,0,0,0,0,0,0,0,0,0,
0,0,0,3,2,4,5,6,0,0,0,0,
0,0,0,0,0,0,18,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0
</data>
</layer>
</map>