Platform SDK: DirectX

Step 2.3: Initialize Direct3D

[C++]

This section pertains only to application development in Visual Basic. See Direct3D Immediate Mode C/C++ Tutorials.

[Visual Basic]

After you create the surfaces that your application requires to render and to display a scene, you can begin initializing Direct3D objects by creating a Direct3D7 object, which is used to create all the other objects you will need to render a scene. You create the Direct3D7 object by calling DirectDraw7.GetDirect3D. The following code from the Triangle tutorial application performs this task:

Sub InitD3D()
    Dim d3d As Direct3D7
    Dim ddsd As DDSURFACEDESC2
        
    ' Retrieve a reference to the Direct3D7 class from the
    ' DirectDraw7 object.
    Set d3d = g_dd.GetDirect3D

After creating a Direct3D7 object, you should create a rendering device to call the Direct3D7.CreateDevice method. The CreateDevice method accepts the globally unique identifier (GUID) of the desired device and the DirectDrawSurface7 object, g_ddsBackBuffer, that the device will render to. This surface must have been created as a 3-D device by using the DDSCAPS_3DDEVICE capability.

Although the tutorial application uses hard-coded GUID values, a real-world application should enumerate devices to get a GUID. For information about device enumeration, see Enumerating Direct3D Devices.

(The application in the Triangle tutorial checks the display mode prior to creating the device. If the display is set to a palettized mode, the application exits. Attempting to create a device for a palettized surface that does not have an associated palette will cause the CreateDevice method to fail. This is done for simplicity, A real-world application should create a render-target surface and attach a palette, or require that the user set their display mode to 16-bit color or higher.)

The following code, taken from the tutorial application, checks the display mode:

    g_dd.GetDisplayMode ddsd
    
    If ddsd.ddpfPixelFormat.lRGBBitCount <= 8 Then
        MsgBox "This application does not support screen display " & _
               "modes lower than 16-bit."
        End
    End If

Then the tutorial application creates a rendering device:

    Set g_d3dDevice = d3d.CreateDevice("IID_IDirect3DHALDevice", g_ddsBackBuffer)

The call to Direct3D7.CreateDevice can fail for many reasons. The most likely cause is when the primary display device does not support 3-D features. Another possibility is if the display hardware cannot render in the current display mode. These possibilities should be checked during device enumeration. To keep the code simple, the Triangle tutorial application attempts to create a software rendering device if the hardware device cannot be created.

Note  Even though the CreateDevice method accepts a DirectDrawSurface7 object, it creates a device object that is separate from a DirectDraw surface object. The device uses a DirectDraw surface as a rendering target.

After the device is created, you can create a viewport object and assign it to the device, as described in Step 2.4: Prepare the Viewport.