Simple Fire Effect
Older article using a paletted technique in DOS.
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 Okay that should be enough To create a nice fire-clone on the screen now, we need to do 3 steps in a loop:
- Update the fire
- Scroll fire up and smooth it
- Visualize fire (copy to VGA) and swap arrays
I'm going to explain each step:
[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)
Related Tutorials
Procedural Generation of 2D Tile Maps in Games
The article explains how to build a procedural 2D cave map generator using cellular automata. It covers map representat…
Localizing A Game Into French — Which Variant Should You Choose?
So, you’re making an awesome indie game, and now you’re thinking about localizing it into French? Great idea! There ar…
How Long Does It Take To Localize An Indie Game?
So you’re planning on localizing your indie game, but you’re not sure how much time to schedule in. While the timing o…
Creating game videos: best practices and pitfalls to avoid
Game video production: practical tips on how to create a game trailer or teaser that you can be proud of. Give your aud…
Adopting CI/CD: How Midwinter Entertainment iterates at speed with the help of IMS
Game development was once like one long sprint: a huge effort until you reached the finish line — at which point you co…
GameDev.net
5 Things to Consider When Making a Video to Promote Your App or Game
How can you show an app or game in your video in a way that attracts new users? Let’s take a look at what to go by when…
Discussion
More from Adam Parusel
Comparing Shadow Mapping Techniques with Shadow Explorer
New Incentives and a Whole New Platform From The Intel AppUp developer program
The final installment in this series on Intel's AppUp program details additional incentives and opportunities for devel…
The Game Maker
This book excerpt contains the first chapter of the book that introduces beginning game developers to the Game Maker pr…
Discussion