CCFastTMXLayer.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /****************************************************************************
  2. Copyright (c) 2008-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. #ifndef __CC_FAST_TMX_LAYER_H__
  25. #define __CC_FAST_TMX_LAYER_H__
  26. #include <map>
  27. #include <unordered_map>
  28. #include "2d/CCNode.h"
  29. #include "2d/CCTMXXMLParser.h"
  30. #include "renderer/CCPrimitiveCommand.h"
  31. #include "base/CCMap.h"
  32. NS_CC_BEGIN
  33. class TMXMapInfo;
  34. class TMXLayerInfo;
  35. class TMXTilesetInfo;
  36. class Texture2D;
  37. class Sprite;
  38. struct _ccCArray;
  39. namespace experimental{
  40. /**
  41. * @addtogroup _2d
  42. * @{
  43. */
  44. /** @brief FastTMXLayer represents the TMX layer.
  45. * It is a subclass of SpriteBatchNode. By default the tiles are rendered using a TextureAtlas.
  46. * If you modify a tile on runtime, then, that tile will become a Sprite, otherwise no Sprite objects are created.
  47. * The benefits of using Sprite objects as tiles are:
  48. * - tiles (Sprite) can be rotated/scaled/moved with a nice API.
  49. * If the layer contains a property named "cc_vertexz" with an integer (in can be positive or negative),
  50. * then all the tiles belonging to the layer will use that value as their OpenGL vertex Z for depth.
  51. * On the other hand, if the "cc_vertexz" property has the "automatic" value, then the tiles will use an automatic vertex Z value.
  52. * Also before drawing the tiles, GL_ALPHA_TEST will be enabled, and disabled after drawing them. The used alpha func will be:
  53. * glAlphaFunc( GL_GREATER, value ).
  54. * "value" by default is 0, but you can change it from Tiled by adding the "cc_alpha_func" property to the layer.
  55. * The value 0 should work for most cases, but if you have tiles that are semi-transparent, then you might want to use a different
  56. * value, like 0.5.
  57. * For further information, please see the programming guide:
  58. * http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps
  59. * @since v3.2
  60. * @js NA
  61. */
  62. class CC_DLL TMXLayer : public Node
  63. {
  64. public:
  65. /** Creates a FastTMXLayer with an tileset info, a layer info and a map info.
  66. *
  67. * @param tilesetInfo An tileset info.
  68. * @param layerInfo A layer info.
  69. * @param mapInfo A map info.
  70. * @return Return an autorelease object.
  71. */
  72. static TMXLayer * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  73. /**
  74. * @js ctor
  75. */
  76. TMXLayer();
  77. /**
  78. * @js NA
  79. * @lua NA
  80. */
  81. virtual ~TMXLayer();
  82. /** Returns the tile gid at a given tile coordinate. It also returns the tile flags.
  83. *
  84. * @param tileCoordinate The tile coordinate.
  85. * @param flags A TMXTileFlags.
  86. * @return The tile gid at a given tile coordinate. It also returns the tile flags.
  87. */
  88. int getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr);
  89. /** Sets the tile gid (gid = tile global id) at a given tile coordinate.
  90. * The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
  91. * If a tile is already placed at that position, then it will be removed.
  92. * @param gid The gid value.
  93. * @param tileCoordinate The tile coordinate.
  94. */
  95. void setTileGID(int gid, const Vec2& tileCoordinate);
  96. /** Sets the tile gid (gid = tile global id) at a given tile coordinate.
  97. * The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
  98. * If a tile is already placed at that position, then it will be removed.
  99. * Use withFlags if the tile flags need to be changed as well.
  100. *
  101. * @param gid A integer value,it will be sets the tile gid.
  102. * @param tileCoordinate The tile coordinate.
  103. * @param flags A TMXTileFlags.
  104. */
  105. void setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags);
  106. /** Removes a tile at given tile coordinate.
  107. *
  108. * @param tileCoordinate The tile Coordinate.
  109. */
  110. void removeTileAt(const Vec2& tileCoordinate);
  111. /** Returns the position in points of a given tile coordinate.
  112. *
  113. * @param tileCoordinate The tile Coordinate.
  114. * @return The position in points of a given tile coordinate.
  115. */
  116. Vec2 getPositionAt(const Vec2& tileCoordinate);
  117. /** Return the value for the specific property name.
  118. *
  119. * @param propertyName The value for the specific property name.
  120. * @return The value for the specific property name.
  121. */
  122. Value getProperty(const std::string& propertyName) const;
  123. /** Creates the tiles. */
  124. void setupTiles();
  125. /** Get the tile layer name.
  126. *
  127. * @return The tile layer name.
  128. */
  129. const std::string& getLayerName() { return _layerName; }
  130. /** Set the tile layer name.
  131. *
  132. * @param layerName The new layer name.
  133. */
  134. void setLayerName(const std::string& layerName) { _layerName = layerName; }
  135. /** Size of the layer in tiles.
  136. *
  137. * @return Size of the layer in tiles.
  138. */
  139. const Size& getLayerSize() const { return _layerSize; }
  140. /** Set the size of the layer in tiles.
  141. *
  142. * @param size The new size of the layer in tiles.
  143. */
  144. void setLayerSize(const Size& size) { _layerSize = size; }
  145. /** Size of the map's tile (could be different from the tile's size).
  146. *
  147. * @return Size of the map's tile (could be different from the tile's size).
  148. */
  149. const Size& getMapTileSize() const { return _mapTileSize; }
  150. /** Set the size of the map's tile.
  151. *
  152. * @param size The new size of the map's tile.
  153. */
  154. void setMapTileSize(const Size& size) { _mapTileSize = size; }
  155. /** Pointer to the map of tiles.
  156. * @js NA
  157. * @lua NA
  158. * @return The pointer to the map of tiles.
  159. */
  160. const uint32_t* getTiles() const { return _tiles; };
  161. /** Set the pointer to the map of tiles.
  162. *
  163. * @param tiles The pointer to the map of tiles.
  164. */
  165. void setTiles(uint32_t* tiles) { _tiles = tiles; _quadsDirty = true;};
  166. /** Tileset information for the layer.
  167. *
  168. * @return Tileset information for the layer.
  169. */
  170. TMXTilesetInfo* getTileSet() const { return _tileSet; }
  171. /** Set the tileset information for the layer.
  172. *
  173. * @param info The new tileset information for the layer.
  174. */
  175. void setTileSet(TMXTilesetInfo* info) {
  176. CC_SAFE_RETAIN(info);
  177. CC_SAFE_RELEASE(_tileSet);
  178. _tileSet = info;
  179. }
  180. /** Layer orientation, which is the same as the map orientation.
  181. *
  182. * @return Layer orientation, which is the same as the map orientation.
  183. */
  184. int getLayerOrientation() const { return _layerOrientation; }
  185. /** Set Layer orientation, which is the same as the map orientation.
  186. *
  187. * @param orientation Layer orientation, which is the same as the map orientation.
  188. */
  189. void setLayerOrientation(int orientation) { _layerOrientation = orientation; }
  190. /** Properties from the layer. They can be added using Tiled.
  191. *
  192. * @return Properties from the layer. They can be added using Tiled.
  193. */
  194. const ValueMap& getProperties() const { return _properties; }
  195. /** Properties from the layer. They can be added using Tiled.
  196. *
  197. * @return Properties from the layer. They can be added using Tiled.
  198. */
  199. ValueMap& getProperties() { return _properties; }
  200. /** Set the properties to the layer.
  201. *
  202. * @param properties The properties to the layer.
  203. */
  204. void setProperties(const ValueMap& properties)
  205. {
  206. _properties = properties;
  207. }
  208. /** Returns the tile (Sprite) at a given a tile coordinate.
  209. * The returned Sprite will be already added to the TMXLayer. Don't add it again.
  210. * The Sprite can be treated like any other Sprite: rotated, scaled, translated, opacity, color, etc.
  211. * You can remove either by calling:
  212. * - layer->removeChild(sprite, cleanup);
  213. *
  214. * @return Returns the tile (Sprite) at a given a tile coordinate.
  215. */
  216. Sprite* getTileAt(const Vec2& tileCoordinate);
  217. /** Set an sprite to the tile,with the tile coordinate and gid.
  218. *
  219. * @param sprite A Sprite.
  220. * @param pos The tile coordinate.
  221. * @param gid The tile gid.
  222. */
  223. void setupTileSprite(Sprite* sprite, const Vec2& pos, uint32_t gid);
  224. //
  225. // Override
  226. //
  227. virtual std::string getDescription() const override;
  228. virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags) override;
  229. void removeChild(Node* child, bool cleanup = true) override;
  230. protected:
  231. bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
  232. void updateTiles(const Rect& culledRect);
  233. Vec2 calculateLayerOffset(const Vec2& offset);
  234. /* The layer recognizes some special properties, like cc_vertexz */
  235. void parseInternalProperties();
  236. Mat4 tileToNodeTransform();
  237. Rect tileBoundsForClipTransform(const Mat4 &tileToClip);
  238. int getVertexZForPos(const Vec2& pos);
  239. //Flip flags is packed into gid
  240. void setFlaggedTileGIDByIndex(int index, uint32_t gid);
  241. //
  242. void updateTotalQuads();
  243. void onDraw(Primitive* primitive);
  244. int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; }
  245. void updateVertexBuffer();
  246. void updateIndexBuffer();
  247. void updatePrimitives();
  248. protected:
  249. //! name of the layer
  250. std::string _layerName;
  251. /** size of the layer in tiles */
  252. Size _layerSize;
  253. /** size of the map's tile (could be different from the tile's size) */
  254. Size _mapTileSize;
  255. /** pointer to the map of tiles */
  256. uint32_t* _tiles;
  257. /** Tileset information for the layer */
  258. TMXTilesetInfo* _tileSet;
  259. /** Layer orientation, which is the same as the map orientation */
  260. int _layerOrientation;
  261. /** properties from the layer. They can be added using Tiled */
  262. ValueMap _properties;
  263. Texture2D *_texture;
  264. /** container for sprite children. map<index, pair<sprite, gid> > */
  265. std::map<int, std::pair<Sprite*, int> > _spriteContainer;
  266. //GLuint _buffersVBO; //0: vertex, 1: indices
  267. Size _screenGridSize;
  268. Rect _screenGridRect;
  269. int _screenTileCount;
  270. int _vertexZvalue;
  271. bool _useAutomaticVertexZ;
  272. /** tile coordinate to node coordinate transform */
  273. Mat4 _tileToNodeTransform;
  274. /** data for rendering */
  275. bool _quadsDirty;
  276. std::vector<int> _tileToQuadIndex;
  277. std::vector<V3F_C4B_T2F_Quad> _totalQuads;
  278. #ifdef CC_FAST_TILEMAP_32_BIT_INDICES
  279. std::vector<GLuint> _indices;
  280. #else
  281. std::vector<GLushort> _indices;
  282. #endif
  283. std::map<int/*vertexZ*/, int/*offset to _indices by quads*/> _indicesVertexZOffsets;
  284. std::unordered_map<int/*vertexZ*/, int/*number to quads*/> _indicesVertexZNumber;
  285. std::vector<PrimitiveCommand> _renderCommands;
  286. bool _dirty;
  287. VertexBuffer* _vertexBuffer;
  288. VertexData* _vData;
  289. IndexBuffer* _indexBuffer;
  290. Map<int , Primitive*> _primitives;
  291. public:
  292. /** Possible orientations of the TMX map */
  293. static const int FAST_TMX_ORIENTATION_ORTHO;
  294. static const int FAST_TMX_ORIENTATION_HEX;
  295. static const int FAST_TMX_ORIENTATION_ISO;
  296. };
  297. // end of tilemap_parallax_nodes group
  298. /// @}
  299. } //end of namespace experimental
  300. NS_CC_END
  301. #endif //__CCTMX_LAYER2_H__