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

前端之家收集整理的这篇文章主要介绍了cocos2d实现CCLabelTTF真正字体描边效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

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

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


  1. #include"cocos2d.h"
  2. usingnamespacecocos2d;
  3. classStrokeLabelTTF:publiccocos2d::CCNode
  4. {
  5. public:
  6. StrokeLabelTTF();
  7. ~StrokeLabelTTF();
  8. public:
  9. staticStrokeLabelTTF*create(constchar*string,constchar*fontName,floatfontSize,floatstrokeSize,constcocos2d::ccColor3B&colStroke=cocos2d::ccc3(0,0),cocos2d::CCTextAlignmenthAlignment=cocos2d::kCCTextAlignmentCenter,cocos2d::CCVerticalTextAlignmentvAlignment=cocos2d::kCCVerticalTextAlignmentTop);
  10. boolinitWithString(constchar*label,floatfStrokeSize,constcocos2d::ccColor3B&colStroke,cocos2d::CCTextAlignmenthAlignment,cocos2d::CCVerticalTextAlignmentvAlignment);
  11. staticStrokeLabelTTF*create(constchar*string,floatfontSize);
  12. public:
  13. voidsetColor(constcocos2d::ccColor3B&color3);
  14. voidsetString(constchar*label);
  15. voidsetStrokeColor(cocos2d::ccColor3Bcol){m_colStroke=col;updateStroke();}
  16. voidsetStrokeSize(floatStrokeSize){m_fStrokeSize=StrokeSize;updateStroke();}
  17. voidsetAnchorPoint(constcocos2d::CCPoint&anchorPoint);
  18. protected:
  19. voidupdateStroke();
  20. private:
  21. floatm_fStrokeSize;
  22. cocos2d::ccColor3Bm_colStroke;
  23. cocos2d::CCSprite*m_sprite;
  24. cocos2d::CCLabelTTF*m_label;
  25. CCPointanchorPoint;
  26. };

  1. #include"StrokeLabelTTF.h"
  2. USING_NS_CC;
  3. StrokeLabelTTF::StrokeLabelTTF()
  4. :m_colStroke(ccc3(0,0))
  5. ,m_fStrokeSize(1.0f)
  6. ,m_sprite(NULL)
  7. ,m_label(NULL)
  8. ,anchorPoint(0.5,0.5)
  9. {}
  10. StrokeLabelTTF::~StrokeLabelTTF()
  11. {
  12. CC_SAFE_RELEASE_NULL(m_label);
  13. }
  14. boolStrokeLabelTTF::initWithString(constchar*string,CCTextAlignmenthAlignment,CCVerticalTextAlignmentvAlignment)
  15. {
  16. m_fStrokeSize=strokeSize;
  17. m_colStroke=colStroke;
  18. m_label=CCLabelTTF::create(string,fontName,fontSize,CCSizeZero,hAlignment,vAlignment);
  19. m_label->retain();
  20. updateStroke();
  21. returntrue;
  22. }
  23. StrokeLabelTTF*StrokeLabelTTF::create(constchar*string,CCVerticalTextAlignmentvAlignment)
  24. {
  25. StrokeLabelTTF*pRet=newStrokeLabelTTF();
  26. if(pRet&&pRet->initWithString(string,fStrokeSize,colStroke,vAlignment))
  27. {
  28. pRet->autorelease();
  29. returnpRet;
  30. }
  31. CC_SAFE_DELETE(pRet);
  32. returnNULL;
  33. }
  34. StrokeLabelTTF*StrokeLabelTTF::create(constchar*string,floatfontSize)
  35. {
  36. StrokeLabelTTF*pRet=newStrokeLabelTTF();
  37. if(pRet&&pRet->initWithString(string,1.5,ccc3(0,kCCTextAlignmentCenter,kCCVerticalTextAlignmentTop))
  38. {
  39. pRet->autorelease();
  40. returnpRet;
  41. }
  42. CC_SAFE_DELETE(pRet);
  43. returnNULL;
  44. }
  45. voidStrokeLabelTTF::updateStroke()
  46. {
  47. if(m_sprite)
  48. {
  49. removeChild(m_sprite,true);
  50. }
  51. CCSizetextureSize=m_label->getContentSize();
  52. textureSize.width+=2*m_fStrokeSize;
  53. textureSize.height+=2*m_fStrokeSize;
  54. //calltoclearerror
  55. glGetError();
  56. CCRenderTexture*rt=CCRenderTexture::create(textureSize.width,textureSize.height);
  57. if(!rt)
  58. {
  59. CCLOG("createrendertextureFailed!!!!");
  60. addChild(m_label);
  61. return;
  62. }
  63. ccColor3Bcol=m_label->getColor();
  64. m_label->setColor(m_colStroke);
  65. ccBlendFuncoriginalBlend=m_label->getBlendFunc();
  66. ccBlendFuncfunc={GL_SRC_ALPHA,GL_ONE};
  67. m_label->setBlendFunc(func);
  68. m_label->setAnchorPoint(ccp(0.5f,0.5f));
  69. rt->begin();
  70. for(inti=0;i<360;i+=15)
  71. {
  72. floatr=CC_DEGREES_TO_RADIANS(i);
  73. m_label->setPosition(ccp(
  74. textureSize.width*0.5f+sin(r)*m_fStrokeSize,
  75. textureSize.height*0.5f+cos(r)*m_fStrokeSize));
  76. m_label->visit();
  77. }
  78. m_label->setColor(col);
  79. m_label->setBlendFunc(originalBlend);
  80. m_label->setPosition(ccp(textureSize.width*0.5f,textureSize.height*0.5f));
  81. m_label->visit();
  82. rt->end();
  83. CCTexture2D*texture=rt->getSprite()->getTexture();
  84. texture->setAliasTexParameters();
  85. m_sprite=CCSprite::createWithTexture(rt->getSprite()->getTexture());
  86. setContentSize(m_sprite->getContentSize());
  87. m_sprite->setAnchorPoint(this->anchorPoint);//------------
  88. m_sprite->setPosition(ccp(0,0));
  89. ((CCSprite*)m_sprite)->setFlipY(true);
  90. addChild(m_sprite);
  91. }
  92. voidStrokeLabelTTF::setString(constchar*label)
  93. {
  94. if(m_label)
  95. {
  96. if(0!=strcmp(label,m_label->getString()))
  97. {
  98. m_label->setString(label);
  99. updateStroke();
  100. }
  101. }
  102. else
  103. {
  104. CCLOG("ERROR:CCLabelTTFStroke::setStringm_label=NULL");
  105. }
  106. }
  107. voidStrokeLabelTTF::setColor(constccColor3B&color3)
  108. {
  109. if(m_label)
  110. {
  111. ccColor3Bcol=m_label->getColor();
  112. if(color3.r!=col.r||color3.g!=col.g||color3.b!=col.b)
  113. {
  114. m_label->setColor(color3);
  115. updateStroke();
  116. }
  117. }
  118. else
  119. {
  120. CCLOG("ERROR:CCLabelTTFStroke::setColorm_label=NULL");
  121. }
  122. }
  123. voidStrokeLabelTTF::setAnchorPoint(constcocos2d::CCPoint&anchorPoint1)
  124. {
  125. this->anchorPoint=anchorPoint1;
  126. updateStroke();
  127. }
原文链接:/cocos2dx/345405.html

猜你在找的Cocos2d-x相关文章