Topic : DirectX Programming
Author : Ian Fleeton
Page : << Previous 6  
Go to page :


DirectDraw object. This will fill out a DDSURFACEDESC2 structure with some stuff you already know and some stuff that you want to know.

What you need to look at are the rgb bit masks.

For 555 these would be

  bin              hex
r 0111110000000000 7C00
g 0000001111100000 03E0
b 0000000000011111 001F

For 565 these are

  bin              hex
r 1111100000000000 F800
g 0000011111100000 07E0
b 0000000000011111 001F

Now, by either looking at the result of the green or red bit masks you can determine which mode is being used.

Let's see some code. (It's untested but it should work. If not email me.)

// Create dd object and set coop and mode.
DDSURFACEDESC2 ddsd;
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(ddsd);
dd->GetDisplayMode(&ddsd);
if(ddsd.ddpfPixelFormat.dwRBitMask == 0x7c00)
  { /* 555 */ }
else if(ddsd.ddpfPixelFormat.dwRBitMask == 0xf800)
  { /* 565 */ }
else
  { /* Give up */ }

I advise doing this only after changing the display mode and storing the result.

Perhaps:

enum { RGB_555, RGB_565 } displayMode;
if(ddsd.ddpfPixelFormat.dwRBitMask == 0x7c00)
  { displayMode = RGB_555; }
else if(ddsd.ddpfPixelFormat.dwRBitMask == 0xf800)
  { displayMode = RGB_565; }
else
  { /* Give up */ }


Ok, how do you write pixels to a 16-bit surface? Well, you achieve this in a similar manner to 8-bit. Except this time we'll use unsigned shorts instead of unsigned chars.

typdef unsigned short ushort; // Notational convenience - ushort is now an alias
ushort* videoMemory;
long pitch;

void lock()
{
    DDSURFACEDESC2 ddsd;
    ddsd.dwSize = sizeof(ddsd);
    back->Lock(0, &ddsd, DDLOCK_WAIT, 0);
    videoMemory = (ushort*)ddsd.lpSurface;

    pitch = ddsd.lPitch; // Pitch is in bytes
    pitch >>= 1; // Pitch is now in ushorts
}

void unlock()
{
    back->Unlock(0);
}

void setPixel(int x, int y, ushort c)
{
    videoMemory[x + pitch * y] = c;
}

How to convert true colour to 16-bit.

ushort trueToRGB555(int r, int g, int b)
{
    // Remove least significant bits
    r >>= 3; // 8-bit to 5-bit
    g >>= 3; // 8-bit to 5-bit
    b >>= 3; // 8-bit to 5-bit
    // Now arrange
    r <<= 10;
    g <<= 5;
    ushort c = r + g + b;
    return c;
}

ushort trueToRGB565(int r, int g, int b)
{
    // Remove least significant bits
    r >>= 3; // 8-bit to 5-bit
    g >>= 2; // 8-bit to 6-bit
    b >>= 3; // 8-bit to 5-bit
    // Now arrange
    r <<= 11;
    g <<= 6;
    ushort c = r + g + b;
    return c;
}


Hope it helps. Any problems e-mail Ian Fleeton at ianf@openrpgs.com.

Page : << Previous 6