16-bit Color
Another article covering 16-bit color? Considering how often the issue comes up, why not? Besides, it's by TANSTAAFL.
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;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 TANSTAAFL
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