Archived

This topic is now archived and is closed to further replies.

Mazen

Another part of the "Stupid questions about 16-bit colors"-series...

Recommended Posts

Mazen    122
How do you output pixels in 16-bit mode, if you (for example) would like a red value of 24, a green value of 223, and a blue value of 144? EXAMPLE: video_buffer[x+y*screenwidth] = ???

Share this post


Link to post
Share on other sites
Staffan    122
It depends on which 16-bit mode you're in, it'll most likely, if not always, be 555 or 565. 555 uses 5 bits for each color and leaves the high bit untouched. 565 uses 6 bits for the green and 5 for blue and red.

555: 0RRRRRGGGGGBBBBB
565: RRRRRGGGGGGBBBBB

So, if you're in 555 mode it'd be:
((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3)
And in 565:
((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3)

I hope that answered your question, don't hesitate to ask again if it didn't .

"Paranoia is the belief in a hidden order behind the visible." - Anonymous

Edited by - Staffan on October 31, 2000 10:49:23 AM

Share this post


Link to post
Share on other sites
Mazen    122
Thanks, you''ve solved my little problem (it seems like I’m in a 565 mode), but some new questions has occured:

How do you know if you’re in mode 555 or 565 (or the more unusual modes)?

How does it work in 32-bit mode?

Share this post


Link to post
Share on other sites
Staffan    122
You can use IDirectDrawSurface(insert number here)::GetPixelFormat to get the number of bits used for green, blue and so on.

    

ddPixelFormat ddpf;
memset(&ddpf, 0, sizeof(ddPixelFormat));
ddpf.dwSize = sizeof(ddPixelFormat);

if(lpddsback->GetPixelFormat(&ddpf) != DD_OK) return true;

rbits = 0; while(ddpf.dwRBitMask) { ddpf.dwRBitMask = ddpf.dwRBitMask & (ddpf.dwRBitMask - 1); rbits++; }
gbits = 0; while(ddpf.dwGBitMask) { ddpf.dwGBitMask = ddpf.dwGBitMask & (ddpf.dwGBitMask - 1); gbits++; }
bbits = 0; while(ddpf.dwBBitMask) { ddpf.dwBBitMask = ddpf.dwBBitMask & (ddpf.dwBBitMask - 1); bbits++; }



In 32 bit mode every color occupies 8 bytes, just as in 24 mode. The only difference between 24 and 32 is that the high byte in 32 bit mode is empty (or ex. used as an alpha channel).

24 (888): RRRRRRRRGGGGGGGGBBBBBBBB
32 (888): 00000000RRRRRRRRGGGGGGGGBBBBBBBB
Or even something like: AAAAAAAARRRRRRRRGGGGGGGGBBBBBBBB

"Paranoia is the belief in a hidden order behind the visible." - Anonymous

Share this post


Link to post
Share on other sites
Tooon    122
As obove posts said, it all depends how your graphic card handles 16bit colors (565 or 1555).

What you can do, is to first locate that information by getting the pixelformat from you primarysurface (wont explain this deeply, there is a tutorial here on gamedev about dooing this stuff).

Anyway, I will show you 2 macros for "packing" 3 components into a 16bit color.

565:
PACK_RGB565(r,g,b)((r<<11)|(g<<5)|b)
PACK_RGB1555(r,g,b)((r<<10)|(g<<5)|b)

remeber, if you use a range of 0-255 for your color attributes, you will need to scale down each component to
1555 : red/8 (0-31), green/8(0-31), blue/8(0-31)
565 : red/8 (0-31), green/4(0-63), blue/8(0-31)
before calling the macros.

And, dont have two macros, have one which use the information
from the PixelFormat to pack and scale the components depending on the 16bpp format of your gfxcard.

Hope this helped!

/ Tooon

Share this post


Link to post
Share on other sites