Creating a Direct3DRM Device and Viewport

The InitApp function calls the locally defined CreateDevAndView function to create a Direct3DRM device and viewport. This function's parameters are a pointer to a DirectDrawClipper object, the number identifying the current driver, and the width and height of the current window's client rectangle.

After verifying that the window dimensions are valid, CreateDevAndView calls the IDirect3DRM::CreateDeviceFromClipper method to create a Direct3DRM device (stored in the dev member of the myglobs structure).

static BOOL CreateDevAndView(LPDIRECTDRAWCLIPPER lpDDClipper,

int driver, int width, int height)

{

HRESULT rval;

if (!width || !height) {

Msg("Cannot create a D3DRM device with invalid window dimensions.");

return FALSE;

}

// Create the D3DRM device from this window and using the specified D3D

// driver.

rval = lpD3DRM->CreateDeviceFromClipper(lpDDClipper,

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

CreateDevAndView uses the device it just created and the camera frame in a call to the IDirect3DRM::CreateViewport method that creates a Direct3DRM viewport. The width and height of the viewport are retrieved by calling the IDirect3DRMDevice::GetWidth and IDirect3DRMDevice::GetHeight methods. The viewport is stored in the view member of the myglobs structure. After creating the viewport, the code sets its background depth to a large number by calling the IDirect3DRMViewport::SetBack method.

width = myglobs.dev->GetWidth();

height = myglobs.dev->GetHeight();

rval = lpD3DRM->CreateViewport(myglobs.dev, myglobs.camera, 0, 0, width,

height, &myglobs.view);

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

Finally, CreateDevAndView calls the locally defined SetRenderState function to set the rendering quality, fill mode, lighting state, and color shade. This function is described in Setting the Render State.

// Set the rendering quality, fill mode, lighting state and

// color shade information.

if (!SetRenderState())

return FALSE;

return TRUE;

}