Friday, January 6, 2012

Improving The Flame Effect

I originally added the flame effect just to fill the middle space at the top of the screen, and to provide a visual reminder that you can't let the player scroll off the top.  At first it was just a static, irregular image -- first all in red, then in a motley red and orange.

Flicker Or Flash?

I decided I wanted the flames to "flicker".  So I added code in the game loop to use an XOR operation to flip the red bits to orange and vice versa.  At first this was annoyingly fast, so I slowed it down a bit by skipping a set number of frames between each flip.  The result was OK, but very regular -- more like a flash than a flicker.  The colors seemed a bit wrong too.

In the news recently have been some reports of arson out in California.  Obviously I feel terrible for the losses of those involved, but the news reports did give me some really good views of raging flames!  I realized that I needed to add some yellow at the core of the flames, and that the flicker effect needed to be more random.


Games tend to need "random" numbers anyway for a variety of uses.  I knew I would need some sort of (pseudo-)random number generator (PRNG) eventually, so I might as well implement one now.  I decided that a linear feedback shift register (LFSR) would be a good way to go.  An LFSR is relatively simple to implement, reasonably cheap to execute, and can provide convincingly "random" sequences of numbers.

I settled on a a 16-bit LFSR, since that size is reasonably convenient to handle with the 6809's resources.  I used the maximal-length polynomial for 16-bit LFSRs from the table in the LFSR Wikipedia article.  For a random seed I used the Extended Color BASIC TIMVAL variable, which is a free running software counter used by the CoCo's ROM for timing purposes.  I sprinkled-in some assembly code with a few logic operations and I was all set!  For now I am bumping the LFSR after every frame, but I reckon that I might have to hit it more often before this is all over.

Flame On!

Now armed with (pseudo-)random numbers, I could make the flames flicker more realistically.  Instead of flipping colors on a regular basis, I now use the LFSR data to randomize the time between color flips.  Using what I learned from the arson reports on the news, I added some yellow bits to the flame graphics and arranged the red and orange bits with the red on the outside parts and the orange in the middle.  Finally, I made sure that the yellow bits and some of the orange bits were exempted from the flickering.  So now the colors go from yellow/orange/red to yellow/orange and back on an irregular basis.  The overall effect looks pretty good "live", although the video cameras I've tried so far don't seem to pick it up very well.

I've spent more time on these flames than I ever intended to spend.  But, I do think they look pretty good.  I'm not sure if it shows-up very well in the video, so you might just have to take my word for it.  FWIW, I also changed the color selection algorithm for the scroll effects to something that is a bit cheaper to execute.  Anyway, I'll include a video clip just to show where I'm actual game is still to come!

No comments:

Post a Comment