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