cocos2d-x 利用CCLabelTTF实现文字描边


转自:http://fengmm521.blog.163.com/blog/static/2509135820138943638777/  点击打开链接


.h

#ifndef __CCSTROKELABEL_H__
#define __CCSTROKELABEL_H__
#include "cocos2d.h"
#include "label_nodes/CCLabelTTF.h"

class CCStrokeLabel : public cocos2d::CCNode 
{
public:
	void initString();
	static CCStrokeLabel* create(cocos2d::CCLabelTTF* labelTTF,cocos2d::ccColor3B fullColor,cocos2d::ccColor3B StrokeColor,float strokeSize);
	
public:
	float getStrokeSize(){ return m_strokeSize; };
	void setStrokeSize(float strokeSize){ m_strokeSize = strokeSize; };

private:
	cocos2d::CCSprite* m_sprite;
	cocos2d::CCLabelTTF* m_label;
	cocos2d::ccColor3B m_fullColor;
	cocos2d::ccColor3B m_StrokeColor;
	float m_strokeSize;
};

#endif //__CCSTROKELABEL_H__

.cpp

#include "CStrokeLabel.h"
using namespace cocos2d;

CCStrokeLabel* CCStrokeLabel::create(cocos2d::CCLabelTTF* labelTTF,\
									 cocos2d::ccColor3B fullColor,\
									 cocos2d::ccColor3B StrokeColor,\
									 float strokeSize)
{
	CCStrokeLabel* tmp = new CCStrokeLabel;
	tmp->autorelease();
	tmp->m_label = labelTTF;
	tmp->m_fullColor = fullColor;
	tmp->m_StrokeColor = StrokeColor;
	tmp->m_strokeSize = strokeSize;
	tmp->initString();
	return tmp;
}

void CCStrokeLabel::initString()
{
	float strokeSize = getStrokeSize();
	// float fontSize = m_fontSize - 2 * strokeSize;
	//  m_label = CCLabelTTF::labelWithString(
	//   m_str.c_str(), m_font.c_str(), m_fontSize);
	CCSize textureSize = m_label->getContentSize();
	textureSize.width += 2 * strokeSize;
	textureSize.height += 2 * strokeSize;
	//call to clear error
	glGetError();
	CCRenderTexture *rt = CCRenderTexture::create(
		textureSize.width, textureSize.height);
	if(!rt)
	{
		//ERROR_MSG("create render texture failed !!!!");
		addChild(m_label);
		return;
	}

	m_label->setColor(m_fullColor);

	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) * strokeSize,
			textureSize.height * 0.5f + cos(r) * strokeSize));
		m_label->visit();
	}
	m_label->setColor(m_StrokeColor);
	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);
}


使用:

CCLabelTTF* ttf = CCLabelTTF::create("ttf label","Arial",50);
		CCStrokeLabel* ttfStroke = CCStrokeLabel::create(ttf,ccc3(255,0,0),ccc3(0,0,255),2.0f);
		ttfStroke->setPosition(ccp(10,250));
		this->addChild(ttfStroke,200);
		CCBlink* blinkx = CCBlink::create(1.0,5);
		ttfStroke->runAction(blinkx);

		CCLabelTTF* ttfx = CCLabelTTF::create("ttf label test white","Arial",50);
		CCStrokeLabel* ttfStrokeWhite = CCStrokeLabel::create(ttfx,ccc3(0,255,0),ccc3(255,255,255),4.0f);
		ttfStrokeWhite->setPosition(ccp(10,100));
		this->addChild(ttfStrokeWhite,200); 

运行效果:



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值