Friday, February 21, 2020 | Toby Opferman

## Fire Algorithm

```Toby Opferman
http://www.opferman.net
programming@opferman.net

FIRE!

Who hasn't done a Graphic Fire?  Obviously you if you are reading this.
There are MANY differnt ways that a fire can be generated and all have
differnt looks/effects depending on how you create it.  This is one way.
Any variation to this format will alter the fire's look, size and shape.

First thing you may want to do is set a Pallete.  The higher color values
in the pallete will be the ones at the bottom of the fire (Hotter) and the
ones lower will be twards the TIP of the fire (Colder).  I have included a
fire pallete ```
(FIRE.PAL)
``` which has a decent fire pallete.  It was stripped off
a .PCX file, so To load it, open it from begining of file and load
RGB (3 Bytes) and Divide the numbers by 4.

(In C Code)
R = fgetc(PCXPallete)>>2;
G = fgetc(PCXPallete)>>2;
B = fgetc(PCXPallete)>>2;

(*The Fire Pallete was made by BEZ, I am color blind*)

And just load those values into the Pallete Registers starting at 0 - 256.
(NOTE, that is a 256 Color Pallete file).

Ok, first step of the Fire algorithm is Randomly plotting pixels
to the bottom of the fire.  You may randomly plot the bottom row, or the
bottom 2 rows, it is up to you.

Depending on how you randomly plot the bottom rows will affect(*By 50%*):
#1 Size Of Fire
#2 Color Of Fire
#3 Shape Of Fire

Options For Pixel Location:
Plot randomly a certain amount of pixels in random spots
Plot Every pixel

Options For Color:
Randomly choose between ENTIRE Pallete
Randomly plot from a certain amount (Like Colors 200 - 255 Will be
randomly plotted)

NOTE: Size of pallete will also determine size of fire (By 50%)
Pallete of 0-100 will be smaller fire than 0-255

Now that you have determined the top, the other 50% that determines
size, shape and hieght of the fire is the fire algorithm itself.

The Fire algorithm takes up some serious CPU processing, so depending
on how you do your fire will judge on how fast it is.

+---------------------+
|  1  |    2    |  3  |
|-----|---------|-----|
|  4  |    5    |  6  |
|-----|---------|-----|
|  7  |    8    |  9  |
+---------------------+

The above is a pixel map. You must now loop to _EVERY_ Pixel on the screen
starting at row TOP + 1 and ending with BOTTOM - 1. of the buffer.

Now, you are at Location 5 Every time.   Every current pixel you are at
is location five when the algorithm takes place.  Every New Pixel will occur
at location two.  This is how the fire grows.  What you do here can vary.
If I were you, I would experament and try differnt techniques to see how
the fire grows.  I will give you ONE variation.

You take Pixel Location 5 and you add it with 3 other locations.

I usually choose:  L7 + L5 + L8 + L6 or L4 + L5 + L8 + L6
Then I divide it by 4 (Take the average)

Which can be a shift >>2 or SHR AX, 2

If you do just what I show up there, it will work to a good fire, but
the fire may SMEAR a bit and look a little bit shitty and not clean
and cut looking.  So, what I do now is Subtract 1 from the average (IF
The Average is NOT 0).

ASM:
SHR AX, 2
JZ PLOT
DEC AX

or

C:

if((NewColor>>2))
NewColor--;

And that is all.  Do that to EVERY pixel!  Now you see why a full
screen fire algorithm CAN get slow that is why it is critical how
you make your calculations.  If you wanted to do a bunch of shit at one
time, your fire may have to suffer some in order to be a bit faster.

```

About Toby Opferman

Professional software engineer with over 15 years...

Learn more »
Codeproject Articles

Programming related articles...

Articles »
Resume

Resume »
Contact

Email: codeproject(at)opferman(dot)com