Reputation: 4584
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!
Upvotes: 0
Views: 649
Reputation: 43462
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.
Upvotes: 1
Reputation: 4543
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.
Upvotes: 0