views:

504

answers:

2

We're working on a couple simple games and we have some performance problems that we keep running into, and I was curious if it was a code issue or a "we're using the wrong objects" issue.

Our games are basically simple ones that work as follows:

We have a custom view that we load, and a custom object that is our game engine. The view has a timer that fires like such:

[NSTimer scheduledTimerWithTimeInterval:1.0 / 30.0 target:self selector:@selector(animationTimerMethod) userinfo:nil repeats:YES];

and in our timer method, we have the following:

- (void)animationTimerMethod:(NSTimer*)timer 
{
  if([gameEngine doGameLoop])  //only redraw if we need to
    [self setNeedsDisplay];
}

Our drawing code is very simple, we're just drawing a couple of images on the screen, using code similar to the following:

- (void)drawRect:(CGRect)rect
{
  CGGraphicsContext ctx = UIGraphicsGetCurrentContext();
  CGContextDrawImage(ctx, someRect, someImage);
}

The main problem we have is responsiveness to touch. Our code will not get a response in the touchesBegan method many times. Is this just a limitation of the Core Graphics library? Should we be using OpenGL? Are there other ways to do simple animations (not even objects moving on screen, just 6 or so frames of animation for an object in the same place) that would be more responsive?

Thanks so much!

A: 

My first question is do your images have transparency?

Images with transparency with HIGHLY effect performance. In Apple's doc's they make reference to this point several times.

The second thing I would ask is, have you tried running your app in Instruments and/or Shark? Both these apps can help give you an idea of where your problems are happening.

kdbdallas
+1  A: 

There are definitely ways to speed this up. Manually redrawing the images every frame means you are redrawing textures you could be reusing. You could reuse them either by moving to OpenGL, or moving the images into a CALayer and then just repositioning the layer instead of redrawing the image.

If you do not want to move to OpenGL you would probably also see a significant performance win by moving to CAAnimation instead of having your code calculate each frame, but that might require significant changes to your engine.

Also, if you can avoid avoid alpha compositing it is a big speed up.

Louis Gerbarg
In this case I'm not moving the images...I'm changing them from time to time (to do simple, in-place animations)...would a move to Layers or CAAnimation still make a difference?
Jonas
I'm currently investigating the alpha thing...it looks like *not* drawing a background (but having a main character w/ alpha) makes a big difference...maybe because the images in the foreground that have an alpha are drawing over a solid color instead of an image, that makes for less CPU time?
Jonas
Lastly...are there good resources out there as far as OpenGL tutorials? My Google-fu has failed me so far...
Jonas
If you have a small number of frames, I think CoreAnimation can cache them for you, which will really speed things up.
Mark Bessey
One big difference to using CoreAnimation is that the screen refresh happens in another thread. That would probably help enormously in terms of responsiveness to user events...
Mark Bessey
If you can create layers with the images and swap around the layers it lets the GPU store the layers on textures. It also lets it synch the updates with screen refresh which will stop spurious compositing. How many images do you have, and how often do you reuse them?
Louis Gerbarg