2008-03-02

The Secret Posts - Particle Effects

(Written 29/01/2008)

Explosions are hard to draw. I’ve always found them difficult, anyway. At least, I did back in the DPaint days. I haven’t tried drawing any since then because explosions are hard to draw.

This circular logic gave me another idea for the game - a particle system that could be used for explosion effects, negating the need for hand-drawn pixel art explosions. It could double up as a way of drawing smoke trails behind rockets, a smartbomb effect, all sorts of things.

I came up with a simple method for handling all of this that uses two classes. The first is a Particle class, that contains all of the information about that particle (velocity components, acceleration, life duration, etc) and a single “run()” method that will move the particle around and draw it. The second class is a ParticleManager, that includes a vector of particles and handles creation of new particles, deletion of old particles, and shortcuts to creating explosions and the like.

The ParticleManager does one rather cunning thing. If the number of particles in its vector is less than the maximum allowed number, it will append new particles to the end of the vector. However, if the number of particles has already reached the maximum, it treats the vector like a FIFO buffer. It strips the oldest particle off the front and adds a new particle to the end. This way, all particles that the manager is told to create will get a chance to exist.

Getting this running in Windows was easy. Getting it working on the GP2x was rather harder. The problem I ran into was that the SDL example shipped with the Code::Blocks devkit uses a hardware surface for the screen (ie. it uses the framebuffer directly). This flickered like crazy, even with double-buffering enabled. After a lot of fiddling I switched it to using a single-buffered software surface, which solved the problem. The problem, I think, was that the devkit doesn’t come with the latest SDL libs (certainly not paeryn’s hardware-accelerated libs), so the hardware mode wasn’t working properly.

The GP2x may be hopeless as an accurate touchscreen device, but man it can throw pixels around. It shifts 500 particles around the screen in software surface mode without any apparent effort at all.