Playing with the Palette
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaBack to the Top ->
I do not take any responsiblity if the code, ideas, etc. causes your computer to :
With that read, and understood, let's get on with the tutorial and have some fun :)
The palette is basically an array of  representing the 256 colors that mode13h supports and the 3 intensities, red, green, and blue.
When using, or manipulating, the palette, you can achieve some cool effects for your games and/or demos as you'll later see. Some of the more popular, that I'll mention later, are the fade and palette rotation routines.
Also, it would be good, for both me and you, if you read through this section two or more times. It'd help you because you'd understand it better, and it'd help me because, well...um....nevermind.
The parts that this section includes are :
SETTING A COLOR :
This is a simple, yet very important thing to learn if you want to become a master of the palette.
Basically, all you have to do is write the color you want to change into port 0x03C8, and then write it's intensities of red, green, and blue into port 0x03C9.
Here's a function to do that for you...
void set_pal(unsigned char color, unsigned char red
aaaaaaaaaaaaaunsigned char green, unsigned char blue)
Now let me explain how you would use it.
Let's say you want to take the color red, which is the number 12, and change it from red to purple. All you would do is input the number 12 as the first parameter, then enter in the intensities that you want it to be changed to, which would be 63, 0, 63 if you wanted it to become purple.
For our example, the function would look like this as real code.
void set_pal(12, 63, 0, 63);
Not to hard, huh?
READING A COLOR :
Here's yet another one of those must-know routines in order to fully understand the palette.
All you do when you read in a color from the palette is you enter in the number of the color you want to read from into port 0x03C7, and then enter in the values of red, blue, and green into port 0x03C9.
Here comes a sample function :
void get_pal(unsigned char color, unsigned char &red,
aaaaaaaaaaaaaunsigned char &green, unsigned char &blue)
aaared = inp(0x03C9);
aaagreen = inp(0x03C9);
aaablue = inp(0x03C9);
Now let me explain how to use this wonderous function.
To use it type in the number of the color you want to read, say 15 which is white. Then enter in the values of red, green,and blue. It'll then read in the color. One example of this code is...
void get_pal(15, 12, 23, 24);
No prob, right? Right?
VERTICAL RETRACE :
Ok, listen up and read carefully because this is a very important function.
If you have ever played around with the palette before, then you might have noticed that when you do some palette procedures the screen seems to gather some fuzz or 'snow' as it's called. Now the way that this little monster is stopped is as follows...
There is an electronic beam moving up and down your monitor's screen updating it's display. As it gets to the bottom of your screen, it takes it a little while to get back up to the top. This is what we call the vertical retrace.
During this, we can update the screen, doing whatever it is that we may have todo, without having it appear on screen intill it is completely through been done, thus getting rid of the on screen fuzz.
Just a little thing to note is that, if you ever get into Windoze's programming, the upcoming vertical retrace function will not have to be used if you mess with the palette, since Windoze is fast enough to update the screen without the extra fuzz. Poor old DOS.
Here's the important vertical retrace function...
All you have to do to use it is call it when you need it.
GRABBING THE PALETTE :
A simple description of what this does is that it takes the current palette, whatever that may be, and stores it into a nice, neat little array.
One thing to keep in mind when doing this is to make the array in which you are going to store the palette in as big as palette, less it won't work. It will compile, I think, but it won't work.
Also, you'll need to define a global array that will store your array such as the one below...
unsigned char pal;
Ok then, on with the code.
aaa int loop1;
aaa for(loop1 = 0; loop1 < 256; loop1++)
aaaaaaget_pal(loop1, pal[loop1], pal[loop2], pal[loop3]);
That's all there is to it. This function, when called, will store the palette into the array specified.
If you change the name of the global array, make sure you change it in the function too, or errors will appear out of thin air.
See I told you that the get_pal() function would come in handy sooner or later.
BLACKING OUT :
This isn't really a necessary function, but you'll most likely need it when, and if, you want to try out/use the fade-up function(s).
Basically, what this does is it puts a black mask across your screen, so that if you had a pixel on the screen and then you called the blackout function, it wouldn't erase your pixel, it would just cover it up with a black "cover".
The function for this little do-dad is below...
aaafor(loop1 = 0; loop1 < 256; loop1++);
aaaaaaset_pal(loop1, 0, 0, 0);
This is another simple function to use. All you have todo, like the w_retrace() function above, is call it when and where needed.
Playing with the Palette by Keith Dowd
TAOTA of PyroMantics Studios
ICQ# - 20099215
"...and the rivers became as blood."