PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/cocos2d/cocos/2d/CCFastTMXLayer.h

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