Step 2.1: Initialize DirectDraw

After creating the application window, the first object you will create is the DirectDraw object, which is required to set your application's cooperative level, and to create the surfaces for display and for use as the render target of a rendering device.

The Triangle sample starts performing initialization by creating a DirectDraw object and setting the application's cooperative level, as shown in the following code:

HRESULT Initialize3DEnvironment( HWND hWnd )
{
    HRESULT hr;
 
    // Create a DirectDraw object.
    hr = DirectDrawCreate( NULL, &g_pDD1, NULL );
    if( FAILED( hr ) )
        return hr;
 
    // Get a ptr to an IDirectDraw4 interface. This interface to DirectDraw
    // represents the DX6 version of the API.
    hr = g_pDD1->QueryInterface( IID_IDirectDraw4, (VOID**)&g_pDD4 );
    if( FAILED( hr ) )
        return hr;
 

The preceding code creates a DirectDraw object by calling the DirectDrawCreate DirectDraw global function. It passes NULL in the first parameter to request that the function create a DirectDraw object for the active display driver. For hardware that doesn't support GDI, such as 3-D only hardware, you should explicitly specify the globally unique identifier (GUID) of the desired driver in the first parameter. These GUIDs are normally obtained through enumeration. The second parameter is the address of a global variable that DirectDrawCreate fills with the address of the IDirectDraw interface for the DirectDraw object, and the last parameter is set to NULL to indicate that the new object will not be used with COM aggregation features. If the DirectDraw object is created successfully, the code queries for the latest iteration of the DirectDraw interface, IDirectDraw4.

The sample continues by setting the application's cooperative level, as follows:

    hr = g_pDD4->SetCooperativeLevel( hWnd, DDSCL_NORMAL );
    if( FAILED( hr ) )
        return hr;
 

The sample sets the cooperative level by calling the IDirectDraw4::SetCooperativeLevel method. Setting the cooperative level effectively tells the system whether or not the application will render in full-screen mode or in a window. (Note that some hardware cannot render into a window. You can detect such hardware by checking for the absence of the DDCAPS2_CANRENDERWINDOWED capability flag when you call IDirectDraw4::GetCaps.) The code requests windowed cooperative level, also called the "normal" cooperative level, by including the DDSCL_NORMAL in the second parameter it passes to SetCooperativeLevel. The SetCooperativeLevel method can fail if another application already controls owns full-screen, exclusive mode.

Note  You can include the DDSCL_FPUSETUP cooperative level flag to increase performance. For more information about this cooperative level flag, see DirectDraw Cooperative Levels and FPU Precision. For general information, see Cooperative Levels in the DirectDraw documentation.

Once you create the DirectDraw object and set the cooperative level, you're ready to prepare the surfaces that will be used to contain and display a rendered scene. The Triangle sample does this as discussed in Step 2.2: Set Up DirectDraw Surfaces.