The second one is a little ‘old school’ demo effect of moving fire. I know it’s not the most convincing version out there but it is a quick and dirty one. Maybe if you play around with the params you’ll get it better. I’m not sure who made the original code. It may even be a mix of several projects.
First we need to setup the fire color palette. There are some formulas out there to generate the colors. A typical one is this (pseudo):
HSLtoRGB is used to generate colors: Hue goes from 0 to 85: red to yellow Saturation is always the maximum: 255 Lightness is 0..255 for x=0..128, and 255 for x=128..255
The next part is the main loop that animates the fire.
'start the loop (one frame per loop) While Not StopFire 'make the background black gBuffer.ForeColor = &c000000 gBuffer.FillRect(0,0,w,h) 'randomize the bottom row of the fire buffer For x = 0 To w - 1 fire(x, h - 1) = Abs(32768 + rnd()*256) Mod 256 + 128 if fire(x, h - 1) > 255 then fire(x, h - 1) = 255 end if Next 'do the fire calculations for every pixel, from top to bottom 'I remember there are other formulas out there that have different effects. Just Google it. For y = 0 To h - 2 For x = 0 To w - 1 fire(x, y) = ((fire((x - 1 + w) Mod w, (y + 1) Mod h) + fire((x) Mod w, (y + 1) Mod h) + fire((x + 1) Mod w, (y + 1) Mod h) + fire((x) Mod w, (y + 2) Mod h)) * 32) / 130 + rnd() Next Next 'set the drawing buffer to the fire buffer, using the palette colors For x = 0 To w - 1 For y = 0 To h - 1 rgbBuffer.pixel(x, y) = palette(fire(x, y)) Next Next 'redraw the screen me.Refresh(false) app.DoEvents wend
That’s it! Or as Tom Hanks would have said in Cast Away: “I have made fire!”
The code and demo can be downloaded from: http://www.gorgeousapps.com/ABFire.zip