Search Tips Advanced Search |
| Delphi Graphics and Game Programming Exposed with DirectX 7.0 by John Ayres Wordware Publishing, Inc. ISBN: 1556226373 Pub Date: 12/01/99
Search this book:
Previous | Table of Contents | Next
The first parameter contains a TDDSurfaceDesc2 structure initialized with information about the enumerated display device. This includes information such as the width, height, and color depth. The second parameter contains a pointer to the application-defined value. This function must return DDENUMRET_OK to continue the enumeration; DDENUMRET_CANCEL will stop the enumeration. The TDDSurfaceDesc2 object is complex, and is used in a number of different IDirectDrawSurface4 methods. The TDDSurfaceDesc2 structure is defined as:
    TDDSurfaceDesc2 = packed record

      dwSize: DWORD;                       // size of the TDDSurfaceDesc structure

      dwFlags: DWORD;                      // flags indicating valid members

      dwHeight: DWORD;                     // surface height

      dwWidth: DWORD;                      // surface width

      case Integer of

      0: (

        lPitch : LongInt;                  // distance to start of next row

       );

      1: (

        dwLinearSize : DWORD;              // optimized surface size

        dwBackBufferCount: DWORD;          // number of backbuffers

        case Integer of

        0: (

          dwMipMapCount: DWORD;            // number of mip-map levels

          dwAlphaBitDepth: DWORD;          // depth of alpha buffer

          dwReserved: DWORD;               // reserved

          lpSurface: Pointer;              // pointer to surface memory

          ddckCKDestOverlay: TDDColorKey;  // color key for destination overlays

          ddckCKDestBlt: TDDColorKey;      // color key for destination blt

          ddckCKSrcOverlay: TDDColorKey;   // color key for source overlays

          ddckCKSrcBlt: TDDColorKey;       // color key for source blt

          ddpfPixelFormat: TDDPixelFormat; // pixel format description

          ddsCaps: TDDSCaps2;              // surface capabilities

          dwTextureStage: DWORD;           // multitexture cascade stage

         );

        1: (

          dwRefreshRate: DWORD;            // refresh rate

         );

       );

    end;
The following listing illustrates creating an IDirectDraw4 object and enumerating all supported display modes. Listing 4-2: Creating IDirectDraw and enumerating display modes
    var

      Form1: TForm1;

      FDirectDraw: IDirectDraw4;

    implementation

    function EnumModesCallback(const lpDDSurfaceDesc: TDDSurfaceDesc2;

                               lpContext: Pointer): HResult; stdcall;

    begin

      {list the height, width, and color depth of the enumerated display mode}

      TStringList(lpContext).Add(IntToStr(lpDDSurfaceDesc.dwWidth)+‘ X ’+

                           IntToStr(lpDDSurfaceDesc.dwHeight)+‘, ’+

                           IntToStr(lpDDSurfaceDesc.ddpfPixelFormat.dwRGBBitCount)+

                            bits/pixel’);

      {continue enumeration}

      Result := DDENUMRET_OK;

    end;

    procedure TForm1.FormCreate(Sender: TObject);

    var

      {we can only get a DirectDraw4 interface from the DirectDraw interface, so we

       need a temporary interface}

      TempDirectDraw: IDirectDraw;

    begin

      {create a temporary DirectDraw object. this is used to create the

       desired DirectDraw4 object}

      DirectDrawCreate(nil, TempDirectDraw, nil);

      try

        {we can only get a DirectDraw4 interface through the QueryInterface

         method of the DirectDraw object}

        TempDirectDraw.QueryInterface(IID_IDirectDraw4, FDirectDraw);

      finally

        {now that we have the desired DirectDraw object, the temporary DirectDraw

         object is no longer needed}

        TempDirectDraw := nil;

      end;

      {begin enumerating supported display modes}

      FDirectDraw.EnumDisplayModes(0,nil, lbxDisplayModes.Items, EnumModesCallback);

    end;
Figure 4-3: Supported display modes Setting the Cooperative Level As discussed earlier, the cooperative level determines if the application runs in full-screen mode or if it will share the desktop with other windows. Basically, it determines how the application will work with other applications. The concept of cooperative level is also found in other DirectX components. Setting the cooperative level requires a simple call to the IDirectDraw4’s SetCooperativeLevel method, which is defined as:
    function SetCooperativeLevel

    (hWnd: HWND;               // a handle to the main window

    dwFlags: DWORD             // flags controlling the cooperative level

    ): HResult;                // returns a DirectX error code
The first parameter is a handle to the application’s main window. The second is a series of flags that specify the actual cooperative level. For our discussion, we should pass DDSCL_EXCLUSIVE and DDSCL_FULLSCREEN, which indicates that our application will be responsible for maintaining the entire display. We’ll see an example of its use below. Setting the Display Mode Here’s where the real action takes place. We’ve created the IDirectDraw4 object, allowed the user to select a display mode or determined that our predefined display mode is supported, and we’ve set the cooperative level. All that’s left is to actually change the display mode itself. This is accomplished through IDirectDraw4’s SetDisplayMode method. The SetDisplayMode method is defined as:
    function SetDisplayMode(

    dwWidth: DWORD;               // the desired width, in pixels

    dwHeight: DWORD;              // the desired height, in pixels

    dwBPP: DWORD;                 // the color depth, in bits

    dwRefreshRate: DWORD;         // the refresh rate

    dwFlags: DWORD                // control flags

    ): HResult;                   // returns a DirectX error code
The first and second parameters specify the desired width and height of the display, in pixels, and it must match a supported display mode as returned by the EnumDisplayModes function. The third parameter indicates the number of bits that describe an individual pixel’s color, and again this must match a supported display mode. The refresh rate and flags parameters can both be set to zero. When this function is called, the display mode is immediately switched to the mode specified. The screen will likely go blank, you’ll hear some clicking, and then the display hardware will reset to the specified mode. Now, before we can set the display mode, DirectDraw makes some demands of the main window that must be met before either the SetCooperativeLevel or the SetDisplayMode methods will work. According to the DirectX documentation, the window whose handle is sent to the SetCooperativeLevel method must not have any border, and must be a stay-on-top window. This can be accomplished in Delphi by setting the form’s BorderStyle to bsNone and the FormStyle to fsStayOnTop in the form’s OnCreate event. Only then will the SetDisplayMode method work correctly.
Previous | Table of Contents | Next

Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.