Spooky Pooky #39 - Particles
Published on 27 Jan 2016 by Joe
An aside about the particle system.
Each particle is a simple struct containing the usual position, velocity, animation and some behaviour flags.
The animation is the same stuff as used elsewhere - a simple array of frames (coords into a spritesheet / delay) plus some behaviour flags and render depth.
I pre-allocate a pool of particles in the game init code (currently only 5000 big) plus a list that hooks into the pool for active particles (I have simple + fast implementations of pools and linked lists).
Each tick the particles are updated, returned to the pool if expired. Their flags indicate if they need collision detection with the player / world, but most don't (e.g. steam particles collide with the player so that they can be wafted along depending on how fast the player is moving).
When something needs to throw out a bunch of particles it calls some emitter code that generates them, allocating from the pool and adding to the list.
For rendering it all goes into the same batch system that I use for all the other sprites. Each particle can have it's own depth and rendering requirements (e.g. blending mode / alpha) and they just end up sorted into batches with the rest of the stuff and flushed out to OpenGL using as few draw calls as possible.
At the numbers I have in the game at the moment they have a negligible effect on performance - nothing like my previous game, QB1-0: