Creating the Device and Viewport

The Direct3D device and viewport are created as part of the application's initialization. After creating a DirectDrawClipper object, the InitApp function calls CreateDevAndView, passing as parameters the DirectDrawClipper object, the driver that was chosen, and the dimensions of the client rectangle.

The CreateDevAndView function uses the IDirect3DRM::CreateDeviceFromClipper method to create a Direct3DRM device, using the driver that was selected by the enumeration process. It uses this IDirect3DRMDevice interface to retrieve the device's width and height, by calling IDirect3DRMDevice::GetWidth and IDirect3DRMDevice::GetHeight methods. After it has retrieved this information, it calls the IDirect3DRM::CreateViewport method to retrieve the IDirect3DRMViewport interface.

When CreateDevAndView has called the IDirect3DRMViewport::SetBack method to set the back clipping plane of the viewport, it calls the locally defined SetRenderState function. SetRenderState is described in the next section, Setting the Render State.

/////////////////////////////////////////////////////////////////////

//

// CreateDevAndView

// Create the D3DRM device and viewport with the given D3D driver and

// with the specified size.

//

/////////////////////////////////////////////////////////////////////

static BOOL

CreateDevAndView(LPDIRECTDRAWCLIPPER lpDDClipper, int driver,

int width, int height)

{

HRESULT rval;

// Create the D3DRM device from this window by using the specified

// D3D driver.

lpD3DRM->lpVtbl->CreateDeviceFromClipper(lpD3DRM, lpDDClipper,

&myglobs.DriverGUID[driver], width, height, &myglobs.dev);

// Create the D3DRM viewport by using the camera frame. Set the

// background depth to a large number. The width and height

// might have been slightly adjusted, so get them from the device.

width = myglobs.dev->lpVtbl->GetWidth(myglobs.dev);

height = myglobs.dev->lpVtbl->GetHeight(myglobs.dev);

rval = lpD3DRM->lpVtbl->CreateViewport(lpD3DRM, myglobs.dev,

myglobs.camera, 0, 0, width, height, &myglobs.view);

if (rval != D3DRM_OK) {

myglobs.dev->lpVtbl->Release(myglobs.dev);

return FALSE;

}

rval = myglobs.view->lpVtbl->SetBack(myglobs.view, D3DVAL(5000.0));

if (rval != D3DRM_OK) {

myglobs.dev->lpVtbl->Release(myglobs.dev);

myglobs.view->lpVtbl->Release(myglobs.view);

return FALSE;

}

// Set the render quality, fill mode, lighting state,

// and color shade info.

if (!SetRenderState())

return FALSE;

return TRUE;

}