void TMXMapInfo::textHandler(void *ctx, const char *ch, int len) { CC_UNUSED_PARAM(ctx); TMXMapInfo *tmxMapInfo = this; std::string text(ch, 0, len); if (tmxMapInfo->isStoringCharacters()) { std::string currentString = tmxMapInfo->getCurrentString(); currentString += text; tmxMapInfo->setCurrentString(currentString); } }
void TMXMapInfo::endElement(void *ctx, const char *name) { CC_UNUSED_PARAM(ctx); TMXMapInfo *tmxMapInfo = this; std::string elementName = name; if (elementName == "data") { if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribBase64) { tmxMapInfo->setStoringCharacters(false); TMXLayerInfo* layer = tmxMapInfo->getLayers().back(); std::string currentString = tmxMapInfo->getCurrentString(); unsigned char *buffer; auto len = base64Decode((unsigned char*)currentString.c_str(), (unsigned int)currentString.length(), &buffer); if (!buffer) { CCLOG("cocos2d: TiledMap: decode data error"); return; } if (tmxMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib)) { unsigned char *deflated = nullptr; Size s = layer->_layerSize; // int sizeHint = s.width * s.height * sizeof(uint32_t); ssize_t sizeHint = s.width * s.height * sizeof(unsigned int); ssize_t CC_UNUSED inflatedLen = ZipUtils::inflateMemoryWithHint(buffer, len, &deflated, sizeHint); CCASSERT(inflatedLen == sizeHint, "inflatedLen should be equal to sizeHint!"); free(buffer); buffer = nullptr; if (!deflated) { CCLOG("cocos2d: TiledMap: inflate data error"); return; } layer->_tiles = reinterpret_cast<uint32_t*>(deflated); } else { layer->_tiles = reinterpret_cast<uint32_t*>(buffer); } tmxMapInfo->setCurrentString(""); } else if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribNone) { _xmlTileIndex = 0; } } else if (elementName == "map") { // The map element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "layer") { // The layer element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "objectgroup") { // The objectgroup element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "object") { // The object element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "tileset") { _recordFirstGID = true; } }
void TMXMapInfo::endElement(void *ctx, const char *name) { CC_UNUSED_PARAM(ctx); TMXMapInfo *tmxMapInfo = this; std::string elementName = name; if (elementName == "data") { if (tmxMapInfo->getLayerAttribs() & TMXLayerAttribBase64) { tmxMapInfo->setStoringCharacters(false); TMXLayerInfo* layer = tmxMapInfo->getLayers().back(); std::string currentString = tmxMapInfo->getCurrentString(); unsigned char *buffer; auto len = base64Decode((unsigned char*)currentString.c_str(), (unsigned int)currentString.length(), &buffer); if (!buffer) { CCLOG("cocos2d: TiledMap: decode data error"); return; } if (tmxMapInfo->getLayerAttribs() & (TMXLayerAttribGzip | TMXLayerAttribZlib)) { unsigned char *deflated = nullptr; Size s = layer->_layerSize; // int sizeHint = s.width * s.height * sizeof(uint32_t); ssize_t sizeHint = s.width * s.height * sizeof(unsigned int); ssize_t CC_UNUSED inflatedLen = ZipUtils::inflateMemoryWithHint(buffer, len, &deflated, sizeHint); CCASSERT(inflatedLen == sizeHint, "inflatedLen should be equal to sizeHint!"); free(buffer); buffer = nullptr; if (!deflated) { CCLOG("cocos2d: TiledMap: inflate data error"); return; } layer->_tiles = reinterpret_cast<uint32_t*>(deflated); } else { layer->_tiles = reinterpret_cast<uint32_t*>(buffer); } 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; } } else if (elementName == "map") { // The map element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "layer") { // The layer element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "objectgroup") { // The objectgroup element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "object") { // The object element has ended tmxMapInfo->setParentElement(TMXPropertyNone); } else if (elementName == "tileset") { _recordFirstGID = true; } }