CCTMXTiledMap.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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/CCTMXTiledMap.h"
  25. #include "2d/CCTMXXMLParser.h"
  26. #include "2d/CCTMXLayer.h"
  27. #include "2d/CCSprite.h"
  28. #include "base/ccUTF8.h"
  29. NS_CC_BEGIN
  30. // implementation TMXTiledMap
  31. TMXTiledMap * TMXTiledMap::create(const std::string& tmxFile)
  32. {
  33. TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
  34. if (ret->initWithTMXFile(tmxFile))
  35. {
  36. ret->autorelease();
  37. return ret;
  38. }
  39. CC_SAFE_DELETE(ret);
  40. return nullptr;
  41. }
  42. TMXTiledMap* TMXTiledMap::createWithXML(const std::string& tmxString, const std::string& resourcePath)
  43. {
  44. TMXTiledMap *ret = new (std::nothrow) TMXTiledMap();
  45. if (ret->initWithXML(tmxString, resourcePath))
  46. {
  47. ret->autorelease();
  48. return ret;
  49. }
  50. CC_SAFE_DELETE(ret);
  51. return nullptr;
  52. }
  53. bool TMXTiledMap::initWithTMXFile(const std::string& tmxFile)
  54. {
  55. CCASSERT(tmxFile.size()>0, "TMXTiledMap: tmx file should not be empty");
  56. _tmxFile = tmxFile;
  57. setContentSize(Size::ZERO);
  58. TMXMapInfo *mapInfo = TMXMapInfo::create(tmxFile);
  59. if (! mapInfo)
  60. {
  61. return false;
  62. }
  63. CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap: Map not found. Please check the filename.");
  64. buildWithMapInfo(mapInfo);
  65. return true;
  66. }
  67. bool TMXTiledMap::initWithXML(const std::string& tmxString, const std::string& resourcePath)
  68. {
  69. _tmxFile = tmxString;
  70. setContentSize(Size::ZERO);
  71. TMXMapInfo *mapInfo = TMXMapInfo::createWithXML(tmxString, resourcePath);
  72. CCASSERT( !mapInfo->getTilesets().empty(), "TMXTiledMap: Map not found. Please check the filename.");
  73. buildWithMapInfo(mapInfo);
  74. return true;
  75. }
  76. TMXTiledMap::TMXTiledMap()
  77. :_mapSize(Size::ZERO)
  78. ,_tileSize(Size::ZERO)
  79. ,_tmxFile("")
  80. , _tmxLayerNum(0)
  81. {
  82. }
  83. TMXTiledMap::~TMXTiledMap()
  84. {
  85. }
  86. // private
  87. TMXLayer * TMXTiledMap::parseLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
  88. {
  89. TMXTilesetInfo *tileset = tilesetForLayer(layerInfo, mapInfo);
  90. if (tileset == nullptr)
  91. return nullptr;
  92. TMXLayer *layer = TMXLayer::create(tileset, layerInfo, mapInfo);
  93. if (nullptr != layer)
  94. {
  95. // tell the layerinfo to release the ownership of the tiles map.
  96. layerInfo->_ownTiles = false;
  97. layer->setupTiles();
  98. }
  99. return layer;
  100. }
  101. TMXTilesetInfo * TMXTiledMap::tilesetForLayer(TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo)
  102. {
  103. auto height = static_cast<uint32_t>(layerInfo->_layerSize.height);
  104. auto width = static_cast<uint32_t>(layerInfo->_layerSize.width);
  105. auto& tilesets = mapInfo->getTilesets();
  106. for (auto iter = tilesets.crbegin(), end = tilesets.crend(); iter != end; ++iter)
  107. {
  108. TMXTilesetInfo* tileset = *iter;
  109. if (tileset)
  110. {
  111. for (uint32_t y = 0; y < height; y++)
  112. {
  113. for (uint32_t x = 0; x < width; x++)
  114. {
  115. auto pos = x + width * y;
  116. auto gid = layerInfo->_tiles[ pos ];
  117. // FIXME:: gid == 0 --> empty tile
  118. if (gid != 0)
  119. {
  120. // Optimization: quick return
  121. // if the layer is invalid (more than 1 tileset per layer)
  122. // an CCAssert will be thrown later
  123. if (tileset->_firstGid < 0 ||
  124. (gid & kTMXFlippedMask) >= static_cast<uint32_t>(tileset->_firstGid))
  125. return tileset;
  126. }
  127. }
  128. }
  129. }
  130. }
  131. // If all the tiles are 0, return empty tileset
  132. CCLOG("cocos2d: Warning: TMX Layer '%s' has no tiles", layerInfo->_name.c_str());
  133. return nullptr;
  134. }
  135. void TMXTiledMap::buildWithMapInfo(TMXMapInfo* mapInfo)
  136. {
  137. _mapSize = mapInfo->getMapSize();
  138. _tileSize = mapInfo->getTileSize();
  139. _mapOrientation = mapInfo->getOrientation();
  140. _objectGroups = mapInfo->getObjectGroups();
  141. _properties = mapInfo->getProperties();
  142. _tileProperties = mapInfo->getTileProperties();
  143. int idx = 0;
  144. auto& layers = mapInfo->getLayers();
  145. for (const auto &layerInfo : layers) {
  146. if (layerInfo->_visible) {
  147. TMXLayer *child = parseLayer(layerInfo, mapInfo);
  148. if (child == nullptr) {
  149. idx++;
  150. continue;
  151. }
  152. addChild(child, idx, idx);
  153. // update content size with the max size
  154. const Size& childSize = child->getContentSize();
  155. Size currentSize = this->getContentSize();
  156. currentSize.width = std::max(currentSize.width, childSize.width);
  157. currentSize.height = std::max(currentSize.height, childSize.height);
  158. this->setContentSize(currentSize);
  159. idx++;
  160. }
  161. }
  162. _tmxLayerNum = idx;
  163. }
  164. // public
  165. TMXLayer * TMXTiledMap::getLayer(const std::string& layerName) const
  166. {
  167. CCASSERT(layerName.size() > 0, "Invalid layer name!");
  168. for (auto& child : _children)
  169. {
  170. TMXLayer* layer = dynamic_cast<TMXLayer*>(child);
  171. if(layer)
  172. {
  173. if(layerName.compare( layer->getLayerName()) == 0)
  174. {
  175. return layer;
  176. }
  177. }
  178. }
  179. // layer not found
  180. return nullptr;
  181. }
  182. TMXObjectGroup * TMXTiledMap::getObjectGroup(const std::string& groupName) const
  183. {
  184. CCASSERT(groupName.size() > 0, "Invalid group name!");
  185. for (const auto objectGroup : _objectGroups)
  186. {
  187. if (objectGroup && objectGroup->getGroupName() == groupName)
  188. {
  189. return objectGroup;
  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. bool TMXTiledMap::getPropertiesForGID(int GID, Value** value)
  208. {
  209. if (_tileProperties.find(GID) != _tileProperties.end()) {
  210. *value = &_tileProperties.at(GID);
  211. return true;
  212. } else {
  213. return false;
  214. }
  215. }
  216. std::string TMXTiledMap::getDescription() const
  217. {
  218. return StringUtils::format("<TMXTiledMap | Tag = %d, Layers = %d", _tag, static_cast<int>(_children.size()));
  219. }
  220. int TMXTiledMap::getLayerNum()
  221. {
  222. return _tmxLayerNum;
  223. }
  224. NS_CC_END