CCSprite* HelloWorld::createStrokeTexture(const char* value, float strokeValue, ccColor3B color)
{
// float fontSize = m_fontSize - 2 * strokeSize;
/* 创建一个CCLabelTTF,含有期望字体样式,作为画笔 */
CCLabelTTF *label = CCLabelTTF::create(value, "Arial", 50);
/* 通过label的大小来设置最终生成的纹理图片的大小,strokeValue为描边字体的偏移量,影响粗细 */
CCSize textureSize = label->getContentSize();
textureSize.width += 2 * strokeValue;
textureSize.height += 2 * strokeValue;
/* 监测OpenGl的错误状态 */
glGetError();
/* 创建一张纹理画布 */
CCRenderTexture *rt = CCRenderTexture::create(textureSize.width, textureSize.height);
if (!rt)
{
CCLog("create render texture failed !!!!");
addChild(label);
return 0;
}
/* 设置描边的颜色 */
label->setColor(color);
/*
*拿到源文字的混色机制,存储以备恢复,并设置新的目标混色机制
*混色机制设为:源颜色透明度(影响亮度)和目标颜色(影响颜色)
*/
ccBlendFunc originalBlend = label->getBlendFunc();
ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE };
label->setBlendFunc(func);
label->setAnchorPoint(ccp(0.5, 0.5));
/* 张开画布,开始绘画 */
rt->begin();
for (int i = 0; i < 360; i += 5)//每变化5度绘制一张
{
float r = CC_DEGREES_TO_RADIANS(i); //度数格式的转换
label->setPosition(ccp(textureSize.width * 0.5f + sin(r) * strokeValue, textureSize.height * 0.5f + cos(r) * strokeValue));
/* CCRenderTexture的用法,在begin和end之间visit的纹理,都会画在CCRenderTexture里面 */
label->visit();//画了一次该label
}
/* 恢复原始的label并绘制在最上层 */
label->setColor(ccWHITE);
label->setBlendFunc(originalBlend);
label->setPosition(ccp(textureSize.width * 0.5f, textureSize.height * 0.5f));
label->visit();
/* 在画布上绘制结束,此时会生成一张纹理 */
rt->end();
/* 取出生成的纹理,添加抗锯齿打磨,并返回 */
CCTexture2D *texture = rt->getSprite()->getTexture();
texture->setAntiAliasTexParameters();// setAliasTexParameters();
CCSprite* sp = CCSprite::createWithTexture(texture);
sp->setFlipY(true);
return sp;
}
Cocos2d-x描边字的实现
最新推荐文章于 2019-08-30 14:44:23 发布