Mixing 2-D DirectDraw Objects With Direct3D

Microsoft Corporation

October 6, 1997

Click to copy the files associated with this technical article.

Summary: The DirectDraw®-Direct3D® Retained Mode (DDRM) sample demonstrates one method of combining 2-D objects on a Microsoft® DirectDraw surface, such as a background, with Direct3D Retained Mode. It also demonstrates how to lock down the primary surface palette, how to force Retained Mode to utilize what is in the palette, and leave the entries in it unchanged. Since the palette will not change, the pixels of the 2-D objects you create will not have to change. This sample renders to a full screen, 8 bit-per-pixel DirectDraw surface using double buffering.

The DDRM sample creates a 640x480 primary surface with one back buffer. It also creates a 640x480 off-screen surface, on which it stores a 2-D bitmap image. A palette is created for the 2-D image, and the palette is associated with both the back buffer and the front buffer. It is necessary to associate the palette with the back buffer because the back buffer will be rendered to by Retained Mode. To lock down the palette, you must set the peFlags member of the PALETTEENTRY structure for all palette entries to D3DPAL_READONLY. This will let Retained Mode know that it can use but not change the entries in the palette. After obtaining a handle to the back buffer, a 16BPP z-buffer is created and attached to it. SetPalette() is called to attach the palette to the back buffer, and a destination color key for blit operations is created for the back buffer. The color key is palette index 255 (white), so when you blit to the back buffer, all pixels that are 255 (white) on the surface will be overwritten by the pixels on the surface you are blitting from. All other pixels will be preserved. The following is the manner in which the destination color key is created:

DDCOLORKEY  ddck;
ddck.dwColorSpaceLowValue = 255;
ddck.dwColorSpaceHighValue = 255;
lpDDSBack->SetColorKey(DDCKEY_DESTBLT, &ddck);

After this is done, CreateDeviceFromSurface() is called to create a Retained Mode device for the back buffer.

When setting up the scene for the Retained Mode device, you must set the background color to 255 (white). When doing this, anything you blit to the back buffer will overwrite the background of the Retained Mode scene. Calling SetSceneBackground(D3DRGB(1,1,1)) will set up the background properly if white is the destination color key. It is important that only the background in the 3-D scene contain white pixels. You should choose a background color (destination color key) that you know will never be used on your 3-D objects.

Following is the sequence of steps to take to update and render the display:

{
   HRESULT             ddrval;
   RECT                rcRect;
   DDBLTFX ddBltFx;

   rcRect.left = 0;
   rcRect.top = 0;
   rcRect.right = 640;
   rcRect.bottom = 480;

   // Clear the back buffer.
   ZeroMemory(&ddBltFx,sizeof(DDBLTFX));
   ddBltFx.dwSize = sizeof(DDBLTFX);
   ddBltFx.dwFillColor = 255;
   lpDDSBack->Blt(NULL,NULL,NULL,DDBLT_COLORFILL | DDBLT_WAIT;
   ,&ddBltFx);

   // Update the 3-D Retained Mode scene.
   scene->Move(D3DVALUE(1.0));
   view->Clear();
   view->Render(scene);
   rmdev->Update();

   // Use DDBLTFAST_DESTCOLORKEY to blit the 2-D bitmap image onto the
   // scene, only updating the white pixels
   while( (ddrval = lpDDSBack->BltFast( 0, 0, lpDDSOne, &rcRect,
                 DDBLTFAST_DESTCOLORKEY ) ) == DDERR_WASSTILLDRAWING );
   // Update the primary surface
   while(lpDDSPrimary->Flip( NULL,0 ) == DDERR_WASSTILLDRAWING);
}

Note   In this version of Microsoft Direct3D, you should not change the palette on your primary surface or your back buffer. Retained Mode will not account for the palette entry changes. Microsoft is aware of this problem. The DDRM sample demonstrates this problem by allowing you to change the palette while it is executing.

Note   The DirectDraw code from portions of the DDEX3 sample was used in parts of this sample.