A few days ago, I was working on my old computer, an ATI VGA Wonder. It had 5 bits for each R,G, or B value(using only 15 bits), so the pixel format looked something like this
XRRRRRGGGGGBBBBB
Now, my new computer has a better video card. It uses all 16 bits, making the pixel format look like:
RRRRRGGGGGGBBBBB
So, I got to thinking... how am I going to make it so that I can specify any color to put on the screen, and have it turn out right, no matter what video card it is.
As a side note, I have written my own bitmap loader to read in 24 bit BMPs, and rectify them to a 16 bit surface (I'll probably share it at some later time, most likely in a separate article)
I sat down and set about my task. The first thing I did was open up the DirectX.hlp file, and took a look at the IDirectDrawSurface Interface. One of its member functions was called "GetPixelFormat". Okay. I had something to work with.
GetPixelFormat takes a pointer to a DDPIXELFORMAT structure (not surprisingly). I looked at the DDPIXELFORMAT structure, and I found a couple of fields that interested me... namely dwRBitMask, dwGBitMask, dwBBitMask.
Now I had all of the information I wanted, just not in a format I could use.
DDPIXELFORMAT ddpf;
memset(&ddpf,0,sizeof(DDPIXELFORMAT);
ddpf.dwSize=sizeof(DDPIXELFORMAT);
lpDDS->GetPixelFormat(&ddpf);
Well, in order to figure out how far to shift bits to the left, I had to do this:
//warning, this code is UGLY
DWORD dwBMask=ddpf.dwBBitMask;
DWORD dwRMask=ddpf.dwRBitMask;
DWORD dwGMask=ddpf.dwGBitMask;
DWORD dwBSHL=0;
DWORD dwRSHL=0;
DWORD dwGSHL=0;
while((dwBMask & 1)==0)
{
dwBMask=dwBMask >> 1;
dwBSHL++;
}
while((dwGMask & 1)==0)
{
dwGMask=dwGMask >> 1;
dwGSHL++;
}
while((dwRMask & 1)==0)
{
dwRMask=dwRMask >> 1;
dwRSHL++;
}
Color=Blue << dwBSHL + Green << dwGSHL + Red << dwRSHL;
So, let's determine how many bits to the right we have to shift them:
DWORD dwBSHR=8;
DWORD dwRSHR=8;
DWORD dwGSHR=8;
while((dwBMask & 1)==1)
{
dwBMask=dwBMask >> 1;
dwBSHR--;
}
while((dwGMask & 1)==1)
{
dwGMask=dwGMask >> 1;
dwGSHR--;
}
while((dwRMask & 1)==1)
{
dwRMask=dwRMask >> 1;
dwRSHR--;
}
Color=(Blue >> dwBSHR) << dwBSHL + (Green >> dwGSHR) << dwGSHL + (Red >> dwRSHR) << dwRSHL;