views:

614

answers:

3

I try to draw rain and snow as particle system using Core Graphics. In simulator rendering proceeded fine but when I run my app on real device rendering is slow down.

So, advise me please approaches to increase particle system drawing performance on iPhone.

May be I should use OpenGL for this or CoreAnimation?

+1  A: 

It's hard to give advice with little to no information about your implementation. One thing that is a major bottleneck on the iPhone from my experience are memory allocations. So if you're allocating new objects for each particle spawned, that would be the first thing you might want to fix. (Allocate a pool of objects and reuse them.)

kloffy
I reuse all objects after their lifetime is ended.
e40pud
If you are sure that the drawing code is the root of your performance problems, switching to OpenGL makes sense, as Phil pointed out. I just wanted to note that there might be room for optimization without rewriting the entire drawing logic. Efficient rendering of a particle system using OpenGL isn't trivial...
kloffy
+2  A: 

OpenGL would be the lowest level to drop to for the rendering, so should offer the best performance (if done right). CoreAnimation would be close enough if there are not too many particles (the exact figure depends on other factors, but upto about 50 should be ok). When you say you're using CoreGraphics at the moment do you mean you're redrawing based on a timer? If so then CoreAnimation will definitely help you out - as long as you can seperate out each particle into a view. You could still use CoreGraphics to render the individual particles.

Are you using a physics engine to calculate the positions?

Phil Nash
> When you say you're using CoreGraphics at the moment do you mean you're redrawing based on a timer?Yes. I use timer.> Are you using a physics engine to calculate the positions?A little bit. I use few formulas for trajectory calculation.
e40pud
How many objects (particles) are we talking about?
Phil Nash
Maximum particles count will be ranged from 100 to 200 (maybe 300)
e40pud
Ok - one view per particle probably won't scale to that level - but you could probably still group nearby particles into a fewer number of views. Beyond that I think OpenGL ES is going to be necessary - but be prepared for a bit of a learning curve if you've not done it before.
Phil Nash
Thank you!What do you think about third libraries (such as Cocos2D)? Is it good idea look at it?
e40pud
I've not used them myself but I've seen Cocos2D used by someone else and it looks pretty good. Unless you're invested in writing your own high quality physics engine I'd go with Cocos2D or similar.
Phil Nash
+2  A: 

If you are simply drawing a view with Core Graphics, then redrawing it every frame to reflect the movement of the particles, you will see terrible performance (for more, see this answer). You will want to go to OpenGL ES or Core Animation for the particle system animation.

My recommendation would be to look at CAReplicatorLayer, a new Core Animation layer type added in iPhone OS 3.0 and Snow Leopard. I've seen some impressive particle systems created just using this one layer type, without using much code. See the ReplicatorDemo sample application (for the Mac, but the core concepts are the same), or Joe Ricioppo's "To 1e100f And Beyond with CAReplicatorLayer" article.

Brad Larson