Stroke example for CCLabelTTF and CCSprite.

Hey guys! Several days ago i found a cocs2d-iphone examples “stroke effect for CCLabelTTF objects”. Here is the link to the cocos2d-iphone forum: http://www.cocos2d-iphone.org/forum/topic/12126

I made little modifications in this example and rewrote it for c+*. Here is my code:
<pre>
CCRenderTexture* createStroke
{

CCRenderTexture* rt = CCRenderTexture::renderTextureWithWidthAndHeight~~>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;
label~~>setOpacity;
label~~>setIsVisible;
ccBlendFunc originalBlend = label~~>getBlendFunc;
ccBlendFunc bf = ;
label~~>setBlendFunc;
CCPoint bottomLeft = ccp~~>getContentSize.width * label~~>getAnchorPoint.x* size,
label~~>getTexture~~>getContentSize.height * label~~>getAnchorPoint.y + size);
CCPoint positionOffset= ccp~~>getContentSize.width / 2,
~~ label~~>getTexture()>getContentSize.height / 2);
CCPoint position = ccpSub;
rt
>begin();

for (int i=0; i<360; i+= 15) // you should optimize that for your needs
{
label~~>setPosition)**size, bottomLeft.y + cos)**size)
);
label~~>visit();
}
rt~~>end;
label~~>setPosition(originalPos);
label~~>setColor;
label~~>setBlendFunc(originalBlend);
label~~>setIsVisible;
label~~>setOpacity(originalOpacity);

rt~~>setPosition;
return rt;
}
</pre>
I added “opacity” property and possibility to add Stroke for all CCSprite nodes.
Example of usage:
<pre>
CCSprite* sprite = CCSprite::spriteWithFile;
addChild;
CCRenderTexture* tex = createStroke/*color*/, 50 /*opacity*/);
addChild~~ 1);

![](/uploads/default/4142/130205095038_stroke.png)


stroke.png (48.8 KB)

I suspect the latest line should be read as

addChild(tex, sprite~~>getZOrder~~ 1);

Thanks for sharing this.

Alexander Belchenko wrote:

I suspect the latest line should be read as
>
addChild(tex, sprite~~>getZOrder~~ 1);
>
Thanks for sharing this.

Exactly! Thx and welcome!

It actually works on iOS, but I’m having some problems on Android.

I know is sounds weird, but have a look at the screenshots

Android:

iOS:

Is there any solution for this?

Solved with:

rt->getSprite()->getTexture()->setAntiAliasTexParameters();

xavi pinol wrote:

It actually works on iOS, but I’m having some problems on Android.
>
I know is sounds weird, but have a look at the screenshots

Right, thx for solving! I use setAntiAliasTexParameters by default for all my textures :slight_smile:

xavi pinol wrote:

Solved with:
>
rt->getSprite()->getTexture()->setAntiAliasTexParameters();
>
xavi pinol wrote:
> It actually works on iOS, but I’m having some problems on Android.
>
> I know is sounds weird, but have a look at the screenshots
> …

Thanks for sharing!

Thanks for sharing !

Thanks for sharing!

Thank you for sharing this! Has anyone created HTML5 support? I could really use it.

Thanks.

Alexandr Gerasimov wrote:

Hey guys! Several days ago i found a cocs2d-iphone examples “stroke effect for CCLabelTTF objects”. Here is the link to the cocos2d-iphone forum: http://www.cocos2d-iphone.org/forum/topic/12126
>
I made little modifications in this example and rewrote it for c+*. Here is my code:
>
>
I added “opacity” property and possibility to add Stroke for all CCSprite nodes.
>
Example of usage:
>
>

Add the check while CCSprite is using setAnchorPoint as below:
<pre>
static CCRenderTexture* createStroke{
CCRenderTexture* rt = CCRenderTexture::create~~>getContentSize.width + size * 2,
label~~>getTexture~~>getContentSize.height+size * 2);
CCPoint originalPos = label~~>getPosition;
ccColor3B originalColor = label~~>getColor;
GLubyte originalOpacity = label~~>getOpacity;
label~~>setColor;
label~~>setOpacity;
bool originalVisibility = label~~>isVisible;
ccBlendFunc originalBlend = label~~>getBlendFunc;
ccBlendFunc bf = ;
label~~>setBlendFunc;
CCPoint bottomLeft = ccp~~>getContentSize.width * label~~>getAnchorPoint.x + size,
label~~>getTexture~~>getContentSize.height * label~~>getAnchorPoint.y* size);
//原來沒有判斷AnchorPoint的寫法
// CCPoint positionOffset= ccp( - label~~>getTexture~~>getContentSize.width / 2,
//~~ label~~>getTexture()>getContentSize.height / 2);
//SetAnchorPoint會影響到positionOffset,所以要做判斷
CCPoint positionOffset = CCPointZero;
if.x 0.5f){
positionOffset.x = 0;
}else if(label->getAnchorPoint().x 0.0f){
positionOffset.x =
label~~>getTexture~~>getContentSize.width / 2;
}else{
positionOffset.x = label~~>getTexture~~>getContentSize.width/2;
}
if.y 0.5f){
positionOffset.y = 0;
}else if(label->getAnchorPoint().y 0.0f){
positionOffset.y =~~ label~~>getTexture()>getContentSize.height / 2;
}else{
positionOffset.y = label
>getTexture()>getContentSize.height/2;
}
CCPoint position = ccpSub;
rt
>begin();
for (int i=0; i<360 data-blogger-escaped-for=“” data-blogger-escaped-i=“” data-blogger-escaped-label~~=“” data-blogger-escaped-needs=“” data-blogger-escaped-optimize=“” data-blogger-escaped-should=“” data-blogger-escaped-that=“” data-blogger-escaped-you=“” data-blogger-escaped-your=“”>setPosition)**size, bottomLeft.y + cos)**size)
);
label~~>visit();
}
rt~~>end;
label~~>setPosition(originalPos);
label~~>setColor;
label~~>setBlendFunc(originalBlend);
label~~>setVisible;
label~~>setOpacity(originalOpacity);
rt~~>setPosition;
//反踞齒
rt~~>getSprite()->getTexture()->setAntiAliasTexParameters();
return rt;
}

is there lua code of this?

This implementation has some a flaw, and you guys can see it more clearly if the stoke color is light color such as yellow(ccc3(255,255,0)), the fill color is white and the background is black.
Though I’ve tried some other implementations, I’ve not found any implementation better than this.