Cocos2d-x描边字的实现

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;
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值