您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-x CCLabelTTF 描边

2013-10-17 13:26 369 查看
原文地址:CCLabelTTF 描边">cocos2d-x CCLabelTTF 描边作者:王烁

CCLabelTTF 描边" TITLE="[转载]cocos2d-x CCLabelTTF 描边" />


bool HelloWorld::init()

{

//////////////////////////////

// 1. super init first

if (
!CCLayer::init() )

{

return false;

}

/////////////////////////////

// 2. add a menu item with "X" image, which is clicked to quit the
program

// you may
modify it.

// add a "close" icon to exit the progress. it's an autorelease
object

CCMenuItemImage *pCloseItem
= CCMenuItemImage::itemFromNormalImage(

"CloseNormal.png",

"CloseSelected.png",

this,

menu_selector(HelloWorld::menuCloseCallback)
);

pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20)
);

// create menu, it's an autorelease object

CCMenu* pMenu = CCMenu::menuWithItems(pCloseItem, NULL);

pMenu->setPosition( CCPointZero );

this->addChild(pMenu, 1);

CCSize size
= CCDirector::sharedDirector()->getWinSize();

CCLabelTTF* pLabel
= CCLabelTTF::labelWithString("http://blog.sina.com.cn/iphonewangshuo",CCSizeMake(360, 100),CCTextAlignmentLeft,"Georgia-BoldItalic", 24);

pLabel->setPosition( ccp(size.width / 2,
size.height/2) );

pLabel->setColor(ccYELLOW);

CCRenderTexture *stroke=this->createStroke(pLabel,2, ccRED);

this->addChild(stroke);

// add the label as a child to this layer

this->addChild(pLabel);

pLabel->setPosition( ccp(size.width / 2+2,
size.height/2) );

// add "HelloWorld" splash screen"

return true;

}

CCRenderTexture* HelloWorld::createStroke(CCLabelTTF*lable,float size,ccColor3B cor)

{

CCRenderTexture*rt=CCRenderTexture::renderTextureWithWidthAndHeight(lable->getTexture()->getContentSize().width,
lable->getTexture()->getContentSize().height+size*2);

CCPoint originalPos
= lable->getPosition();

ccColor3B originalColor
= lable->getColor();

bool originalVisibility
=lable->getIsVisible();

lable->setColor(cor);

lable->setIsVisible(true);

ccBlendFunc originalBlend
= lable->getBlendFunc();

ccBlendFunc blendFunc1
= { GL_SRC_ALPHA, GL_ONE };

lable->setBlendFunc(blendFunc1);

float aa=lable->getTexture()->getContentSize().width*lable->getAnchorPoint().x+size;

float bb=lable->getTexture()->getContentSize().height*lable->getAnchorPoint().y+size;

CCPoint bottomLeft=ccp(aa,
bb);

float aaa=lable->getTexture()->getContentSize().width*lable->getAnchorPoint().x-
lable->getTexture()->getContentSize().width/2;

float bbb=lable->getTexture()->getContentSize().height*lable->getAnchorPoint().y-
lable->getTexture()->getContentSize().height/2;

CCPoint positionOffset=ccp(aaa,bbb);

CCPoint position=ccpSub(originalPos,
positionOffset);

rt->begin();

for (int i=0; i<</span>360; i+=30) // you should
optimize that for your needs

{

lable->setPosition(ccp(bottomLeft.x+sin(CC_DEGREES_TO_RADIANS(i)*size),
bottomLeft.y+cos(CC_DEGREES_TO_RADIANS(i)*size)));

lable->visit();

}

rt->end();

lable->setPosition(originalPos);

lable->setColor(originalColor);

lable->setBlendFunc(originalBlend);

lable->setIsVisible(originalVisibility);

rt->setPosition(position);

return rt;

}

void HelloWorld::menuCloseCallback(CCObject*
pSender)

{

CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

exit(0);

#endif

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: