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

Last reviewed: September 5, 1997
Article ID: Q152646
4.00 WINDOWS kbprg kbgraphic kbfile

The information in this article applies to:

  • Microsoft DirectX 2 Software Development Kit (SDK), for Windows 95

SUMMARY

The DDRM sample demonstrates one method of combining 2D 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 2D objects you create will not have to change. This sample renders to a full screen, 8 bit-per-pixel DirectDraw surface using double buffering.

You can find DDRM.EXE (size: 59165 bytes) 
                     , a self-extracting file, on these services:

  • Microsoft's World Wide Web site on the Internet

          On the www.microsoft.com home page, click the Support icon
          Click Knowledge Base, and select the product
    
          Enter kbfile DDRM.EXE (size: 59165 bytes) 
                               , and click GO!
          Open the article, and click the button to download the file
    
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the Softlib/Mslfiles folder
          Get DDRM.EXE (size: 59165 bytes) 
    

    - Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download DDRM.EXE (size: 59165 bytes) 
    
    
    For additional information about downloading, please see the following article in the Microsoft Knowledge Base:

       ARTICLE-ID: Q119591
       TITLE     : How to Obtain Microsoft Support Files from Online Services
    
    

    MORE INFORMATION

    The DDRM sample creates a 640x480 primary surface with one back buffer. It also creates a 640x480 offscreen surface, on which it stores a 2D bitmap image. A palette is created for the 2D 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 3D scene contain white pixels. You should choose a background color (destination color key) you know will never be used on your 3D 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 reference words: 4.00
    KBCategory: kbprg kbgraphic kbfile
    KBSubcategory: Direct3D
    Keywords : Direct3D kbfile kbgraphic kbprg
    Technology : kbDirectXSDK
    Version : 4.00
    Platform : WINDOWS


    THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

    Last reviewed: September 5, 1997
    © 1998 Microsoft Corporation. All rights reserved. Terms of Use.