DDRM.EXE: Mixing 2-D DirectDraw Objects With Direct3D

ID: Q152646


The information in this article applies to:
  • Microsoft DirectX Software Development Kit, version 2.0, used with:
    • Microsoft Windows 95


SUMMARY

The DDRM sample demonstrates one method of combining 2-D objects on a DirectDraw surface, such as a background, with Direct3D Retained Mode. It also demonstrates how to lock the primary surface's palette down and 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.


MORE INFORMATION

The following file is available for download from the Microsoft Download Center. Click the file name below to download the file:

Ddrm.exe
For more information about how to download files from the Microsoft Download Center, please visit the Download Center at the following Web address
http://www.microsoft.com/downloads/search.asp
and then click How to use the Microsoft Download Center.

The DDRM sample creates a 640x480 primary surface with one back buffer. It also creates a 640x480 offscreen 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. Following is 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) 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 3D Retained Mode scene
      scene->Move(D3DVALUE(1.0));
      view->Clear();
      view->Render(scene);
      rmdev->Update();

      // Use DDBLTFAST_DESTCOLORKEY to blit the 2D 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 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.


REFERENCES

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

Additional query words: kbgraphic kbfile Direct3D

Keywords : kbfile kbsample kbDirect3dRM KbDirectX kbSDKWin32 kbWinOS95 kbfaq kbDirectX200
Version : WINDOWS:2.0,95
Platform : WINDOWS
Issue type : kbinfo


Last Reviewed: January 21, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.