参考链接:http://blog.csdn.net/song_hui_xiang/article/details/17375279
之前发表过一篇文章,使用 cocos2d-x 新增的一个 api 来创建黑边和阴影,
在真机上跑过之后发现很坑爹,黑边死活出不来。
无奈之下采用之前查到的一种方案,大体就是先上下左右移动一个像素放黑色标签,
放完之后再在中央放一个其他颜色的标签,达到黑边的效果。
不过创建了四个标签耗费偏大,实际只需要一个就行了,
再调用 CCSprite 的createWithTexture 即可复用同一块纹理,效率显著提高。
另外使用了 CCRenderTexture,给字体轮廓附加一些其他的色泽,增强效果。
代码如下:
- void addSuperLabel(CCNode* t_pNodeParent, const string& in_oStrTitle) {
- CCDirector* t_pDirector = CCDirector::sharedDirector();
- CCSize t_oSize = t_pDirector->getWinSize();
- float t_fScaleFactor = t_pDirector->getContentScaleFactor();
- float t_fFontSize = 42.f / t_fScaleFactor;
- // -------------------- 外部坐标 --------------------
- float t_fOutX = t_oSize.width * 0.5f;
- float t_fOutY = t_oSize.height * 0.5f;
- // -------------------- 字体轮廓 --------------------
- CCLabelTTF* t_pLblTitle = CCLabelTTF::create(in_oStrTitle.c_str(), "Helvetica-Bold", t_fFontSize);
- CCTexture2D* t_pTex = t_pLblTitle->getTexture();
- CCSize t_oSizeTex = t_pTex->getContentSize();
- float t_fInX = t_oSizeTex.width * 0.5f;
- float t_fInY = t_oSizeTex.height * 0.5f;
- // -------------------- 黑边 --------------------
- CCSprite* t_pSpUp = CCSprite::createWithTexture(t_pTex);
- t_pSpUp->setPosition(ccp(t_fOutX, t_fOutY + 1.f));
- t_pSpUp->setColor(ccBLACK);
- t_pNodeParent->addChild(t_pSpUp);
- CCSprite* t_pSpLeft = CCSprite::createWithTexture(t_pTex);
- t_pSpLeft->setPosition(ccp(t_fOutX - 1.f, t_fOutY));
- t_pSpLeft->setColor(ccBLACK);
- t_pNodeParent->addChild(t_pSpLeft);
- CCSprite* t_pSpDown = CCSprite::createWithTexture(t_pTex);
- t_pSpDown->setPosition(ccp(t_fOutX, t_fOutY - 1.f));
- t_pSpDown->setColor(ccBLACK);
- t_pNodeParent->addChild(t_pSpDown);
- CCSprite* t_pSpRight = CCSprite::createWithTexture(t_pTex);
- t_pSpRight->setPosition(ccp(t_fOutX + 1.f, t_fOutY));
- t_pSpRight->setColor(ccBLACK);
- t_pNodeParent->addChild(t_pSpRight);
- // -------------------- 渐变层 --------------------
- ccColor4B t_oColorTop = ccc4(255, 255, 255, 255);
- ccColor4B t_oColorBot = ccc4(187, 117, 60, 255);
- CCLayerGradient* t_pLyrGradient = CCLayerGradient::create(t_oColorTop, t_oColorBot);
- t_pLyrGradient->setContentSize(t_oSizeTex);
- // -------------------- 着色 --------------------
- CCRenderTexture* t_pRenderTex = CCRenderTexture::create(t_oSizeTex.width, t_oSizeTex.height);
- t_pRenderTex->beginWithClear(0.f, 0.f, 0.f, 1.f);
- // 字体轮廓
- t_pLblTitle->setBlendFunc((ccBlendFunc){GL_ONE, GL_ZERO});
- t_pLblTitle->setPosition(ccp(t_fInX, t_fInY));
- t_pLblTitle->visit();
- // 渐变
- t_pLyrGradient->setBlendFunc((ccBlendFunc){GL_DST_COLOR, GL_ZERO});
- t_pLyrGradient->visit();
- t_pRenderTex->end();
- // 添加到父节点
- t_pRenderTex->setPosition(ccp(t_fOutX, t_fOutY));
- t_pNodeParent->addChild(t_pRenderTex);
- }