There are several parts to a fire effect. All these are done in an offscreen buffer, which is then copied over to the screen. The buffer typically consists of an array of bytes.
2. Heat Spread
pixel(x,y) = (pixel(x,y-1) + pixel(x,y+1) + pixel(x-1,y) + pixel(x+1,y))
-----------------------------------------------------------
4
Simple and fast. I will explain other smoothing techniques later which allow you to make the
convection proportional to the temperature of the pixel.3. Cooling
4. Convection
Uneven Cooling
pixel(x,y) = pixel(x,y) - coolingmap(x,y)
Right, so now we've got that out of the way, it's time for some pseudocode.
INTEGER: xsize
INTEGER: ysize
INTEGER: c
INTEGER: n1
INTEGER: n2
INTEGER: n3
INTEGER: n4
INTEGER: p
ARRAY_OF_BYTES: buffer1(xsize*ysize)
ARRAY_OF_BYTES: buffer2(xsize*ysize)
ARRAY_OF_BYTES: CoolingMap(xsize*ysize)
loop forever
loop y from 1 to (ysize-2) ;Loop through all pixels on the screen, except
loop x from 1 to (xsize-2) ;the ones at the very edge.
n1 = read pixel from buffer1(x+1, y) ;Read the 4 neighbouring pixels
n2 = read pixel from buffer1(x-1, y)
n3 = read pixel from buffer1(x, y+1)
n4 = read pixel from buffer1(x, y-1)
c = read pixel from CoolingMap(x, y) ;Read a pixel from the cooling map
p = ((n1+n2+n3+n4) / 4) ;The average of the 4 neighbours
p = p-c ;minus c
if p<0 then p=0 ;Don't let the fire cool below zero
write pixel of value p to buffer2(x,y-1) ;write this pixel to the other buffer
;notice that it is one pixel higher.
end x loop
end yloop
copy buffer2 to the screen ;Display the next frame
copy buffer2 to buffer1 ;Update buffer1
scroll CoolingMap up one pixel
end of loop
Sparks
Fire Balls