【cocos2d-x笔记】CCLabelTTF 字体描边

直接贴代码,网上找的,修改过的

#ifndef __CCSTROKELABEL_H__
#define __CCSTROKELABEL_H__
#include "cocos2d.h" 

USING_NS_CC;

class CCStrokeLabel : public cocos2d::CCNode {
public:
	CCStrokeLabel();
	~CCStrokeLabel();
public: 
	static CCStrokeLabel * create(const char *string, const char *fontName, float fontSize); 
	float getStrokeSize();
    bool repaint();
	static CCStrokeLabel* 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);
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();}
protected:
	void updateStroke();
private:
	float                   m_fStrokeSize;
	cocos2d::ccColor3B      m_colStroke;
	cocos2d::CCSprite*      m_sprite;
	cocos2d::CCLabelTTF*    m_label;
};

#endif


 

#include "CCStrokeLabel.h"

using namespace cocos2d;

CCStrokeLabel::CCStrokeLabel()
{

}

CCStrokeLabel::~CCStrokeLabel()
{

}

 CCStrokeLabel* CCStrokeLabel::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*/ )
 {
	 CCStrokeLabel *pRet = new CCStrokeLabel();
	 if(pRet && pRet->initWithString(string, fontName, fontSize, strokeSize, colStroke, hAlignment, vAlignment))
	 {
		 pRet->autorelease();
		 return pRet;
	 }
	 CC_SAFE_DELETE(pRet);

	 return NULL;
 }

 CCStrokeLabel * CCStrokeLabel::create( const char *string, const char *fontName, float fontSize )
 {
	 return create(string,fontName,fontSize,3);
 }

 bool CCStrokeLabel::initWithString( const char *label, const char *fontName, float fontSize, float fStrokeSize, const cocos2d::ccColor3B& colStroke, cocos2d::CCTextAlignment hAlignment, cocos2d::CCVerticalTextAlignment vAlignment )
 {
	 m_fStrokeSize = fStrokeSize;
	 m_colStroke = colStroke;
	 m_label = CCLabelTTF::create(label, fontName, fontSize, CCSizeZero,hAlignment,vAlignment);
	 m_label->retain();
	 updateStroke();
	 return true;
 }

 void CCStrokeLabel::setColor( const cocos2d::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 CCStrokeLabel::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.5, 0.5));
	 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(ccp(0, 0));
	 m_sprite->setPosition(ccp(0, 0));
	 ((CCSprite *)m_sprite)->setFlipY(true);
	 addChild(m_sprite);
 }

 void CCStrokeLabel::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");
	 }
 }


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值