CCFastTMXTiledMap.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /****************************************************************************
  2. Copyright (c) 2009-2010 Ricardo Quesada
  3. Copyright (c) 2010-2012 cocos2d-x.org
  4. Copyright (c) 2011 Zynga Inc.
  5. Copyright (c) 2013-2017 Chukong Technologies Inc.
  6. http://www.cocos2d-x.org
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. ****************************************************************************/
  23. #include "2d/CCFastTMXTiledMap.h"
  24. #include "2d/CCFastTMXLayer.h"
  25. #include "base/ccUTF8.h"
  26. NS_CC_BEGIN
  27. namespace experimental {
  28. // implementation FastTMXTiledMap
  29. TMXTiledMap * TMXTiledMap::create(const std::string& tmxFile)
  30. {
  31. TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
  32. if (ret->initWithTMXFile(tmxFile))
  33. {
  34. ret->autorelease();
  35. return ret;
  36. }
  37. CC_SAFE_DELETE(ret);
  38. return nullptr;
  39. }
  40. TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath)
  41. {
  42. TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
  43. if (ret->initWithXML(tmxString, resourcePath))
  44. {
  45. ret->autorelease();
  46. return ret;
  47. }
  48. CC_SAFE_DELETE(ret);
  49. return nullptr;
  50. }
  51. bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile)
  52. {
  53. CCASSERT(tmxFile.size()>0, "FastTMXTiledMap: tmx file should not be empty");
  54. setContentSize(Size::ZERO);
  55. TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile);
  56. if (! mapInfo)
  57. {
  58. return false;
  59. }
  60. CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
  61. buildWithMapInfo(mapInfo);
  62. return true;
  63. }
  64. bool TMXTiledMap::initWithXML(const std::string& tmxString, const std::string& resourcePath)
  65. {
  66. setContentSize(Size::ZERO);
  67. TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);
  68. CCASSERT( !mapInfo->getTilesets().empty(), "FastTMXTiledMap: Map not found. Please check the filename.");
  69. buildWithMapInfo(mapInfo);
  70. return true;
  71. }
  72. TMXTiledMap::TMXTiledMap()
  73. :_mapSize(Size::ZERO)
  74. ,_tileSize(Size::ZERO)
  75. {
  76. }
  77. TMXTiledMap::~TMXTiledMap()
  78. {
  79. }
  80. // private
  81. TMXLayer * TMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
  82. {
  83. TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
  84. if (tileset == nullptr)
  85. return nullptr;
  86. TMXLayer *layer = TMXLayer::create(tileset, layerInfo, mapInfo);
  87. // tell the layerinfo to release the ownership of the tiles map.
  88. layerInfo->_ownTiles = false;
  89. layer->setupTiles();
  90. return layer;
  91. }
  92. TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
  93. {
  94. Size size = layerInfo->_layerSize;
  95. auto& tilesets = mapInfo->getTilesets();
  96. for (auto iter = tilesets.crbegin(), iterCrend = tilesets.crend(); iter != iterCrend; ++iter)
  97. {
  98. TMXTilesetInfo* tilesetInfo = *iter;
  99. if (tilesetInfo)
  100. {
  101. for( int y=0; y < size.height; y++ )
  102. {
  103. for( int x=0; x < size.width; x++ )
  104. {
  105. uint32_t pos = static_cast<uint32_t>(x + size.width * y);
  106. uint32_t gid = layerInfo->_tiles[ pos ];
  107. // gid are stored in little endian.
  108. // if host is big endian, then swap
  109. //if( o == CFByteOrderBigEndian )
  110. // gid = CFSwapInt32( gid );
  111. /* We support little endian.*/
  112. // FIXME: gid == 0 --> empty tile
  113. if( gid != 0 )
  114. {
  115. // Optimization: quick return
  116. // if the layer is invalid (more than 1 tileset per layer) an CCAssert will be thrown later
  117. if( (gid & kTMXFlippedMask)
  118. >= static_cast<uint32_t>(tilesetInfo->_firstGid))
  119. {
  120. return tilesetInfo;
  121. }
  122. }
  123. }
  124. }
  125. }
  126. }
  127. // If all the tiles are 0, return empty tileset
  128. CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str());
  129. return nullptr;
  130. }
  131. void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
  132. {
  133. _mapSize = mapInfo->getMapSize();
  134. _tileSize = mapInfo->getTileSize();
  135. _mapOrientation = mapInfo->getOrientation();
  136. _objectGroups = mapInfo->getObjectGroups();
  137. _properties = mapInfo->getProperties();
  138. _tileProperties = mapInfo->getTileProperties();
  139. int idx=0;
  140. auto& layers = mapInfo->getLayers();
  141. for(const auto &layerInfo : layers) {
  142. if (layerInfo->_visible)
  143. {
  144. TMXLayer *child = parseLayer(layerInfo, mapInfo);
  145. if (child == nullptr) {
  146. idx++;
  147. continue;
  148. }
  149. addChild(child, idx, idx);
  150. // update content size with the max size
  151. const Size& childSize = child->getContentSize();
  152. Size currentSize = this->getContentSize();
  153. currentSize.width = std::max( currentSize.width, childSize.width );
  154. currentSize.height = std::max( currentSize.height, childSize.height );
  155. this->setContentSize(currentSize);
  156. idx++;
  157. }
  158. }
  159. }
  160. // public
  161. TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const
  162. {
  163. CCASSERT(layerName.size() > 0, "Invalid layer name!");
  164. for (auto& child : _children)
  165. {
  166. TMXLayer* layer = dynamic_cast<TMXLayer*>(child);
  167. if(layer)
  168. {
  169. if(layerName.compare( layer->getLayerName()) == 0)
  170. {
  171. return layer;
  172. }
  173. }
  174. }
  175. // layer not found
  176. return nullptr;
  177. }
  178. TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
  179. {
  180. CCASSERT(groupName.size() > 0, "Invalid group name!");
  181. if (_objectGroups.size()>0)
  182. {
  183. for (const auto& objectGroup : _objectGroups)
  184. {
  185. if (objectGroup && objectGroup->getGroupName() == groupName)
  186. {
  187. return objectGroup;
  188. }
  189. }
  190. }
  191. // objectGroup not found
  192. return nullptr;
  193. }
  194. Value TMXTiledMap::getProperty(const std::string& propertyName) const
  195. {
  196. if (_properties.find(propertyName) != _properties.end())
  197. return _properties.at(propertyName);
  198. return Value();
  199. }
  200. Value TMXTiledMap::getPropertiesForGID(int GID) const
  201. {
  202. if (_tileProperties.find(GID) != _tileProperties.end())
  203. return _tileProperties.at(GID);
  204. return Value();
  205. }
  206. std::string TMXTiledMap::getDescription() const
  207. {
  208. return StringUtils::format("<FastTMXTiledMap | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
  209. }
  210. } //end of namespace experimental
  211. NS_CC_END