代码:
/** @brief FastTMXLayer represents the TMX layer. FastTMXLayer代表了TMX层
* It is a subclass of SpriteBatchNode. By default the tiles are rendered using a TextureAtlas.
*他是SpriteBatchNode的一个子类,瓦片的渲染默认使用TextureAtlas(纹理地图集)
* If you modify a tile on runtime, then, that tile will become a Sprite, otherwise no Sprite objects are created.
* 如果你在运行的时候修改了一个瓦片,那么,这个瓦片将变成一个Sprite,否则,将不会创建精灵对象
* The benefits of using Sprite objects as tiles are:
* 将精灵对象用作瓦片的好处如下:
* - 瓦片(精灵)可以被旋转,缩放,移动 通过漂亮的函数接口
* - tiles (Sprite) can be rotated/scaled/moved with a nice API.
* If the layer contains a property named "cc_vertexz" with an integer (in can be positive or negative),
* 如果层包含一个名字为cc_vertexz的整型属性(可以是确定或取消)
* then all the tiles belonging to the layer will use that value as their OpenGL vertex Z for depth.
* 然后,所有属于层的瓦片都将用那个值来表示他们在OpenGL中z轴的深度
* On the other hand, if the "cc_vertexz" property has the "automatic" value, then the tiles will use an automatic vertex Z value.
* 另一方面,如贵哦cc_vertexz属性是自动分配的值,那么,瓦片将使用这个自动分配的Z值
* Also before drawing the tiles, GL_ALPHA_TEST will be enabled, and disabled after drawing them. The used alpha func will be:
* 在绘制瓦片之前,GL_ALPHA_TEST将设置为可用,绘制完成后设置为不可用。使用alpha的函数如下:
* glAlphaFunc( GL_GREATER, value ).
* "value" by default is 0, but you can change it from Tiled by adding the "cc_alpha_func" property to the layer.
* value的默认值是0,但是你可以通过添加cc_alpha_func属性到层来改变这个值
* 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
* 大部分情况下value值应该是0,但是,如果你使用的瓦片是半透明的,那么你或许应该使用一个不同的value ,比如0.5
* value, like 0.5.
* For further information, please see the programming guide:
* http://www.cocos2d-iphone.org/wiki/doku.php/prog_guide:tiled_maps
* @since v3.2
* @js NA
*/
class CC_DLL TMXLayer : public Node
{
public:
/** Creates a FastTMXLayer with an tileset info, a layer info and a map info.
*
* @param tilesetInfo An tileset info.
* @param layerInfo A layer info.
* @param mapInfo A map info.
* @return Reruen an autorelease object.
*/
/// 创建一个TMXLayer
static TMXLayer * create(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
/**
* @js ctor
*/
TMXLayer();
/**
* @js NA
* @lua NA
*/
virtual ~TMXLayer();
/** Returns the tile gid at a given tile coordinate. It also returns the tile flags.
*
* @param tileCoordinate The tile coordinate.
* @param flags A TMXTileFlags.
* @return The tile gid at a given tile coordinate. It also returns the tile flags.
*/
/// 得到一个tile的GID
int getTileGIDAt(const Vec2& tileCoordinate, TMXTileFlags* flags = nullptr);
/** Sets the tile gid (gid = tile global id) at a given tile coordinate.
* The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
* If a tile is already placed at that position, then it will be removed.
* @param gid The gid value.
* @param tileCoordinate The tile coordinate.
*/
/// 设置一个tile的GID
void setTileGID(int gid, const Vec2& tileCoordinate);
/** Sets the tile gid (gid = tile global id) at a given tile coordinate.
* The Tile GID can be obtained by using the method "tileGIDAt" or by using the TMX editor -> Tileset Mgr +1.
* If a tile is already placed at that position, then it will be removed.
* Use withFlags if the tile flags need to be changed as well.
*
* @param gid A integer value,it will be sets the tile gid.
* @param tileCoordinate The tile coordinate.
* @param flags A TMXTileFlags.
*/
/// 设置一个tile的GID
void setTileGID(int gid, const Vec2& tileCoordinate, TMXTileFlags flags);
/** Removes a tile at given tile coordinate.
*
* @param tileCoordinate The tile Coordinate.
*/
/// 清除瓦片
void removeTileAt(const Vec2& tileCoordinate);
/** Returns the position in points of a given tile coordinate.
*
* @param tileCoordinate The tile Coordinate.
* @return The position in points of a given tile coordinate.
*/
/// 返回在给定的瓦片坐标系中的坐标
Vec2 getPositionAt(const Vec2& tileCoordinate);
/** Return the value for the specific property name.
*
* @param propertyName The value for the specific property name.
* @return The value for the specific property name.
*/
/// 得到属性
Value getProperty(const std::string& propertyName) const;
/** Creates the tiles. */
/// 创建瓦片
void setupTiles();
/** Get the tile layer name.
*
* @return The tile layer name.
*/
/// 得到层的名字
inline const std::string& getLayerName(){ return _layerName; }
/** Set the tile layer name.
*
* @param layerName The new layer name.
*/
/// 设置瓦片层的名字
inline void setLayerName(const std::string& layerName){ _layerName = layerName; }
/** Size of the layer in tiles.
*
* @return Size of the layer in tiles.
*/
/// 得到层的尺寸
inline const Size& getLayerSize() const { return _layerSize; };
/** Set the size of the layer in tiles.
*
* @param size The new size of the layer in tiles.
*/
/// 设置层的尺寸
inline void setLayerSize(const Size& size) { _layerSize = size; };
/** Size of the map's tile (could be different from the tile's size).
*
* @return Size of the map's tile (could be different from the tile's size).
*/
/// 得到地图中瓦片的尺寸
inline const Size& getMapTileSize() const { return _mapTileSize; };
/** Set the size of the map's tile.
*
* @param size The new size of the map's tile.
*/
/// 设置地图中瓦片的尺寸
inline void setMapTileSize(const Size& size) { _mapTileSize = size; };
/** Pointer to the map of tiles.
* @js NA
* @lua NA
* @return The pointer to the map of tiles.
*/
// 得到瓦片的指针
const uint32_t* getTiles() const { return _tiles; };
/** Set the pointer to the map of tiles.
*
* @param tiles The pointer to the map of tiles.
*/
/// 设置瓦片的指针
void setTiles(uint32_t* tiles) { _tiles = tiles; _quadsDirty = true;};
/** Tileset information for the layer.
*
* @return Tileset information for the layer.
*/
/// 得到层的信息
inline TMXTilesetInfo* getTileSet() const { return _tileSet; };
/** Set the tileset information for the layer.
*
* @param info The new tileset information for the layer.
*/
/// 设置层的信息
inline void setTileSet(TMXTilesetInfo* info) {
CC_SAFE_RETAIN(info);
CC_SAFE_RELEASE(_tileSet);
_tileSet = info;
};
/** Layer orientation, which is the same as the map orientation.
*
* @return Layer orientation, which is the same as the map orientation.
*/
/// 得到层的方向
inline int getLayerOrientation() const { return _layerOrientation; };
/** Set Layer orientation, which is the same as the map orientation.
*
* @param orientation Layer orientation, which is the same as the map orientation.
*/
/// 设置层的方向
inline void setLayerOrientation(int orientation) { _layerOrientation = orientation; };
/** Properties from the layer. They can be added using Tiled.
*
* @return Properties from the layer. They can be added using Tiled.
*/
/// 得到属性MAP
inline const ValueMap& getProperties() const { return _properties; };
/** Properties from the layer. They can be added using Tiled.
*
* @return Properties from the layer. They can be added using Tiled.
*/
/// 得到属性MAP
inline ValueMap& getProperties() { return _properties; };
/** Set the properties to the layer.
*
* @param properties The properties to the layer.
*/
/// 设置属性MAP
inline void setProperties(const ValueMap& properties)
{
_properties = properties;
};
/** Returns the tile (Sprite) at a given a tile coordinate.
* The returned Sprite will be already added to the TMXLayer. Don't add it again.
* The Sprite can be treated like any other Sprite: rotated, scaled, translated, opacity, color, etc.
* You can remove either by calling:
* - layer->removeChild(sprite, cleanup);
*
* @return Returns the tile (Sprite) at a given a tile coordinate.
*/
/// 得到对应坐标的瓦片
Sprite* getTileAt(const Vec2& tileCoordinate);
/** Set an sprite to the tile,with the tile coordinate and gid.
*
* @param sprite A Sprite.
* @param pos The tile coordinate.
* @param gid The tile gid.
*/
/// 设置对应坐标的瓦片
void setupTileSprite(Sprite* sprite, Vec2 pos, int gid);
//
// Override
//
virtual std::string getDescription() const override;
virtual void draw(Renderer *renderer, const Mat4& transform, uint32_t flags) override;
void removeChild(Node* child, bool cleanup = true) override;
protected:
/// 初始化
bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
/// 更新瓦片
void updateTiles(const Rect& culledRect);
/// 计算层的偏移量
Vec2 calculateLayerOffset(const Vec2& offset);
/* The layer recognizes some special properties, like cc_vertez */
/// 解析内部的属性
void parseInternalProperties();
/// 瓦片节点的变换
Mat4 tileToNodeTransform();
Rect tileBoundsForClipTransform(const Mat4 &tileToClip);
int getVertexZForPos(const Vec2& pos);
//Flip flags is packed into gid
void setFlaggedTileGIDByIndex(int index, int gid);
//
void updateTotalQuads();
void onDraw(Primitive* primitive);
inline int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; }
void updateVertexBuffer();
void updateIndexBuffer();
void updatePrimitives();
protected:
//! name of the layer
std::string _layerName;
/** size of the layer in tiles */
Size _layerSize;
/** size of the map's tile (could be different from the tile's size) */
Size _mapTileSize;
/** pointer to the map of tiles */
uint32_t* _tiles;
/** Tileset information for the layer */
TMXTilesetInfo* _tileSet;
/** Layer orientation, which is the same as the map orientation */
int _layerOrientation;
/** properties from the layer. They can be added using Tiled */
ValueMap _properties;
Texture2D *_texture;
/** container for sprite children. map<index, pair<sprite, gid> > */
std::map<int, std::pair<Sprite*, int> > _spriteContainer;
//GLuint _buffersVBO; //0: vertex, 1: indices
Size _screenGridSize;
Rect _screenGridRect;
int _screenTileCount;
int _vertexZvalue;
bool _useAutomaticVertexZ;
/** tile coordinate to node coordinate transform */
Mat4 _tileToNodeTransform;
/** data for rendering */
bool _quadsDirty;
std::vector<int> _tileToQuadIndex;
std::vector<V3F_C4B_T2F_Quad> _totalQuads;
std::vector<GLushort> _indices;
std::map<int/*vertexZ*/, int/*offset to _indices by quads*/> _indicesVertexZOffsets;
std::unordered_map<int/*vertexZ*/, int/*number to quads*/> _indicesVertexZNumber;
std::vector<PrimitiveCommand> _renderCommands;
bool _dirty;
VertexBuffer* _vertexBuffer;
VertexData* _vData;
IndexBuffer* _indexBuffer;
Map<int , Primitive*> _primitives;
public:
/** Possible orientations of the TMX map */
static const int FAST_TMX_ORIENTATION_ORTHO;
static const int FAST_TMX_ORIENTATION_HEX;
static const int FAST_TMX_ORIENTATION_ISO;
};