Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a background fast in cocos2d?

I'm toying with a small game on my iPad using cocos2d and I've run into some performance worries. I have a 512x512 image tiled as my background. That gives me around 40fps with 20 sprites (in a CCSpriteBatchNode), the code for the background is this:

CCSprite *background;
background = [CCSprite spriteWithFile:@"oak.png" rect : CGRectMake(0,
                                                                   0,
                                                                   size.width,
                                                                   size.height)];
background.position =  ccp( size.width /2 , size.height/2 );
        
ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
[background.texture setTexParameters: &params];

If I remove the background I get a solid 60fps.

I've tried converting the image to PVRTC and that did give an extra fps or two. I get identical framerates using a 1024x768 image instead of the tiled version.

Since my background will remain axis aligned, unscaled and generally static. I figure there should be a faster way to draw it than having it as a regular CCSprite?

alt text

like image 837
grapefrukt Avatar asked Oct 10 '10 10:10

grapefrukt


1 Answers

Turns out cocos2d moves in mysterious ways. Adding the background to an otherwise empty wrapping CCSprite gets the framerate back up to 60:

CCSprite *spback = [(CCSprite*)[CCSprite alloc] init];
[self addChild:spback];

CCSprite *sp = [CCSprite spriteWithFile:@"Background.png"];
sp.position = ccp(1024/2, 768/2);
[spback addChild:sp];

Credits for this goes to yaoligang on the cocos2d forums.

like image 193
grapefrukt Avatar answered Oct 13 '22 14:10

grapefrukt