在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了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();
- }