CCLabelTTF 黑边 + 底图混合

本文介绍了一种在Cocos2d-x中实现黑边字体效果的方法,通过创建并复用纹理,提高了绘制效率。同时利用CCRenderTexture增强了字体的视觉表现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考链接:http://blog.youkuaiyun.com/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);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值