cocos2d实现CCLabelTTF真正字体描边效果

cocos2d实现CCLabelTTF真正字体描边效果,第1张

概述在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。 开发人员:Jason's.Alex   QQ:531401335 csdn博客:http://blog.csdn.net/RuShrooM [cpp] view plain copy #inclu

在开发游戏中,我们需要在需要在游戏中显示一个字体轮廓比较清晰的效果,我们就需要给字体的周围进行描边,让字体显示比较更加突出,我重写了cclabelttf类,使它具有描边的特效,和描边的大小以及颜色。。。

开发人员:Jason's.Alex QQ:531401335

csdn博客:http://blog.csdn.net/rushrooM


[cpp] view plain copy #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; };
[cpp] view plain copy #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(); } 总结

以上是内存溢出为你收集整理的cocos2d实现CCLabelTTF真正字体描边效果全部内容,希望文章能够帮你解决cocos2d实现CCLabelTTF真正字体描边效果所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1023697.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-23
下一篇2022-05-23

发表评论

登录后才能评论

评论列表(0条)

    保存