问题
One part of my app shows a landscape, but it's kinda boring as of now. Therefore, I'm planning to animate some particles over the screen (think of something like tiny wings - http://www.youtube.com/watch?v=DpmcX-rWGfs). However, i have not yet found any built-in particle system; How can i do this memory-efficiently? I've already implemented my own animation system for some clouds animating over the landscape using CADisplayLink, and it's kind of sluggish (though i hope to make it faster soon). Another very heavy system, like animating 20 small points at a time i suppose, will probably break it.
回答1:
I have not yet found any built-in particle system;
There are several "Free" projects that embed particle systems out there, you can visit this video tutorial for a Particle System that will be more than efficient enough for the needs that you mention. That way of making a particle system is basically the same way that Cocos2d is using, so watch the tutorial, and then download the project files, you can easily embed their Particle Emitter in your project.
How can i do this memory-efficiently?
I would recommend you using the "Object Pool Pattern", basically you preallocate a "Pool" of particles, let's say 1000 objects. Then your emitters, ask the pool for particles when they need them. If the pool get's empty you can manage the case accordingly. This may not look efficient from a memory perspective, but it's very efficient performance wise ( to avoid real time allocation of many small objects like particles ).
Some suggestions, when you declare your particle structure try to be lightweight and aligned to powers of 2 ( this will make your structure more cache friendly ), this one is 32 bytes:
struct Particle
{
CGPoint position;
CGPoint speed;
float life;
float decay;
unsigned short index;
unsigned char color_R;
unsigned char color_G;
unsigned char color_B;
unsigned char color_A;
unsigned char rotation;
};
But depending of your needs, this could be much smaller, maybe you don't really need color/index/etc. You will have to evaluate that yourself.
Finally I would recommend you to take a look at cocos2d CCParticleSystem
class, you can download the code here. Their implementation is not that lightweight, but it's quite flexible and can achieve very nice effects.
Good luck with your particles :)
回答2:
You should check out the new book iOS Recipes written by Matt Drance:
http://pragprog.com/titles/cdirec/ios-recipes
Among the recipes is one to make use of the built-in CoreAnimation CAReplicatorLayer to build an emitter in "Construct a simple Emitter".
The book is still in alpha but if you buy it now you get updates as it is finished.
回答3:
Cocos2D is a 2D graphics engine that uses OpenGL ES 1 for rendering. It comes with a built-in particle system. The code of the particle engine is quite simple. It uses VBOs to draw textured quads for the particles. You should be able to adapt that to your own needs.
回答4:
If you want to target iOS5 and above, you can use the built-in facilities of the OS, which is the CAEmitterLayer.
来源:https://stackoverflow.com/questions/5407046/drawing-particles