Microsoft DirectX 8.1 (C++) |
The CreateDevice sample project performs Microsoft® Direct3D® initialization in the InitD3D application-defined function called from WinMain after the window is created. After you create an application window, you are ready to initialize the Direct3D object 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.
After creating a Direct3D object, you can use the IDirect3D8::CreateDevice method to create a Direct3D device. You can also use the Direct3D object to enumerate devices, types, modes, and so on. The code fragment below creates a Direct3D object with the Direct3DCreate8 function.
if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) ) return E_FAIL;
The only parameter passed to Direct3DCreate8 should always be D3D_SDK_VERSION. This informs Direct3D that the correct header files are being used. This value is incremented whenever a header or other change would require applications to be rebuilt. If the version does not match, Direct3DCreate8 will fail.
The next step is to retrieve the current display mode by using the IDirect3D8::GetAdapterDisplayMode method as shown in the code fragment below.
D3DDISPLAYMODE d3ddm; if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) ) return E_FAIL;
The Format member of the D3DDISPLAYMODE structure 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 you can specify how you want your 3-D application to behave. The CreateDevice sample project sets its Windowed member to TRUE, its SwapEffect member to D3DSWAPEFFECT_DISCARD, and its BackBufferFormat member to d3ddm.Format.
D3DPRESENT_PARAMETERS d3dpp; ZeroMemory( &d3dpp, sizeof(d3dpp) ); d3dpp.Windowed = TRUE; d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; d3dpp.BackBufferFormat = d3ddm.Format;
The final step is to use the IDirect3D8::CreateDevice method to create the Direct3D device, as illustrated in the following code example.
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) )
The preceding code sample 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 code 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 Direct3D has been initialized, the next step is to ensure that you have a mechanism to process system messages, as described in Step 3: Handling System Messages.