color pixel - easy question ddraw

Started by
6 comments, last by stevenmarky 23 years, 10 months ago
in direct draw how do you find the color of a single pixel at an x,y posistion. I know all about 16-bit color ect, but I don''t know how to implement it. I need to know for an advanced collision map. Thanks. Please help. //end
Advertisement
Um, something like this:

int color;
ddsback->lock();
color = buffer[ x + y * lpitch ];
ddsback->unlock();

I left some stuff out of course, and it''s dog slow.
Um, shouldn''t it be

buffer[ x + y * lpitch ] = color; 


Instead of the other way around? And in 16-bit you have to do some funky multiplies to get it right. Not quite sure where though.

buffer is a USHORT*, btw, which you get from ddsd.lpSurface, ddsd being the DDSURFACEDESC that you passed in to the lock function.


I like food.
I like food.
To READ a pixel, you must first lock the surface:

LPDIRECTDRAWSURFACE surf;
DDSURFACEDESC2 ddsd;
ZeroMemory(&ddsd,sizeof(ddsd));
ddsd.dwSize=sizeof(ddsd);
HRESULT rval=surf->Lock(NULL, &ddsd, DDLOCK_WAIT, NULL);
if (!FAILED(rval)) { ... do pixel manip here ... }


Next, you have to compute the pixel offset in the surface.
DDraw will give you the pitch (distance from one line to the next) in BYTES. if you have a 15, 16, 24, or 32 bit surface, you will have to figure out how many bytes each pixel takes. For 8 bits per pixel, each pixel takes 1 byte. for 15 or 16 bits per pixel, they take 2. It's a little tricky with 24 bits -- sometimes it takes 3 bytes, sometimes it takes 4. You'll have to look at the pixelformat structure for your primary surface to figure it out.

Anyways ... once you know how many bytes per pixel, you just index into the locked surface:

unsigned char *pPixel = ddsd.lpSurface[ (x * bytesPerPix) + (y * ddsd.lPitch) ];

switch(bytesPerPix) {
case 1: pixel = *pPixel; break;
case 2: pixel = *((USHORT*)pPixel); break;
case 3: pixel = pPixel[0]<<16 / pPixel[1]<<8 / pPixel[2];
case 4: pixel = *((DWORD*)pPixel); break;
}


note that the 3-bytes-per-pixel case might be in the wrong order...

How to interpret the bits (which ones are Red, Green, and Blue) comes from the pixelformat structure.

Hope this helps.

(BTW, much of the nasty nitty gritty DirectX details are wrapped up in a nice little library called "NukeDX". check out Nuke Software for more info.

-- Pryankster


Edited by - Pryankster on June 3, 2000 4:52:56 PM
-- Pryankster(Check out my game, a work in progress: DigiBot)
First, you should know that reading a pixel value is exactly like plotting a pixel except your setting a variable equalt to the value in memory instead of setting the value in memory equal to a variable. Of course the variable is a color value

TrigonLoki - acw83''s right, the bit of code you gave would be to plot a pixel.

Anyway here''s my code to do it (in 16 bit mode), i can''t remember exactly why i have to do the "y*(lPitch/2)" but i think you get messed up colors if you don''t.
(src_pixel is a WORD)

src_pixel = buffer[x + y*(lPitch/2)];

After you do this you have to covert to RGB.
In 5-6-5 mode i do this:

#define GetR(c) ((c>>11)&0x1f)
#define GetG(c) ((c>>5)&0x3f)
#define GetB(c) (c & 0x1f)

and then get the RGB from the pixel.

For 5-5-5 mode you have to change the way you get the green value, but i''m not sure what that is (shouldn''t be too hard to find out).

Good Luck

+AA_970+
please, people, dont encourage macros...whats wrong with:

inline int GetRed(const int& nCol)
{
return ( (nCol >> 11) & 0x1f );
}

Edited by - POINT on June 3, 2000 7:05:38 PM

Edited by - POINT on June 3, 2000 7:07:09 PM
if you look in the SetColorKey code of the DDUtil stuff, they use

rgb = GetPixel(m_DC, 0, 0);

which is using the GDI function... is that what you wanted to do?



Calen

~freedom
Calen~freedom
GetPixel() is all well and good, if you''re just grabbing one pixel to check what the colorkey pixel color should be -- but it works with GDI, and requires an HDC. not quick. Generally, you know what format your surfaces are going to be in, or you can write the two-or-three special cases that matter, with DX. it''s the price you pay for speed.

-- Pryankster
-- Pryankster(Check out my game, a work in progress: DigiBot)

This topic is closed to new replies.

Advertisement