Mord Fustang
Mord Fustang

Reputation: 1553

Cocos2d ccDrawLine performance issue

I use cocos2d 2.0 and Xcode 4.5. I am trying to learn how to draw a line. I can draw a line but after I drew few lines a serious performance issue occurs on Simulator.

Simulator starts to freeze, draws lines very very slowly and worst of all ,I guess because of -(void)draw is called every frame, the label on the screen becomes bold

before lines :

enter image description here

after lines;

enter image description here

I use following code : .m

-(id) init
{
    if( (self=[super init])) {


        CCLabelTTF *label = [CCLabelTTF labelWithString:@"Simple Line Demo" fontName:@"Marker Felt" fontSize:32];
        label.position =  ccp( 240, 300 );
        [self addChild: label];

        _naughtytoucharray =[[NSMutableArray alloc ] init];

         self.isTouchEnabled = YES;


    }
    return self;
}

-(BOOL) ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
    BOOL isTouching;
    // determine if it's a touch you want, then return the result
    return isTouching;
}


-(void) ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event  {
    UITouch *touch = [ touches anyObject];
    CGPoint new_location = [touch locationInView: [touch view]];
    new_location = [[CCDirector sharedDirector] convertToGL:new_location];

    CGPoint oldTouchLocation = [touch previousLocationInView:touch.view];
    oldTouchLocation = [[CCDirector sharedDirector] convertToGL:oldTouchLocation];
    oldTouchLocation = [self convertToNodeSpace:oldTouchLocation];
    // add my touches to the naughty touch array
    [_naughtytoucharray addObject:NSStringFromCGPoint(new_location)];
    [_naughtytoucharray addObject:NSStringFromCGPoint(oldTouchLocation)];
}
-(void)draw
{
    [super draw];
    ccDrawColor4F(1.0f, 0.0f, 0.0f, 100.0f);
    for(int i = 0; i < [_naughtytoucharray count]; i+=2)
    {
        CGPoint start = CGPointFromString([_naughtytoucharray objectAtIndex:i]);
        CGPoint end = CGPointFromString([_naughtytoucharray objectAtIndex:i+1]);
        ccDrawLine(start, end);

    }
}
- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    ManageTraffic *line = [ManageTraffic node];
    [self addChild: line z:99 tag:999];
}

I saw few Air Traffic Control games such as Flight Control, ATC Mania works really well.

Does this performance issue occur because of CCDrawLine/UITouch *touch or it is a common issue? What Flight Control, ATC Mania might be using for line drawing?

Thanks in advance.

EDIT::::

OK I guess problem is not ccDrawLine, problem is I call ManageTraffic *line = [ManageTraffic node]; every time touch ends it calls init of node so it overrides scene

- (void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    ManageTraffic *line = [ManageTraffic node];
    [self addChild: line z:99 tag:999];
}

Upvotes: 0

Views: 383

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

There's three things going on:

  1. You assess performance on the Simulator. Test it on a device as Ben says.
  2. You store points as strings and convert strings back to CGPoint. That is terribly inefficient.
  3. ccDrawLine is not exactly efficient. For a couple dozen line segments it's ok. In your case maybe not (see below).

For #2, create a point class with only a CGPoint property and use that to store points in the array. Removes the string conversion or packing into NSData.

For #3 make sure that new points are only added if the new point is at least n points away from the previous point. For example a distance of 10 should reduce the number of points while still allowing for relatively fine line details.

Also regarding #3, I notice you add both current and previous point to the array. Why? You only need to add the new point, and then draw points from index 0 to 1, from 1 to 2, and so on. You only have to test for the case where there is only 1 point. The previous touch event's location is always the next touch event's previousLocation. So you're storing twice as many points as you need to.

Upvotes: 1

Related Questions