工具函数文字描边,CCSprite 描边。

CCRenderTexture * UITool::createStroke(CCObject * label, int size, ccColor3B color, int opacity ,int step  )
{
	CCTextureProtocol * label_texture = dynamic_cast<CCTextureProtocol*>(label);
	CCNode * label_node = dynamic_cast<CCNode*>(label);
	CCRGBAProtocol * label_rgb = dynamic_cast<CCRGBAProtocol*>(label);

	//CCSize labelSize = label_node->getContentSize();
	CCSize labelSize = label_texture->getTexture()->getContentSize();

	CCRenderTexture* rt = CCRenderTexture::create(
		labelSize.width + size * 2,
		labelSize.height + size * 2
		);

	CCPoint originalPos = label_node->getPosition();

	ccColor3B originalColor = label_rgb->getColor();

	GLubyte originalOpacity = label_rgb->getOpacity();

	bool originalVisibility = label_node->isVisible();

	label_rgb->setColor(color);

	label_rgb->setOpacity(opacity);

	label_node->setVisible(true);

	ccBlendFunc originalBlend = label_texture->getBlendFunc();

	ccBlendFunc bf = {GL_SRC_ALPHA, GL_ONE};

	label_texture->setBlendFunc(bf);

	CCPoint bottomLeft = ccp(
		labelSize.width * label_node->getAnchorPoint().x + size, 
		labelSize.height * label_node->getAnchorPoint().y + size);

	CCPoint positionOffset= ccp(
		-labelSize.width / 2,
		-labelSize.height / 2);
	CCPoint position = ccpSub(originalPos, positionOffset);
	rt->begin();

	for (int i=0; i<360; i+= step) // you should optimize that for your needs
	{
		label_node->setPosition(
			ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)
			);
		label_node->visit();
	}
	rt->end();

	label_node->setPosition(originalPos);
	label_rgb->setColor(originalColor);
	label_texture->setBlendFunc(originalBlend);
	label_node->setVisible(originalVisibility);
	label_rgb->setOpacity(originalOpacity);

	rt->setPosition(position);

	return rt;
}





   

你可能感兴趣的:(工具函数文字描边,CCSprite 描边。)