프로그래밍/Cocos2D2012. 2. 6. 10:52
//
//  HelloWorldLayer.m
//  FontStrokeDemo
//
//  Created by user on 12-2-1.
//  Copyright __MyCompanyName__ 2012亮�. All rights reserved.
//


// Import the interfaces
#import "HelloWorldLayer.h"

// HelloWorldLayer implementation
@implementation HelloWorldLayer

+(CCScene *) scene
{
	// 'scene' is an autorelease object.
	CCScene *scene = [CCScene node];
	
	// 'layer' is an autorelease object.
	HelloWorldLayer *layer = [HelloWorldLayer node];
	
	// add layer as a child to scene
	[scene addChild: layer];
	
	// return the scene
	return scene;
}

-(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 meio = ccp(label.texture.contentSize.width/2+size, label.texture.contentSize.height/2+size);
	[rt begin];
	for (int i=0; i<360; i+=30) // you should optimize that for your needs
	{
		[label setPosition:ccp(meio.x + sin(CC_DEGREES_TO_RADIANS(i))*size, meio.y + cos(CC_DEGREES_TO_RADIANS(i))*size)];
		[label visit];
	}
	[rt end];
	[label setPosition:originalPos];
	[label setColor:originalColor];
	[label setBlendFunc:originalBlend];
	[label setVisible:originalVisibility];
	[rt setPosition:originalPos];
	return rt;
}

// on "init" you need to initialize your instance
-(id) init
{
	// always call "super" init
	// Apple recommends to re-assign "self" with the "super" return value
	if( (self=[super init])) {
		
        //label1
		CCLabelTTF* label = [CCLabelTTF labelWithString:@"FontStrokeDemo"
                                             dimensions:CGSizeMake(305,179) 
                                              alignment:UITextAlignmentLeft
                                               fontName:@"Arial" 
                                               fontSize:38];
        [label setPosition:ccp(240, 160)];
        [label setColor:ccWHITE];
        CCRenderTexture* stroke = [self createStroke:label  
                                                size:3  
                                               color:ccBLUE];
        [self addChild:stroke];
        [self addChild:label];
        
        //label2
		CCLabelTTF* label2 = [CCLabelTTF labelWithString:@"Test String"
                                             dimensions:CGSizeMake(305,179) 
                                              alignment:UITextAlignmentLeft
                                               fontName:@"Arial" 
                                               fontSize:38];
        [label2 setPosition:ccp(240, 100)];
        [label2 setColor:ccWHITE];
        CCRenderTexture* stroke2 = [self createStroke:label2  
                                                size:3  
                                               color:ccRED];
        [self addChild:stroke2];
        [self addChild:label2];
	}
	return self;
}

// on "dealloc" you need to release all your retained objects
- (void) dealloc
{
	// in case you have something to dealloc, do it in this method
	// in this particular example nothing needs to be released.
	// cocos2d will automatically release all the children (Label)
	
	// don't forget to call "super dealloc"
	[super dealloc];
}
@end
Posted by windship