CCFastTMXTiledMap.cpp 7.7 KB

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