(Mods, if this post is in the wrong section, please help me choose a better one? Thanks!)
I'm sure that a lot of us who are trying to make a 2D game have struggled with making things blow up. Having skills to be both artist and developer seems to be quite rare, and I am no exception to this. Many of us have stumbled upon an old explosion sprite sheet generator written by Cliff Harris at
http://www.positech.co.uk/content/explosion/explosiongenerator.html (and if you haven't, you have now!). I looked at this, but the main problem I had with it was that whatever background color I used to generate the explosion animation would "seep" into the final drawing of my sprites, looking especially weird when drawn over another image. As stated in the URL above, the source code has been lost and is no longer available. I tried various ways to get the background to be completely transparent, but couldn't come up with anything (likely due to lack of knowledge) and pretty much wrote off the sprites as something I couldn't really use they way I intended.
As I got to thinking about it more, I thought I might be able to re-write the sprite sheet generator that Mr. Harris wrote using the same base images he did. I toyed around with it for a couple of days and have come up with an XNA 3.0 version that, while not an exact reproduction of his algorithms (how could they be? noone has the source anymore), does a really good approximation of his explosions. Mr. Harris was also kind enough to allow me to use and redistribute the textures used in the original explosion generator.
Operating the app is pretty straightforward (all user input is handled in Game1.Update()), just press the space bar and it will generate a random explosion in a 64x64 pixel rectangle, store every 5th frame, then rewrite the stored frames into a 5x5 grid (sprite sheet) as a single texture. When the explosion is complete, the sprite sheet will be displayed and immediately to the right of where the explosion was originally generated, a 30 FPS animation of the sprite sheet will loop so you can see what the sprite sheet itself looks like when animated. If you want a different explosion, just press space again and you'll get a new random explosion sprite sheet. If you want to keep what you see, press enter and the app will write the sprite sheet texture as a PNG image with a fully transparent background.
As for the code itself... I'm not the world's best programmer, and I'm still learning XNA on the whole. The algorithms I used are VERY inefficient which is the point of making the explosion into a sprite sheet instead of using it as a "live" particle generator. I'm sure there are better, less intensive ways to accomplish my task, but the end result is what matters here, and I personally think the resulting animations are nice. The code is heavily commented. Heavily. Way more than it probably should be, but I wanted people to be able to learn from the code so I figured more was better in this case. There are a good deal of hard-coded values/magic numbers strewn about the code, but at the moment it does what I want it to and I when I need to reuse it I'll likely make it more flexible. The primary values that will affect how the explosions are generated are stored in consts in #region Members.
Feel free to take this code and do whatever you want with it. Learn from it, modify it, reuse it, whatever you like for whatever purpose you like, I don't care. If you feel like mentioning my name somewhere in whatever-you-do-with-it, that's fine. If not, I'm not going to lose any sleep or chase you down or anything. :) If you make something cool with it, I'd love to see it! I welcome any/all comments and criticisms on the app, but please try to be constructive.
Without any further babbling on my end, you can download the project here:
http://www.fileden.com/files/2009/6/4/2465621/ExplosionGenerator2.1.rar
[Edit: Fixed a bug discovered by zygote from
www.ziggyware.com having to do with the renderTarget object]