It's the 4th of July today and we're celebrating at ArrayFire! The 4th of July implies fireworks, and fireworks obviously imply particle systems. Particle systems are a collection of many small images or points that can be rendered to represent some complex behaving object. So before we can launch our fireworks, we will need to create a particle system. The large number of particles in a system lends well to GPU computation. Thankfully, ArrayFire's easy to use interface will allow us to do this simply and efficiently. First, let's examine the structure of a typical particle system.

[feature_headline type="left" level="h4" looks_like="h4"]Particle System Structure and Hierarchy
[/feature_headline]
Individual particles in a system typically have a variety of properties that govern their individual behavior. A non-comprehensive list below summarizes some of these:
<ul>
	<li>position</li>
	<li>velocity</li>
	<li>acceleration</li>
	<li>lifespan</li>
	<li>color</li>
	<li>size</li>
	<li>rotation</li>
	<li>etc...</li>
</ul>
The properties are used to simulate the particles motion and appearance. Together, a collection of particles forms a particle system. The particle system holds additional information about the system as a whole. For example, these properties can include:
<ul>
	<li>position of the particle system center of mass</li>
	<li>total # of particles</li>
	<li>type of particle system</li>
	<li>image to draw</li>
	<li># of active particles</li>
	<li>particle system bounding box</li>
	<li>etc...</li>
</ul>
The particle system is responsible for keeping track of all of its particles. It simulates and updates their positions. It draws the particles to the screen. It also keeps track of which particles are alive or need to be respawned.
A single scene might have multiple particle systems at once. Fireworks wouldn't be as exciting if there could only be one explosion at a time, so we'll also need a particle system manager. The manager will keep track of active particle systems and make sure each one of them is updated at the appropriate time. Particle systems can be added and removed from the manager and the manager will keep track of the systems in memory. These functional requirements create a straightforward blueprint for a small collection of classes and functions required to make a particle system.

[feature_headline type="left" level="h4" looks_like="h4"]Basic classes and data structures
[/feature_headline]
Starting from the top down, let's create a particle manager:
<code>
/*
 * Particle Manager Class
 * keeps track of all living particle systems
 */

class particle_manager
{
public:
    //a vector containing pointers to our active systems
    std::vector systems;

    particle_manager() {}
    
    //function to add a system
    void addSystem(particle_system *sys) {
        systems.push_back(sys);
    }

    //used to update all systems
    void update() {
        systems.erase(std::remove_if(systems.begin(), systems.end(),
                    [](particle_system* ps){bool alive = ps-&gt;update(); if(!alive) delete ps; return !alive;}), systems.end());
    }

    //used to render all systems
    void render(af::array &amp;image) {
        for(auto sys : systems)
            sys-&gt;render(image);
    }
};
</code>
