package org.newdawn.slick.particles; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.security.AccessController; import java.security.PrivilegedAction; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import org.newdawn.slick.Color; import org.newdawn.slick.Image; import org.newdawn.slick.SlickException; import org.newdawn.slick.opengl.TextureImpl; import org.newdawn.slick.opengl.renderer.Renderer; import org.newdawn.slick.opengl.renderer.SGL; import org.newdawn.slick.util.Log; /** * A particle syste responsible for maintaining a set of data about individual * particles which are created and controlled by assigned emitters. This pseudo * chaotic nature hopes to give more organic looking effects * * @author kevin */ public class ParticleSystem { /** The renderer to use for all GL operations */ protected SGL GL = Renderer.get(); /** The blending mode for the glowy style */ public static final int BLEND_ADDITIVE = 1; /** The blending mode for the normal style */ public static final int BLEND_COMBINE = 2; /** The default number of particles in the system */ private static final int DEFAULT_PARTICLES = 100; /** List of emitters to be removed */ private ArrayList removeMe = new ArrayList(); /** * Set the path from which images should be loaded * * @param path * The path from which images should be loaded */ public static void setRelativePath(String path) { ConfigurableEmitter.setRelativePath(path); } /** * A pool of particles being used by a specific emitter * * @author void */ private class ParticlePool { /** The particles being rendered and maintained */ public Particle[] particles; /** The list of particles left to be used, if this size() == 0 then the particle engine was too small for the effect */ public ArrayList available; /** * Create a new particle pool contiaining a set of particles * * @param system The system that owns the particles over all * @param maxParticles The maximum number of particles in the pool */ public ParticlePool( ParticleSystem system, int maxParticles ) { particles = new Particle[ maxParticles ]; available = new ArrayList(); for( int i=0; i 0) { pool.particles[i].update(delta); pCount++; } } } } } } /** * Get the number of particles in use in this system * * @return The number of particles in use in this system */ public int getParticleCount() { return pCount; } /** * Get a new particle from the system. This should be used by emitters to * request particles * * @param emitter The emitter requesting the particle * @param life The time the new particle should live for * @return A particle from the system */ public Particle getNewParticle(ParticleEmitter emitter, float life) { ParticlePool pool = (ParticlePool) particlesByEmitter.get(emitter); ArrayList available = pool.available; if (available.size() > 0) { Particle p = (Particle) available.remove(available.size()-1); p.init(emitter, life); p.setImage(sprite); return p; } Log.warn("Ran out of particles (increase the limit)!"); return dummy; } /** * Release a particle back to the system once it has expired * * @param particle The particle to be released */ public void release(Particle particle) { if (particle != dummy) { ParticlePool pool = (ParticlePool)particlesByEmitter.get( particle.getEmitter() ); pool.available.add(particle); } } /** * Release all the particles owned by the specified emitter * * @param emitter The emitter owning the particles that should be released */ public void releaseAll(ParticleEmitter emitter) { if( !particlesByEmitter.isEmpty() ) { Iterator it= particlesByEmitter.values().iterator(); while( it.hasNext()) { ParticlePool pool= (ParticlePool)it.next(); for (int i=0;i