Re: How do I use 16-bpp surfaces?

Posted by Dennis Muller on January 20, 1998 at 22:11:33:

In Reply to: How do I use 16-bpp surfaces? posted by Fredrik Andersson on January 14, 1998 at 13:53:09:

: I got a problem with 16-bits-per-pixel DirectDraw surfaces.
: When I try to use the Lock() function to put individual
: pixels in my 16bpp surface, they all go to the left half
: of the surface. Also, they get strange, random-looking colors.
: I tried to use the RGB macro, but it dosen't help.
: As far as I see, the Lock() function returns an
: 8-bit pointer (char*). Can that be used in 16bpp surfaces?
: And, what's the difference between 16bpp and 24bpp,
: except for the color depth?
: Can I use RGB values to specify pixels in a 16bpp
: surface as well? Which mode would you recommend?

: I will be very happy for any help I get! Thanks!

I too once had trouble with the 16 bit pixel depth and had someone clue me in on my mistakes. Here is some code that I use. The LowBitPos, HighBitPos, and GetPixelFormat is modified code from the book Windows Graphics Programming by Stan Trujillo. Sorry not many comments but email me if you have any questions.

// public variables
LPDIRECTDRAW            lpDD;           // DirectDraw object
LPDIRECTDRAWSURFACE     lpDDSPrimary;   // DirectDraw primary surface
LPDIRECTDRAWSURFACE     lpDDSBack;      // DirectDraw back surface
WORD			numRedBits;
WORD			numGreenBits;
WORD			numBlueBits;

WORD LowBitPos( DWORD dword )
    DWORD test=1;
    for (WORD i=0;i>=1; }
    return 0;

void GetPixelFormat(void)
    DDPIXELFORMAT     format;

    ZeroMemory(&format, sizeof(format));
    format.dwSize = sizeof(format);
    if (lpDDSPrimary->GetPixelFormat(&format) == DD_OK)
        //WORD loRedBit = LowBitPos(format.dwRBitMask);
        //WORD hiRedBit = HighBitPos(format.dwRBitMask);
        //numRedBits = (hiRedBit - loRedBit + 1 );

        WORD loGreenBit = LowBitPos(format.dwGBitMask);
        WORD hiGreenBit = HighBitPos(format.dwGBitMask);
        numGreenBits = (hiGreenBit - loGreenBit + 1 );

        if (numGreenBits == 5)
            numRedBits = 10;
            numRedBits = 11;

        //WORD loBlueBit = LowBitPos(format.dwBBitMask);
        //WORD hiBlueBit = HighBitPos(format.dwBBitMask);
        //numBlueBits = (hiBlueBit - loBlueBit + 1 );

WORD RGB16(int iRed, int iGreen, int iBlue)
    WORD Color = ((iRed > 1) + X);
    *lpDDMemory = Color;

void LockBackBuffer(void)
    HRESULT ddrval;
    ZeroMemory(&ddsd, sizeof(ddsd));
    ddsd.dwSize = sizeof(ddsd);
    ddrval = lpDDSBack->Lock(0, &ddsd, DDLOCK_WAIT, 0);

void UnlockBackBuffer(void)

To use, getpixelformat then lock surface, draw pixels, unlock surface, flip or blit surface.

Discuss this article in the forums

Date this article was posted to 7/25/1999
(Note that this date does not necessarily correspond to the date the article was written)

See Also:

© 1999-2011 All rights reserved. Terms of Use Privacy Policy
Comments? Questions? Feedback? Click here!