Advertisement

Color scan in DirectDraw?

Started by April 29, 2001 04:58 PM
1 comment, last by Just_Me 23 years, 9 months ago
Hello, Does anyone know how I can scan a 16-bit bitmap file used as a background in my game for certain colors? I need it to check if the player is (for example) in water (blue) or on the road (grey) or something. It basically has to check if a certain pixel is a certain color. Im using DirectDraw 6.0. p.s. I use the libraries of Andre LaMothe made for the book Tips and Tricks of the Windows Game Programming Guru''s, and in the library already is a function ColorScan for 16 bit colors, but that doesnt work . thx in advance
Well, you''d lock the surface, and then check the actually surface data for certain values.
Advertisement
More specifically, lock the surface using the DDLOCK_SURFACEMEMORYPTR flag so that you get a pointer to the surface data. Be sure to cast the surface pointer to a USHORT if you''re in 16-bit mode, and divide the surface pitch in half so that it''s in USHORTs instead of bytes. You would use:

USHORT *lpSurf = ddsd.lpSurface;
int nPitch = ddsd.lPitch >> 1;

if ddsd is the DDSURFACEDESC2 whose address you passed to lock. Now, you locate the pixel at location (x, y) using the simple equation x + y * nPitch, and read the color value into a USHORT.

USHORT wColor = lpSurf[x + y * nPitch];

That is the 16-bit color word for that particular pixel of the background. If you''re reading this at the player''s current location, make sure you do it before you actually draw the player, or you''ll read the color in the player sprite instead of the color in the background. Once you have the color word you can break it up into its components using the bit masks that LaMothe describes in his book.

-Ironblayde
 Aeon Software

Down with Tiberia!
"All your women are belong to me." - Nekrophidius
"Your superior intellect is no match for our puny weapons!"

This topic is closed to new replies.

Advertisement