In the development game, we need to display a clear font outline in the game. We need to draw borders around the font to make the font display more prominent, I have rewritten the cclabelttf class so that it has the special effects of stroke, the size and color of stroke...
[Cpp]
[Cpp]
[Cpp] # include "cocos2d. h"
Using namespace cocos2d;
Class StrokeLabelTTF: public cocos2d: CCNode
{
Public:
StrokeLabelTTF ();
~ StrokeLabelTTF ();
Public:
Static StrokeLabelTTF * create (const char * string, const char * fontName, float fontSize, float strokeSize, const cocos2d: ccColor3B & colStroke = cocos2d: ccc3 (0, 0, 0), cocos2d: CCTextAlignment hAlignment = cocos2d: kCCTextAlignmentCenter, cocos2d: CCVerticalTextAlignment vAlignment = cocos2d: kCCVerticalTextAlignmentTop );
Bool labels (const char * label, const char * fontName, float fontSize, float fStrokeSize, const cocos2d: ccColor3B & colStroke, cocos2d: CCTextAlignment hAlignment, cocos2d: Invalid vAlignment );
Static StrokeLabelTTF * create (const char * string, const char * fontName, float fontSize );
Public:
Void setColor (const cocos2d: ccColor3B & color3 );
Void setString (const char * label );
Void setStrokeColor (cocos2d: ccColor3B col) {m_colStroke = col; updateStroke ();}
Void setStrokeSize (float StrokeSize) {m_fStrokeSize = StrokeSize; updateStroke ();}
Void setAnchorPoint (const cocos2d: CCPoint & anchorPoint );
Protected:
Void updateStroke ();
Private:
Float m_fStrokeSize;
Cocos2d: ccColor3B m_colStroke;
Cocos2d: CCSprite * m_sprite;
Cocos2d: CCLabelTTF * m_label;
CCPoint anchorPoint;
};
# Include "cocos2d. h"
Using namespace cocos2d;
Class StrokeLabelTTF: public cocos2d: CCNode
{
Public:
StrokeLabelTTF ();
~ StrokeLabelTTF ();
Public:
Static StrokeLabelTTF * create (const char * string, const char * fontName, float fontSize, float strokeSize, const cocos2d: ccColor3B & colStroke = cocos2d: ccc3 (0, 0, 0), cocos2d: CCTextAlignment hAlignment = cocos2d: kCCTextAlignmentCenter, cocos2d: CCVerticalTextAlignment vAlignment = cocos2d: kCCVerticalTextAlignmentTop );
Bool labels (const char * label, const char * fontName, float fontSize, float fStrokeSize, const cocos2d: ccColor3B & colStroke, cocos2d: CCTextAlignment hAlignment, cocos2d: Invalid vAlignment );
Static StrokeLabelTTF * create (const char * string, const char * fontName, float fontSize );
Public:
Void setColor (const cocos2d: ccColor3B & color3 );
Void setString (const char * label );
Void setStrokeColor (cocos2d: ccColor3B col) {m_colStroke = col; updateStroke ();}
Void setStrokeSize (float StrokeSize) {m_fStrokeSize = StrokeSize; updateStroke ();}
Void setAnchorPoint (const cocos2d: CCPoint & anchorPoint );
Protected:
Void updateStroke ();
Private:
Float m_fStrokeSize;
Cocos2d: ccColor3B m_colStroke;
Cocos2d: CCSprite * m_sprite;
Cocos2d: CCLabelTTF * m_label;
CCPoint anchorPoint;
};
[Cpp]
# Include "StrokeLabelTTF. h"
USING_NS_CC;
StrokeLabelTTF: StrokeLabelTTF ()
: M_colStroke (ccc3 (0, 0, 0 ))
, M_fStrokeSize (1.0f)
, M_sprite (NULL)
, M_label (NULL)
, AnchorPoint (0.5, 0.5)
{}
StrokeLabelTTF ::~ StrokeLabelTTF ()
{
CC_SAFE_RELEASE_NULL (m_label );
}
Bool labels: initWithString (const char * string, const char * fontName, float fontSize, float strokeSize, const cocos2d: ccColor3B & colStroke, CCTextAlignment hAlignment, invalid vAlignment)
{
M_fStrokeSize = strokeSize;
M_colStroke = colStroke;
M_label = CCLabelTTF: create (string, fontName, fontSize, CCSizeZero, hAlignment, vAlignment );
M_label-> retain ();
UpdateStroke ();
Return true;
}
Optional * values: create (const char * string, const char * fontName, float fontSize, float fStrokeSize, const cocos2d: ccColor3B & colStroke, CCTextAlignment hAlignment, invalid vAlignment)
{
StrokeLabelTTF * pRet = new StrokeLabelTTF ();
If (pRet & pRet-> initWithString (string, fontName, fontSize, fStrokeSize, colStroke, hAlignment, vAlignment ))
{
PRet-> autorelease ();
Return pRet;
}
CC_SAFE_DELETE (pRet );
Return NULL;
}
StrokeLabelTTF * StrokeLabelTTF: create (const char * string, const char * fontName, float fontSize)
{
StrokeLabelTTF * pRet = new StrokeLabelTTF ();
If (pRet & pRet-> initWithString (string, fontName, fontSize, 1.5, ccc3 (, 0), kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop ))
{
PRet-> autorelease ();
Return pRet;
}
CC_SAFE_DELETE (pRet );
Return NULL;
}
Void StrokeLabelTTF: updateStroke ()
{
If (m_sprite)
{
RemoveChild (m_sprite, true );
}
CCSize textureSize = m_label-> getContentSize ();
TextureSize. width + = 2 * m_fStrokeSize;
TextureSize. height + = 2 * m_fStrokeSize;
// Call to clear error
GlGetError ();
CCRenderTexture * rt = CCRenderTexture: create (textureSize. width, textureSize. height );
If (! Rt)
{
CCLOG ("create render texture failed !!!! ");
AddChild (m_label );
Return;
}
CcColor3B col = m_label-> getColor ();
M_label-> setColor (m_colStroke );
CcBlendFunc originalBlend = m_label-> getBlendFunc ();
CcBlendFunc func = {GL_SRC_ALPHA, GL_ONE };
M_label-> setBlendFunc (func );
M_label-> setAnchorPoint (ccp (0.5f, 0.5f ));
Rt-> begin ();
For (int I = 0; I <360; I + = 15)
{
Float r = CC_DEGREES_TO_RADIANS (I );
M_label-> setPosition (ccp (
TextureSize. width * 0.5f + sin (r) * m_fStrokeSize,
TextureSize. height * 0.5f + cos (r) * m_fStrokeSize ));
M_label-> visit ();
}
M_label-> setColor (col );
M_label-> setBlendFunc (originalBlend );
M_label-> setPosition (ccp (textureSize. width * 0.5f, textureSize. height * 0.5f ));
M_label-> visit ();
Rt-> end ();
CCTexture2D * texture = rt-> getSprite ()-> getTexture ();
Texture-> setAliasTexParameters ();
M_sprite = CCSprite: createWithTexture (rt-> getSprite ()-> getTexture ());
SetContentSize (m_sprite-> getContentSize ());
M_sprite-> setAnchorPoint (this-> anchorPoint );//------------
M_sprite-> setPosition (ccp (0, 0 ));
(CCSprite *) m_sprite)-> setFlipY (true );
AddChild (m_sprite );
}
Void StrokeLabelTTF: setString (const char * label)
{
If (m_label)
{
If (0! = Strcmp (label, m_label-> getString ()))
{
M_label-> setString (label );
UpdateStroke ();
}
}
Else
{
CCLOG ("ERROR: CCLabelTTFStroke: setString m_label = NULL ");
}
}
Void StrokeLabelTTF: setColor (const ccColor3B & color3)
{
If (m_label)
{
CcColor3B col = m_label-> getColor ();
If (color3.r! = Col. r | color3.g! = Col. g | color3. B! = Col. B)
{
M_label-> setColor (color3 );
UpdateStroke ();
}
}
Else
{
CCLOG ("ERROR: CCLabelTTFStroke: setColor m_label = NULL ");
}
}
Void StrokeLabelTTF: setAnchorPoint (const cocos2d: CCPoint & anchorPoint1)
{
This-> anchorPoint = anchorPoint1;
UpdateStroke ();
}
# Include "StrokeLabelTTF. h"
USING_NS_CC;
StrokeLabelTTF: StrokeLabelTTF ()
: M_colStroke (ccc3 (0, 0, 0 ))
, M_fStrokeSize (1.0f)
, M_sprite (NULL)
, M_label (NULL)
, AnchorPoint (0.5, 0.5)
{}
StrokeLabelTTF ::~ StrokeLabelTTF ()
{
CC_SAFE_RELEASE_NULL (m_label );
}
Bool labels: initWithString (const char * string, const char * fontName, float fontSize, float strokeSize, const cocos2d: ccColor3B & colStroke, CCTextAlignment hAlignment, invalid vAlignment)
{
M_fStrokeSize = strokeSize;
M_colStroke = colStroke;
M_label = CCLabelTTF: create (string, fontName, fontSize, CCSizeZero, hAlignment, vAlignment );
M_label-> retain ();
UpdateStroke ();
Return true;
}
Optional * values: create (const char * string, const char * fontName, float fontSize, float fStrokeSize, const cocos2d: ccColor3B & colStroke, CCTextAlignment hAlignment, invalid vAlignment)
{
StrokeLabelTTF * pRet = new StrokeLabelTTF ();
If (pRet & pRet-> initWithString (string, fontName, fontSize, fStrokeSize, colStroke, hAlignment, vAlignment ))
{
PRet-> autorelease ();
Return pRet;
}
CC_SAFE_DELETE (pRet );
Return NULL;
}
StrokeLabelTTF * StrokeLabelTTF: create (const char * string, const char * fontName, float fontSize)
{
StrokeLabelTTF * pRet = new StrokeLabelTTF ();
If (pRet & pRet-> initWithString (string, fontName, fontSize, 1.5, ccc3 (, 0), kCCTextAlignmentCenter, kCCVerticalTextAlignmentTop ))
{
PRet-> autorelease ();
Return pRet;
}
CC_SAFE_DELETE (pRet );
Return NULL;
}
Void StrokeLabelTTF: updateStroke ()
{
If (m_sprite)
{
RemoveChild (m_sprite, true );
}
CCSize textureSize = m_label-> getContentSize ();
TextureSize. width + = 2 * m_fStrokeSize;
TextureSize. height + = 2 * m_fStrokeSize;
// Call to clear error
GlGetError ();
CCRenderTexture * rt = CCRenderTexture: create (textureSize. width, textureSize. height );
If (! Rt)
{
CCLOG ("create render texture failed !!!! ");
AddChild (m_label );
Return;
}
CcColor3B col = m_label-> getColor ();
M_label-> setColor (m_colStroke );
CcBlendFunc originalBlend = m_label-> getBlendFunc ();
CcBlendFunc func = {GL_SRC_ALPHA, GL_ONE };
M_label-> setBlendFunc (func );
M_label-> setAnchorPoint (ccp (0.5f, 0.5f ));
Rt-> begin ();
For (int I = 0; I <360; I + = 15)
{
Float r = CC_DEGREES_TO_RADIANS (I );
M_label-> setPosition (ccp (
TextureSize. width * 0.5f + sin (r) * m_fStrokeSize,
TextureSize. height * 0.5f + cos (r) * m_fStrokeSize ));
M_label-> visit ();
}
M_label-> setColor (col );
M_label-> setBlendFunc (originalBlend );
M_label-> setPosition (ccp (textureSize. width * 0.5f, textureSize. height * 0.5f ));
M_label-> visit ();
Rt-> end ();
CCTexture2D * texture = rt-> getSprite ()-> getTexture ();
Texture-> setAliasTexParameters ();
M_sprite = CCSprite: createWithTexture (rt-> getSprite ()-> getTexture ());
SetContentSize (m_sprite-> getContentSize ());
M_sprite-> setAnchorPoint (this-> anchorPoint );//------------
M_sprite-> setPosition (ccp (0, 0 ));
(CCSprite *) m_sprite)-> setFlipY (true );
AddChild (m_sprite );
}
Void StrokeLabelTTF: setString (const char * label)
{
If (m_label)
{
If (0! = Strcmp (label, m_label-> getString ()))
{
M_label-> setString (label );
UpdateStroke ();
}
}
Else
{
CCLOG ("ERROR: CCLabelTTFStroke: setString m_label = NULL ");
}
}
Void StrokeLabelTTF: setColor (const ccColor3B & color3)
{
If (m_label)
{
CcColor3B col = m_label-> getColor ();
If (color3.r! = Col. r | color3.g! = Col. g | color3. B! = Col. B)
{
M_label-> setColor (color3 );
UpdateStroke ();
}
}
Else
{
CCLOG ("ERROR: CCLabelTTFStroke: setColor m_label = NULL ");
}
}
Void StrokeLabelTTF: setAnchorPoint (const cocos2d: CCPoint & anchorPoint1)
{
This-> anchorPoint = anchorPoint1;
UpdateStroke ();
}