I have been looking for the past couple of hours for a way to create a stroke around labels in cocos2d but so far all I have come up with is this: CCLabelTTF Font Stroke Demo this is just what I need but the stroke looks very blocky, and I am needing something that looks smoother. Is there any way to turn on some kind of antialiasing for the stroke or maybe there is some other way to create a stroke. Any help would be appreciated.
Asked
Active
Viewed 1,454 times
1 Answers
1
To create a stroke:
-(CCRenderTexture*) createStroke: (CCLabelTTF*) label size:(float)size color:(ccColor3B)cor
{
CCRenderTexture* rt = [CCRenderTexture renderTextureWithWidth:label.texture.contentSize.width+size*2 height:label.texture.contentSize.height+size*2];
CGPoint originalPos = [label position];
ccColor3B originalColor = [label color];
BOOL originalVisibility = [label visible];
[label setColor:cor];
[label setVisible:YES];
ccBlendFunc originalBlend = [label blendFunc];
[label setBlendFunc:(ccBlendFunc) { GL_SRC_ALPHA, GL_ONE }];
CGPoint bottomLeft = ccp(label.texture.contentSize.width * label.anchorPoint.x + size, label.texture.contentSize.height * label.anchorPoint.y + size);
//CGPoint positionOffset = ccp(label.texture.contentSize.width * label.anchorPoint.x - label.texture.contentSize.width/2,label.texture.contentSize.height * label.anchorPoint.y - label.texture.contentSize.height/2);
//use this for adding stoke to its self...
CGPoint positionOffset= ccp(-label.contentSize.width/2,-label.contentSize.height/2);
CGPoint position = ccpSub(originalPos, positionOffset);
[rt begin];
for (int i=0; i<360; i+=60) // 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];
[[[rt sprite] texture] setAntiAliasTexParameters];//THIS
[label setPosition:originalPos];
[label setColor:originalColor];
[label setBlendFunc:originalBlend];
[label setVisible:originalVisibility];
[rt setPosition:position];
return rt;
}
Usage:
CCRenderTexture* myStroke = [self createStroke:myCCLabelTTF size:myStrokeSize color:ccYELLOW];
[myCCLabelTTF addChild:myStroke z:-1 tag:kTagStroke];
And to increase smoothness, modify the following function to fit your needs (decrease the +60 increment to perhaps +30). Just note that the more iterations, the more more time spent rendering, which will negatively affect performance:
for (int i=0; i<360; i+=60) // 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];
}

Lorenzo Linarducci
- 541
- 1
- 4
- 8