But before we're going to talk about the algorithm we have to define two 2d-arrays [x,y] with equal sizes. Let's call them Buffer1 and Buffer2. The size of those arrays depends on the size you want to have the fire at (e.g. 320x200 = 64000 bytes).
The next thing we need to define is a palette with gradient colors like black-orange-white (0-255) (but every other palette is also possible). This is IMHO the hardest part ... to create a good palette
Let's take a closer look at the structure of a flame:
- Fire is constantly moving up
- It's hot at the bottom (white) and cools down at the top (black)
- Fire doesn't look pixelated but smoothed
- Sometimes we see some hot sparks/spots
- Update the fire
- Scroll fire up and smooth it
- Visualize fire (copy to VGA) and swap arrays
[size="5"]Step 1
Put some coal on the ground which should be burned: Fill the bottom of Buffer1 with a value between, let's say, 100-150 (white/yellow) ... play around with this!
for x <- 0 to 319
{
Buffer1 [x , 198] = random_value (100-150)
Buffer1 [x , 199] = random_value (100-150)
}
for x <- 0 to random_value (10-50)
{
Buffer1 [x-1,197] = 255
Buffer1 [x ,197] = 255
Buffer1 [x+1,197] = 255
Buffer1 [x-1,198] = 255
Buffer1 [x ,198] = 255
Buffer1 [x+1,198] = 255
Buffer1 [x-1,199] = 255
Buffer1 [x ,199] = 255
Buffer1 [x+1,199] = 255
}
[size="5"]Step 2
Now ... to move the fire up we will have to copy each pixel to the line above. To cool the fire down at the top we just decrement the value of each pixel before we move it up. But if we did only this two steps the fire would look very pixelated ... one couldn't even say "fire" to it What we are doing to let it look more realistic is smoothing: Instead of copying the decremented value of each pixel to the line above, we copy the average value of the surrounding pixels to the line above (decremented by one of course - to cool down). Sounds confusing ?!? Okay let me explain it. If we only scrolled the fire up and cooled it down it would look like this: Attention: the value of the pixel will be read from Buffer1 but written to Buffer2! Otherwise it could look a little bit crazy (or at least it won't be the effect we want to see) %-)
for x <- 0 to 319
{
for y <- 1 to 199
{
Buffer2 [x,y-1] = Buffer1 [x,y] - 1
}
}
Notice: you can also take the 8 surrounding pixels (upper left, upper, upper, right, left,...)
for x <- 0 to 319 {
for y <- 1 to 199 {
Buffer2 [x,y-1] = (Buffer1 [x-1,y] + Buffer1 [x+1,y]
+ Buffer1 [x,y-1] + Buffer1 [x,y+1]) / 4 - 1
}
}
[size="5"]Step 3
Know you have to copy Buffer2 to the VGA memory and then you should copy Buffer2 into Buffer1 so that this will be the destination array in the next loop ! Notice: to copy Buffer2 into Buffer1 it's sufficient to swap the memory-addresses of the 2 arrays (you should have used two virtual screens instead of normal arrays and those buffers should be accessed through direct memory access (SEGMENT : INDEX), now just swap the segment addresses... if you don't understand it or have problems with this, go and read Denthor's Tutorials in the graphics section (Virtual screens)).
I hope this article could help you!
[size="3"]Literature
[1] PC UNDERGROUND, Data Becker, ISBN: 3-8158-1185-6 (german)