- CCRenderTexture* createStroke(CCSprite* label, int size, ccColor3B color, GLubyte opacity)
- {
- CCRenderTexture* rt = CCRenderTexture::renderTextureWithWidthAndHeight(
- label->getTexture()->getContentSize().width + size * 2,
- label->getTexture()->getContentSize().height+size * 2
- );
- CCPoint originalPos = label->getPosition();
- ccColor3B originalColor = label->getColor();
- GLubyte originalOpacity = label->getOpacity();
- bool originalVisibility = label->getIsVisible();
- label->setColor(color);
- label->setOpacity(opacity);
- label->setIsVisible(true);
- ccBlendFunc originalBlend = label->getBlendFunc();
- ccBlendFunc bf = {GL_SRC_ALPHA, GL_ONE};
- label->setBlendFunc(bf);
- CCPoint bottomLeft = ccp(
- label->getTexture()->getContentSize().width * label->getAnchorPoint().x + size,
- label->getTexture()->getContentSize().height * label->getAnchorPoint().y + size);
- CCPoint positionOffset= ccp(
- - label->getTexture()->getContentSize().width / 2,
- - label->getTexture()->getContentSize().height / 2);
- CCPoint position = ccpSub(originalPos, positionOffset);
- rt->begin();
- for (int i=0; i<360; i+= 15) // you should optimize that for your needs
- {
- label->setPosition(
- ccp(bottomLeft.x + sin(CC_DEGREES_TO_RADIANS(i))*size, bottomLeft.y + cos(CC_DEGREES_TO_RADIANS(i))*size)
- );
- label->visit();
- }
- rt->end();
- label->setPosition(originalPos);
- label->setColor(originalColor);
- label->setBlendFunc(originalBlend);
- label->setIsVisible(originalVisibility);
- label->setOpacity(originalOpacity);
- rt->setPosition(position);
- return rt;
- }
文字描边的函数,下面是使用方法:
- CCSprite* sprite = CCSprite::spriteWithFile("some_sprite.png");
- addChild(sprite);
- CCRenderTexture* tex = createStroke(sprite, 2 /*size*/, ccc3(0, 255, 0)/*color*/, 50 /*opacity*/);
- addChild(tex, sprite->getZOrder() - 1);