-----> 文字
CCLabelTTF
CCLabelTTF对象渲染比较慢,每次调用setString的时候,一个新的OpenGL纹理将被创建,这意味着setString和创建一个新的标签一样慢,所以当需要频繁更新label的时候,应该选择另外两种文字类。
CCLabelBMFont 可以表现任意包括中文(精灵纹理 位图字体)
它是CCSpriteSheet的一个子类,对每一个字符就像一个CCSprite来对待,因此每个单独的字符可以进行旋转、缩放、设置透明等操作。
使用该类之前,需要添加好字体文件,包括一个图片文件(AA.png)和一个字体坐标文件(AA.fnt)。两个文件的名称相同,只是扩展名不同。
CCLabelBMFont的改变相当于每次只改变图片坐标。
CCLabelBMFont中每个文字相当于一个CCSprite,可以旋转,移动,改变尺寸等
绘制字符串时,根据字符对应的unicode码去查找ccBMFontDef信息,从缓存中取出图片,再根据ccBMFontDef中坐标、宽高取出对应区域的字符图片,把字符在字符串中的索引位置作为tag添加到CCLabelBMFont中,因为CCLabelBMFont本身是CCSpriteBatchNode,这样就实现了批处理渲染精灵,提高了性能。
CCLabelAtlas 只可表现256个字符
这个类会将传入的图片“分成”指定宽高的小块,根据需要获得字符的ASCII与startCharMap指定的图中的第一个字符的ASCII计算偏移量,取得对应的小块。这就需要图片中的字符是按照ASCII的顺序连续排列的。
CCLabelAtlas:也是CCAtlasNode的子类,参数的含义:要绘制的字符,图片文件,图片文件中每个字符的宽度,图片文件中每个字符的高度,图片的起始字符。
#ifndef __CCATLAS_NODE_H__
#define __CCATLAS_NODE_H__
#include "CCNode.h"
#include "CCProtocols.h"
#include "ccTypes.h"
NS_CC_BEGIN
class CCTextureAtlas;
1.COCos2d-X 节点(CCAtlasNode.h)API AtlasNode 是 Node 的子类,实现了 RGBAProtocol 、 TextureProtocol 协议
节点的所有功能都有效,再加上以下功能:
- opacity and RGB colors //不透明度和RGB颜色
/** @brief CCAtlasNode is a subclass(子集) of CCNode that implements(工具) the CCRGBAProtocol and CCTextureProtocol protocol(协议)
It knows how to render a TextureAtlas object.
If you are going to render a TextureAtlas consider(考虑) subclassing CCAtlasNode (or a subclass of CCAtlasNode)
All features from CCNode are valid(有效地), plus the following features:
- opacity and RGB colors
*/
class CC_DLL CCAtlasNode : public CCNodeRGBA, public CCTextureProtocol
{
protected:
//! chars per row
unsigned int m_uItemsPerRow; //每行字符数
//! chars per column
unsigned int m_uItemsPerColumn; //每列字符数
//! width of each char
unsigned int m_uItemWidth; //每个字符width
//! height of each char
unsigned int m_uItemHeight; //每个字符height
ccColor3B m_tColorUnmodified;
CC_PROPERTY(CCTextureAtlas*, m_pTextureAtlas, TextureAtlas); // CCTextureAtlas get set方法
// protocol variables
bool m_bIsOpacityModifyRGB;
CC_PROPERTY(ccBlendFunc, m_tBlendFunc, BlendFunc); //gl的 blendfunc接口
// quads to draw
CC_PROPERTY(unsigned int, m_uQuadsToDraw, QuadsToDraw); //渲染接口
// color uniform
GLint m_nUniformColor;
// This varible is only used for CCLabelAtlas FPS display. So plz don't modify its value.
bool m_bIgnoreContentScaleFactor;
public:
CCAtlasNode();
virtual ~CCAtlasNode();
/** creates a CCAtlasNode with an Atlas file the width and height of each item and the quantity of items to render*/
static CCAtlasNode * create(const char* tile,unsigned int tileWidth, unsigned int tileHeight,
unsigned int itemsToRender);
/** initializes an CCAtlasNode with an Atlas file the width and height of each item and the quantity of items to render*/
bool initWithTileFile(const char* tile, unsigned int tileWidth, unsigned int tileHeight, unsigned int itemsToRender);
/** initializes an CCAtlasNode with a texture the width and height of each item measured in points and the quantity of items to render*/
bool initWithTexture(CCTexture2D* texture, unsigned int tileWidth, unsigned int tileHeight, unsigned int itemsToRender);
/** updates the Atlas (indexed vertex array).
* Shall be overridden in subclasses
*/
virtual void updateAtlasValues();
virtual void draw(void);
// CC Texture protocol
/** returns the used texture*/
virtual CCTexture2D* getTexture(void);
/** sets a new texture. it will be retained*/
virtual void setTexture(CCTexture2D *texture);
virtual bool isOpacityModifyRGB();
virtual void setOpacityModifyRGB(bool isOpacityModifyRGB);
virtual const ccColor3B& getColor(void);
virtual void setColor(const ccColor3B& color);
virtual void setOpacity(GLubyte opacity);
private :
void calculateMaxItems(); //最大字符数
void updateBlendFunc();
void updateOpacityModifyRGB();
friend class CCDirector;
void setIgnoreContentScaleFactor(bool bIgnoreContentScaleFactor);
};
// end of base_node group
/// @}
NS_CC_END
#endif // __CCATLAS_NODE_H__