在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。
开发人员:Jason's.Alex QQ:531401335
csdn博客:http://blog.csdn.net/RuShrooM
- #include"cocos2d.h"
- usingnamespacecocos2d;
- classStrokeLabelTTF:publiccocos2d::CCNode
- {
- public:
- StrokeLabelTTF();
- ~StrokeLabelTTF();
- public:
- staticStrokeLabelTTF*create(constchar*string,constchar*fontName,floatfontSize,floatstrokeSize,constcocos2d::ccColor3B&colStroke=cocos2d::ccc3(0,0),cocos2d::CCTextAlignmenthAlignment=cocos2d::kCCTextAlignmentCenter,cocos2d::CCVerticalTextAlignmentvAlignment=cocos2d::kCCVerticalTextAlignmentTop);
- boolinitWithString(constchar*label,floatfStrokeSize,constcocos2d::ccColor3B&colStroke,cocos2d::CCTextAlignmenthAlignment,cocos2d::CCVerticalTextAlignmentvAlignment);
- staticStrokeLabelTTF*create(constchar*string,floatfontSize);
- public:
- voidsetColor(constcocos2d::ccColor3B&color3);
- voidsetString(constchar*label);
- voidsetStrokeColor(cocos2d::ccColor3Bcol){m_colStroke=col;updateStroke();}
- voidsetStrokeSize(floatStrokeSize){m_fStrokeSize=StrokeSize;updateStroke();}
- voidsetAnchorPoint(constcocos2d::CCPoint&anchorPoint);
- protected:
- voidupdateStroke();
- private:
- floatm_fStrokeSize;
- cocos2d::ccColor3Bm_colStroke;
- cocos2d::CCSprite*m_sprite;
- cocos2d::CCLabelTTF*m_label;
- CCPointanchorPoint;
- };
- #include"StrokeLabelTTF.h"
- USING_NS_CC;
- StrokeLabelTTF::StrokeLabelTTF()
- :m_colStroke(ccc3(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);
- }
- boolStrokeLabelTTF::initWithString(constchar*string,CCTextAlignmenthAlignment,CCVerticalTextAlignmentvAlignment)
- {
- m_fStrokeSize=strokeSize;
- m_colStroke=colStroke;
- m_label=CCLabelTTF::create(string,fontName,fontSize,CCSizeZero,hAlignment,vAlignment);
- m_label->retain();
- updateStroke();
- returntrue;
- }
- StrokeLabelTTF*StrokeLabelTTF::create(constchar*string,CCVerticalTextAlignmentvAlignment)
- {
- StrokeLabelTTF*pRet=newStrokeLabelTTF();
- if(pRet&&pRet->initWithString(string,fStrokeSize,colStroke,vAlignment))
- {
- pRet->autorelease();
- returnpRet;
- }
- CC_SAFE_DELETE(pRet);
- returnNULL;
- }
- StrokeLabelTTF*StrokeLabelTTF::create(constchar*string,floatfontSize)
- {
- StrokeLabelTTF*pRet=newStrokeLabelTTF();
- if(pRet&&pRet->initWithString(string,1.5,ccc3(0,kCCTextAlignmentCenter,kCCVerticalTextAlignmentTop))
- {
- pRet->autorelease();
- returnpRet;
- }
- CC_SAFE_DELETE(pRet);
- returnNULL;
- }
- voidStrokeLabelTTF::updateStroke()
- {
- if(m_sprite)
- {
- removeChild(m_sprite,true);
- }
- CCSizetextureSize=m_label->getContentSize();
- textureSize.width+=2*m_fStrokeSize;
- textureSize.height+=2*m_fStrokeSize;
- //calltoclearerror
- glGetError();
- CCRenderTexture*rt=CCRenderTexture::create(textureSize.width,textureSize.height);
- if(!rt)
- {
- CCLOG("createrendertextureFailed!!!!");
- addChild(m_label);
- return;
- }
- ccColor3Bcol=m_label->getColor();
- m_label->setColor(m_colStroke);
- ccBlendFuncoriginalBlend=m_label->getBlendFunc();
- ccBlendFuncfunc={GL_SRC_ALPHA,GL_ONE};
- m_label->setBlendFunc(func);
- m_label->setAnchorPoint(ccp(0.5f,0.5f));
- rt->begin();
- for(inti=0;i<360;i+=15)
- {
- floatr=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);
- }
- voidStrokeLabelTTF::setString(constchar*label)
- {
- if(m_label)
- {
- if(0!=strcmp(label,m_label->getString()))
- {
- m_label->setString(label);
- updateStroke();
- }
- }
- else
- {
- CCLOG("ERROR:CCLabelTTFStroke::setStringm_label=NULL");
- }
- }
- voidStrokeLabelTTF::setColor(constccColor3B&color3)
- {
- if(m_label)
- {
- ccColor3Bcol=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::setColorm_label=NULL");
- }
- }
- voidStrokeLabelTTF::setAnchorPoint(constcocos2d::CCPoint&anchorPoint1)
- {
- this->anchorPoint=anchorPoint1;
- updateStroke();
- }