Microsoft DirectX 8.1 (Visual Basic)

Step 2: Initializing Direct3D

The CreateDevice sample project performs system initialization in its InitD3D application-defined function called from Form_Load after the form is shown. After you display a form for the application, you are ready to initialize the Microsoft® Direct3D® device that you will use to render the scene. This process includes creating a Direct3D object, setting the presentation parameters, and finally creating the Direct3D device.

To create a Direct3D object, use the DirectX8.Direct3DCreate method. You can use this Direct3D object to enumerate devices, types, modes, and so on.

Set g_D3D = g_DX.Direct3DCreate()
If g_D3D Is Nothing Then Exit Function

The next step is to retrieve the current display mode by using the Direct3D8.GetAdapterDisplayMode method as shown in the code fragment below.

Dim Mode As D3DDISPLAYMODE
g_D3D.GetAdapterDisplayMode D3DADAPTER_DEFAULT, mode

The Format member of the D3DDISPLAYMODE type will be used when creating the Direct3D device. To run in windowed mode, the Format member is used to create a back buffer that matches the adapter's current mode.

By filling in the fields of the D3DPRESENT_PARAMETERS object, you can specify how you want your 3-D application to behave. The CreateDevice sample project sets its Windowed member to 1(True), its SwapEffect member to D3DSWAPEFFECT_COPY_VSYNC, and its BackBufferFormat member to mode.Format.

Dim d3dpp As D3DPRESENT_PARAMETERS
d3dpp.Windowed = 1
d3dpp.SwapEffect = D3DSWAPEFFECT_COPY_VSYNC
d3dpp.BackBufferFormat = mode.Format

The final step is to use Direct3D8.CreateDevice to create the Direct3D device, as illustrated in the following code example.

Set g_D3DDevice = g_D3D.CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, _
                                     D3DCREATE_SOFTWARE_VERTEXPROCESSING, d3dpp)
If g_D3DDevice Is Nothing Then Exit Function

The preceding code creates the device with the default adapter by using the D3DADAPTER_DEFAULT flag. In most cases, the system will have only a single adapter, unless it has multiple graphics hardware cards installed. Indicate that you prefer a hardware device over a software device by specifying D3DDEVTYPE_HAL for the DeviceType parameter. This sample uses D3DCREATE_SOFTWARE_VERTEXPROCESSING to tell the system to use software vertex processing. Note that if you tell the system to use hardware vertex processing by specifying D3DCREATE_HARDWARE_VERTEXPROCESSING, you will see a significant performance gain on video cards that support hardware vertex processing.

Now that the required Direct3D objects have been created, the next step, to execute the rendering loop, is described in Step 3: Rendering and Displaying a Scene.