当前位置:编程学习 > wap >>

cocos2d实现CCLabelTTF真正字体描边效果

在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。

 

 

 

 

[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 initWithString(const char *label, const char *fontName, float fontSize, float fStrokeSize, const cocos2d::ccColor3B&      colStroke, cocos2d::CCTextAlignment hAlignment, cocos2d::CCVerticalTextAlignment 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 initWithString(const char *label, const char *fontName, float fontSize, float fStrokeSize, const cocos2d::ccColor3B&      colStroke, cocos2d::CCTextAlignment hAlignment, cocos2d::CCVerticalTextAlignment 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 StrokeLabelTTF::initWithString(const char *string, const char *fontName, float fontSize, float strokeSize,const cocos2d::ccColor3B& colStroke, CCTextAlignment hAlignment,CCVerticalTextAlignment vAlignment) 

    m_fStrokeSize = strokeSize; 
    m_colStroke = colStroke; 
    m_label = CCLabelTTF::create(string, fontName, fontSize, CCSizeZero,hAlignment,vAlignment); 
    m_label->retain(); 
 
    updateStroke(); 
 
    return true; 

StrokeLabelTTF* StrokeLabelTTF::create(const char *string, const char *fontName, float fontSize, float fStrokeSize,const cocos2d::ccColor3B& colStroke ,CCTextAlignment hAlignment,CCVerticalTextAlignment 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,0,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); 补充:移动开发 , 其他 ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,