CCLabelTTF 黑边 + 底图混合

参考链接:http://blog.csdn.net/song_hui_xiang/article/details/17375279

之前发表过一篇文章,使用 cocos2d-x 新增的一个 api 来创建黑边和阴影,

在真机上跑过之后发现很坑爹,黑边死活出不来。

无奈之下采用之前查到的一种方案,大体就是先上下左右移动一个像素放黑色标签,

放完之后再在中央放一个其他颜色的标签,达到黑边的效果。

不过创建了四个标签耗费偏大,实际只需要一个就行了,

再调用 CCSprite 的createWithTexture 即可复用同一块纹理,效率显著提高。

另外使用了 CCRenderTexture,给字体轮廓附加一些其他的色泽,增强效果。

代码如下:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void addSuperLabel(CCNode* t_pNodeParent, const string& in_oStrTitle) {  
  2.     CCDirector* t_pDirector = CCDirector::sharedDirector();  
  3.     CCSize t_oSize = t_pDirector->getWinSize();  
  4.     float t_fScaleFactor = t_pDirector->getContentScaleFactor();  
  5.     float t_fFontSize = 42.f / t_fScaleFactor;  
  6.       
  7.     // -------------------- 外部坐标 --------------------  
  8.     float t_fOutX = t_oSize.width * 0.5f;  
  9.     float t_fOutY = t_oSize.height * 0.5f;  
  10.       
  11.     // -------------------- 字体轮廓 --------------------  
  12.     CCLabelTTF* t_pLblTitle = CCLabelTTF::create(in_oStrTitle.c_str(), "Helvetica-Bold", t_fFontSize);  
  13.     CCTexture2D* t_pTex = t_pLblTitle->getTexture();  
  14.     CCSize t_oSizeTex = t_pTex->getContentSize();  
  15.     float t_fInX = t_oSizeTex.width * 0.5f;  
  16.     float t_fInY = t_oSizeTex.height * 0.5f;  
  17.       
  18.     // -------------------- 黑边 --------------------  
  19.     CCSprite* t_pSpUp = CCSprite::createWithTexture(t_pTex);  
  20.     t_pSpUp->setPosition(ccp(t_fOutX, t_fOutY + 1.f));  
  21.     t_pSpUp->setColor(ccBLACK);  
  22.     t_pNodeParent->addChild(t_pSpUp);  
  23.       
  24.     CCSprite* t_pSpLeft = CCSprite::createWithTexture(t_pTex);  
  25.     t_pSpLeft->setPosition(ccp(t_fOutX - 1.f, t_fOutY));  
  26.     t_pSpLeft->setColor(ccBLACK);  
  27.     t_pNodeParent->addChild(t_pSpLeft);  
  28.       
  29.     CCSprite* t_pSpDown = CCSprite::createWithTexture(t_pTex);  
  30.     t_pSpDown->setPosition(ccp(t_fOutX, t_fOutY - 1.f));  
  31.     t_pSpDown->setColor(ccBLACK);  
  32.     t_pNodeParent->addChild(t_pSpDown);  
  33.       
  34.     CCSprite* t_pSpRight = CCSprite::createWithTexture(t_pTex);  
  35.     t_pSpRight->setPosition(ccp(t_fOutX + 1.f, t_fOutY));  
  36.     t_pSpRight->setColor(ccBLACK);  
  37.     t_pNodeParent->addChild(t_pSpRight);  
  38.       
  39.     // -------------------- 渐变层 --------------------  
  40.     ccColor4B t_oColorTop = ccc4(255, 255, 255, 255);  
  41.     ccColor4B t_oColorBot = ccc4(187, 117, 60, 255);  
  42.     CCLayerGradient* t_pLyrGradient = CCLayerGradient::create(t_oColorTop, t_oColorBot);  
  43.     t_pLyrGradient->setContentSize(t_oSizeTex);  
  44.       
  45.     // -------------------- 着色 --------------------  
  46.     CCRenderTexture* t_pRenderTex = CCRenderTexture::create(t_oSizeTex.width, t_oSizeTex.height);  
  47.     t_pRenderTex->beginWithClear(0.f, 0.f, 0.f, 1.f);  
  48.     // 字体轮廓  
  49.     t_pLblTitle->setBlendFunc((ccBlendFunc){GL_ONE, GL_ZERO});  
  50.     t_pLblTitle->setPosition(ccp(t_fInX, t_fInY));  
  51.     t_pLblTitle->visit();  
  52.     // 渐变  
  53.     t_pLyrGradient->setBlendFunc((ccBlendFunc){GL_DST_COLOR, GL_ZERO});  
  54.     t_pLyrGradient->visit();  
  55.     t_pRenderTex->end();  
  56.     // 添加到父节点  
  57.     t_pRenderTex->setPosition(ccp(t_fOutX, t_fOutY));  
  58.     t_pNodeParent->addChild(t_pRenderTex);  
  59. }  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值