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

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

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

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

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


  1. #include"cocos2d.h"@H_502_34@
  2. usingnamespacecocos2d;@H_502_34@
  3. @H_502_34@
  4. classStrokeLabelTTF:publiccocos2d::CCNode@H_502_34@
  5. {@H_502_34@
  6. public:@H_502_34@
  7. StrokeLabelTTF();@H_502_34@
  8. ~StrokeLabelTTF();@H_502_34@
  9. @H_502_34@
  10. public:@H_502_34@
  11. 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@
  12. boolinitWithString(constchar*label,floatfStrokeSize,constcocos2d::ccColor3B&colStroke,cocos2d::CCTextAlignmenthAlignment,cocos2d::CCVerticalTextAlignmentvAlignment);@H_502_34@
  13. @H_502_34@
  14. staticStrokeLabelTTF*create(constchar*string,floatfontSize);@H_502_34@
  15. @H_502_34@
  16. public:@H_502_34@
  17. voidsetColor(constcocos2d::ccColor3B&color3);@H_502_34@
  18. voidsetString(constchar*label);@H_502_34@
  19. voidsetStrokeColor(cocos2d::ccColor3Bcol){m_colStroke=col;updateStroke();}@H_502_34@
  20. voidsetStrokeSize(floatStrokeSize){m_fStrokeSize=StrokeSize;updateStroke();}@H_502_34@
  21. voidsetAnchorPoint(constcocos2d::CCPoint&anchorPoint);@H_502_34@
  22. @H_502_34@
  23. @H_502_34@
  24. protected:@H_502_34@
  25. voidupdateStroke();@H_502_34@
  26. @H_502_34@
  27. private:@H_502_34@
  28. floatm_fStrokeSize;@H_502_34@
  29. cocos2d::ccColor3Bm_colStroke;@H_502_34@
  30. cocos2d::CCSprite*m_sprite;@H_502_34@
  31. cocos2d::CCLabelTTF*m_label;@H_502_34@
  32. CCPointanchorPoint;@H_502_34@
  33. };@H_502_34@

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

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