Holy smoke!
24 Nov 2012Well, not really.
Nostalgia got the better of me today in the form of some good old mode 13 demo code. All the hours I’d blown previously developing little tid-bit apps like this and I never saved off any of my code. Thankfully, I have a good memory and whilst my assembly skills aren’t “top shelf”, they’re certainly up to the task of re-creating this sort of effect.
Smoke, Fire, Flame?
They’re all the same. They work of the same principal.
- Set a palette that suits your effect (yellows, reds, orange for fire), (black to white for smoke)
- Create some noise as far south as you can on the video buffer
- Blur the pixels out on the screen making your resulting pixels ascend and decay
Easy!
On to the code …
So, I’ll present this little demo in a couple of chunks and explain them as I show them. The code is pretty well documented anyway so that reading it line for line should be very self-explanitory.
This is the main program. It needs to drop us into the required video mode, make sure we don’t want to quit (i.e. was there a key hit?), actually perform the effect (the loop of random/average) and then clean up (send us back to text mode, return control to dos).
Giving the effect some colour
Setting up the palette in this type of routine really does determine the “type” of routine that it is. As I’d said above:
- Purely greyscale will give you a smokey effect on screen
- Gradients running through black, red, orange, yellow, white will give you fire/flame
- Black, blue up to purple will give you a cool alcohol type fire
- Greens will give you something alien
The idea is to experiment with palette creation to see what comes out best for you. Here’s how I setup a greyscale palette.
So, just a touch of VGA theory here. Unlike today’s video modes, the 256 colour VGA supported 256 indices that each had an RGB intensity set ranging (0..63) each. Sometime, I don’t know how we ever used this video mode, but we got by – and made some damn cool stuff using it. So, software port 3c7
takes a colour index. 3c8 can be used to read the colour intensities (not used in this program). 3c9
is the port we use to write (r,g,b) intensities. Dividing by 4 allows me to interpolate 0..255 against 0..63 so that 0 is the colour with least intensity up to 255 which has the greatest.
###(not so) Random
Getting psuedo random numbers with nothing in the toolbox is difficult. The method that I’ve used here is to constantly read from software port 40h which is tightly coupled with the timer interrupt but it keeps a fairly steady count. The uniformity of these numbers actually provides a very tame smoke effect, you’re not going to see much chaos.
So, following along with the effect we only randomize the last two rows of the video array. Simple.
Airbrush, Airbrush!!
It’s just an averaging effect. We take the average of the current pixel, left, right and top most. We then re-set the pixel back into video memory 1 pixel above our current location. Therefore, we have no interest in trying to process the top-most row of video memory.
The only other part that is a little awkward to look at in this code block will be the ADC
instructions. We’re dealing with bytes (values of 0..255). We’re adding 4 of these together so we’re going to quickly overflow a byte sized register. ADC
(or add with carry adjustment) allows us to overflow this information into AX
’s higher-order byte (AH
). When it comes time to divide (or take the arithmetic average) of this pixel’s intensity, we’ll be able to perform this operation on the word sized AX
register. Neat. Check it out:
So, when you put it all together (and run it in DosBox) you’ll get something that looks like this:
Well.. I’m feeling all nostalgic now. Might go and fire up DosBox and play a couple of games of Double Dragon.