Learning notes for Cocos2d-x (13) Details on carbon label
Preface in the first two notes, we learned about CCMen and CCMenuItem. During the course, we met CCMenItemLabel and the other two classes derived from it. In fact, there is no CCMenItemLabel, however, we still use it as the common name of all CCLabelXXX. Through Automatic completion, we can find that there are three kinds of csvabelxxx. CCLabelProtocol is their common derived class, which is our ideal CCLabel.
CCLabelProtocol
class CC_DLL CCLabelProtocol{public: /** * Sets a new label using an string * * @param A null terminated string */ virtual void setString(const char *label) = 0; /** * Returns the string that is currently being used in this label * * @return The string that is currently being used in this label */ virtual const char* getString(void) = 0;};
CCLabelTTF
Class CC_DLL CCLabelTTF: public CCSprite, public CCLabelProtocol {public: CCLabelTTF (); virtual ~ CCLabelTTF (); const char * description (); /*** create ** @ param string text content * @ param fontName font name * @ param fontSize font size * @ param hAlignment horizontal alignment * @ param vAlignment vertical alignment *@ param textDefinition font description **/static CCLabelTTF * create (const char * string, const char * fontName, float fontSize); static CCLabelTTF * create (const char * string, const char * fontName, float fontSize, const CCSize & dimensions, CCTextAlignment hAlignment ); static CCLabelTTF * create (const char * string, const char * fontName, float fontSize, const CCSize & dimensions, CCTextAlignment hAlignment, CCVerticalTextAlignment vAlignment); static CCLabelTTF * contains (const char * string, ccFontDefinition & textDefinition); // initialize bool initWithString (const char * string, const char * fontName, float fontSize); bool initWithString (const char * string, const char * fontName, float fontSize, const CCSize & dimensions, CCTextAlignment hAlignment); bool values (const char * string, const char * fontName, float fontSize, const CCSize & dimensions, CCTextAlignment hAlignment, and CCVerticalTextAlignment vAlignment ); bool definition (const char * string, ccFontDefinition & textDefinition); // you can set and obtain TextDefinition void setTextDefinition (optional * theDefinition); ccFontDefinition * getTextDefinition (); // set the text shadow void enableShadow (const CCSize & shadowOffset, float shadowOpacity, float shadowBlur, bool watermark = true); // cancel the text shadow void disableShadow (bool mustUpdateTexture = true ); // set the stroke void enableStroke (const ccColor3B & strokeColor, float strokeSize, bool mustUpdateTexture = true); // cancel the stroke void disableStroke (bool mustUpdateTexture = true ); // set the text color void setFontFillColor (const ccColor3B & tintColor, bool mustUpdateTexture = true); // initialize CCLabelTTF bool init (); // create an empty CCLabelTTF static CCLabelTTF * create (); // set/obtain the text virtual void setString (const char * label); virtual const char * getString (void ); // obtain/set the horizontal alignment ment (); void align (CCTextAlignment alignment); // obtain/set the Vertical alignment Using getverticalignment (); void align (precise verticalignment ); CCSize getDimensions (); void setDimensions (const CCSize & dim); // set and obtain the font size float getFontSize (); void setFontSize (float fontSize ); // set and obtain the font name const char * getFontName (); void setFontName (const char * fontName); private: // update the texture bool updateTexture (); protected: /** set the text definition for this label */void _ extract (watermark & textDefinition, bool mustUpdateTexture = true); ccFontDefinition _ prepareTextDefinition (bool adjustForResolution = false ); /** Dimensions of the label in Points */CCSize m_tDimensions; // horizontal aligment CCTextAlignment m_hAlignment; // vertical alignment CCVerticalTextAlignment m_vAlignment; // the font name std :: string * m_pFontName; // tag font size float m_fFontSize; // tag text std: string m_string; // attributes related to text shadow bool m_shadowEnabled; CCSize m_shadowOffset; float m_shadowOpacity; float m_shadowBlur; // bool m_strokeEnabled; ccColor3B m_strokeColor; float m_strokeSize;/** font tint */ccColor3B m_textFillColor ;};CCLabelTTF is a standard text container class that contains common methods such as text font, size, direction, shadow, and stroke. MustUpdateTexture can be seen everywhere. It is not difficult to prove that the modification to the CCLabelTTF requires real-time texture refresh and re-rendering.
CCLabelAtlas
Class CC_DLL CCLabelAtlas: public CCAtlasNode, public CCLabelProtocol {public: CCLabelAtlas (): m_sString () {} virtual ~ CCLabelAtlas () {m_sString.clear ();} /*** create CCLabelAtlas ** @ param string text content * @ param charMapFile character set file * @ param itemWidth width of each character * @ param itemHeight of each character * @ param startCharMap Character Set the first character */static CCLabelAtlas * create (const char * string, const char * charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap); static CCLabelAtlas * create (const char * string, const char * fntFile ); // initialize bool initWithString (const char * string, const char * charMapFile, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap); bool initWithString (const char * string, const char * fntFile); bool initWithString (const char * string, CCTexture2D * texture, unsigned int itemWidth, unsigned int itemHeight, unsigned int startCharMap ); // super methods virtual void updateAtlasValues (); virtual void setString (const char * label); virtual const char * getString (void); # if CC_LABELATLAS_DEBUG_DRAW virtual void draw (); # endifprotected: std: string m_sString; // The text unsigned int m_uMapStartChar; // the first character in the character set };
CCLabelBMFont
Class CC_DLL CCLabelBMFont: public CCSpriteBatchNode, public CCLabelProtocol, public CCRGBAProtocol {public: CCLabelBMFont (); virtual ~ CCLabelBMFont (); // release the memory occupied by the text static void purgeCachedData (); /*** create CCLabelBMFont ** @ param str text * @ param fntFile font file path * @ param width font width of each row * @ param alignment * @ param imageOffset interval between texts * /static CCLabelBMFont * create (const char * str, const char * fntFile, float width, CCTextAlignment alignment, CCPoint imageOffset); static CCLabelBMFont * create (const char * str, const char * fntFile, float width, CCTextAlignment alignment ); static CCLabelBMFont * create (const char * str, const char * fntFile, float width); static CCLabelBMFont * create (const char * str, const char * fntFile ); static CCLabelBMFont * create (); // initialize bool init (); bool initWithString (const char * str, const char * fntFile, float width = kCCLabelAutomaticWidth, CCTextAlignment alignment = timeout, CCPoint imageOffset = CCPointZero);/** updates the font chars based on the string to render */void createFontChars (); // super method virtual void setString (const char * newString ); virtual void setString (const char * newString, bool needUpdateLabel); virtual const char * getString (void); virtual void setCString (const char * label ); // sets the virtual void setAnchorPoint (const CCPoint & var); // updates the Label virtual void updateLabel (); // sets the alignment of virtual void setAlignment (CCTextAlignment alignment ); // set the width of each row to virtual void setWidth (float width); virtual void setLineBreakWithoutSpace (bool breakWithoutSpace); // set the scale to virtual void setScale (float scale ); virtual void setScaleX (float scaleX); virtual void setScaleY (float scaleY); // CCRGBAProtocol virtual bool weight (); virtual void setOpacityModifyRGB (bool weight); virtual GLubyte getOpacity (); virtual GLubyte getDisplayedOpacity (); virtual void setOpacity (GLubyte opacity); virtual void priority (GLubyte parentOpacity); virtual bool priority (); virtual void priority (bool priority ); virtual const ccColor3B & getColor (void); virtual const ccColor3B & getDisplayedColor (); virtual void setColor (const ccColor3B & color); virtual void updateDisplayedColor (const ccColor3B & parentColor ); virtual bool isCascadeColorEnabled (); virtual void setCascadeColorEnabled (bool cascadeColorEnabled); // set/obtain the font file void setFntFile (const char * fntFile); const char * getFntFile (); // obtain the configuration file CCBMFontConfiguration * getConfiguration () const; # if CC_LABELBMFONT_DEBUG_DRAW virtual void draw (); # endif // character: char * atlasNameFromFntFile (const char * fntFile ); int trim (unsigned short first, unsigned short second); float round (CCSprite * characterSprite); protected: virtual void setString (unsigned short * newString, bool needUpdateLabel); // The Label text content unsigned short * m_sString; // The font file name std: string m_sFntFile; // initial string without line breaks unsigned short * m_sInitialString; std :: string m_sInitialStringUTF8; // alignment CCTextAlignment m_pAlignment; // maximum float m_fWidth of each row; CCBMFontConfiguration * m_pConfiguration; bool adjust; // offset of the texture atlas CCPoint m_tImageOffset; // reused char CCSprite * m_pReusedChar; // texture RGBA GLubyte weight; GLubyte m_cRealOpacity; ccColor3B m_tDisplayedColor; ccColor3B m_tRealColor; bool weight; /** conforms to CCRGBAProtocol protocol */bool m_bIsOpacityModifyRGB ;};
Conclusion 1) CCLabelTTF is a subclass of CCLabelProtocol and a subclass of CCSprite. CCLabelTTF is used to render text labels. You can specify the font. Each time you set the string content, you must recreate the texture and rendering, poor performance (you can view the relevant source code), so we usually use CCLabelAtlas or CCLabelBMFont instead.
2) CCLabelAtlas is not only a subclass of CCLabelProtocol, but also a subclass of CCAtlasNode. CCAtlasNode encapsulates a CCTextureAtlas variable. When CCTextureAtlas initializes an image file, it loads the image to the cache (CCTextureCache. CCLabelAtlas has a high rendering efficiency, but it is too restrictive. It is not as flexible as CCLabelBMFont.
(3) CCLabelBMFont is not only a subclass of CCLabelProtocol, but also a subclass of CCSpriteBatchNode. To create a CCLabelBMFont object, a string and a file in fnt format (Font Library) are required ). CCLabelBMFont * label = CCLabelBMFont: labelWithString (Bitmap Font Atlas, fonts/bitmapFontTest. fnt );
This bitmapFontTest. the fnt file contains the following information: The name of the image (the image contains all the characters you want to draw), the unicode encoding of the characters in the image, the coordinates of the characters in the image, and the width. When initializing the CCLabelBMFont object, the image is added to the cache (CCTextureCache), The fnt file is parsed, and the information in the fnt file is saved to a ccBMFontDef array, the index of the array is charId (the unicode encoding value of the character ). When you draw a string, find the ccBMFontDef information based on the unicode code corresponding to the character, retrieve the image from the cache, and then retrieve the character image of the corresponding region based on the coordinates and width and height of ccBMFontDef, add the character index position in the string as a tag to CCLabelBMFont, because CCLabelBMFont itself is CCSpriteBatchNode, which implements the batch processing rendering wizard and improves the performance.
Reference 1) [cocos2d-x] --- CCLabelProtocol text label details http://blog.csdn.net/qiurisuixiang/article/details/8774634 1) CCLabelTTF is a subclass of CCLabelProtocol and a subclass of CCSprite, CCLabelTTF is used to render text labels, you can specify the font, each time you set the string content, you need to re-create the texture and rendering, and the performance is not good (you can view the relevant source code). Therefore, we usually use CCLabelAtlas or CCLabelBMFont instead.
2) CCLabelAtlas is not only a subclass of CCLabelProtocol, but also a subclass of CCAtlasNode. CCAtlasNode encapsulates a CCTextureAtlas variable. When CCTextureAtlas initializes an image file, it loads the image to the cache (CCTextureCache. CCLabelAtlas has a high rendering efficiency, but it is too restrictive. It is not as flexible as CCLabelBMFont.
(3) CCLabelBMFont is not only a subclass of CCLabelProtocol, but also a subclass of CCSpriteBatchNode. To create a CCLabelBMFont object, a string and a file in fnt format (Font Library) are required ). CCLabelBMFont * label = CCLabelBMFont: labelWithString (Bitmap Font Atlas, fonts/bitmapFontTest. fnt );
The bitmapFontTest. fnt file contains the following information: The name of the corresponding image (the image contains all the characters you want to draw ),
The unicode encoding corresponding to the character, the coordinates of the character in the image, and the width. When initializing the CCLabelBMFont object, the image is added to the cache (CCTextureCache), The fnt file is parsed, and the information in the fnt file is saved to a ccBMFontDef array, the index of the array is charId (the unicode encoding value of the character ). When you draw a string, find the ccBMFontDef information based on the unicode code corresponding to the character, retrieve the image from the cache, and then retrieve the character image of the corresponding region based on the coordinates and width and height of ccBMFontDef, add the character index position in the string as a tag to CCLabelBMFont, because CCLabelBMFont itself is CCSpriteBatchNode, which implements the batch processing rendering wizard and improves the performance.