views:

46

answers:

1

Hi there!

I'm making a game which requires multiple images to be drawn on the screen. The code I'm using for this is:

- (void)drawRect:(CGRect)rect {
   for (i=1;i<=totalImages;i++) {
       if (imageExists[i]) {
           [image drawAtPoint:CGPointmake(imageX[i],imageY[i])];
       }
   }
} 

This drawRect is called by a function:

-(void) gameLoop {
   [self setNeedsDisplay];
}

And the gameloop is called 60 timers per second by a timer:

[NSTimer scheduledTimerWithTimerInterval:0.01666 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];

imageExists[], imageX[] and imageY[] are arrays I use in my gamecode to create/remove and move the images across the screen.

My problem is that when I have about 40 images moving around the framespeed is almost halved from when there was only 1 image. And my game will require 88 images max. (this only happens when I test it on my iPod Touch, in the Simulator everything works fine...)

My question is: why is the drawrect function that slow, or is it the drawAtPoint? And how can I fix this? Should I actually use this for drawing images, or should I learn OpenGL ES for making games?

+1  A: 

My recommendation is to look into a game library like cocos2d-iphone which takes care of most of the OpenGL stuff for you behind the scenes. It will get much higher performance for typical game functions than Quartz and CoreAnimation, and includes things like physics and sound libraries for integrated game interactivity.

The primary thing writing to OpenGL can do for a 2D game is better manage what goes into video memory vs. what stays in RAM, which matters a lot when blitting sprites to the right spot on the screen.

samkass
I'll look into Cocos2d-iPhoneThanks ^^
Maxim Schoemaker