Setting the Render State

The InitApp function calls CreateDevAndView, which in turn calls the SetRenderState function to set the rendering quality, fill mode, lighting state, and color shade. Rmmain.cpp calls the SetRenderState function whenever the render state may have changed; for example, whenever the user changes the fill or lighting modes.

SetRenderState calls the IDirect3DRMDevice::SetQuality method to set the rendering quality, IDirect3DRMDevice::SetDither to set the dithering flag, and IDirect3DRMDevice::SetTextureQuality to set the texture quality. Each of these methods has a Get counterpart, which SetRenderState calls before setting any values. If the current value in the myglobs structure is the same as the value retrieved by the Get method, the function takes no further action.

BOOL SetRenderState(void)

{

HRESULT rval;

if (myglobs.dev->GetQuality() != myglobs.RenderQuality) {

rval = myglobs.dev->SetQuality(myglobs.RenderQuality);

}

if (myglobs.dev->GetDither() != myglobs.bDithering) {

rval = myglobs.dev->SetDither(myglobs.bDithering);

}

if (myglobs.dev->GetTextureQuality() != myglobs.TextureQuality) {

rval = myglobs.dev->SetTextureQuality(myglobs.TextureQuality);

}

Now SetRenderState sets the shades, default texture colors, and default texture shades based on the current number of bits per pixel.

·If the device supports only one bit per pixel, SetRenderState uses IDirect3DRMDevice::SetShades to set the number of shades to four; this is the number of shades in a ramp of colors used for shading. SetRenderState also calls IDirect3DRM::SetDefaultTextureShades to set the default number of shades for textures.

·If the device supports 16 bits per pixel, SetRenderState calls an additional method: IDirect3DRM::SetDefaultTextureColors. This method sets the default number of colors used in textures.

·If the device supports 24 or 32 bits per pixel, SetRenderState calls the same methods as for 16 bits per pixel. The only difference is that in this case it sets the shades and default texture shades to 256 instead of 32.

switch (myglobs.BPP) {

case 1:

if (FAILED(myglobs.dev->SetShades(4)))

goto shades_error;

if (FAILED(lpD3DRM->SetDefaultTextureShades(4)))

goto shades_error;

break;

case 16:

if (FAILED(myglobs.dev->SetShades(32)))

goto shades_error;

if (FAILED(lpD3DRM->SetDefaultTextureColors(64)))

goto shades_error;

if (FAILED(lpD3DRM->SetDefaultTextureShades(32)))

goto shades_error;

break;

case 24:

case 32:

if (FAILED(myglobs.dev->SetShades(256)))

goto shades_error;

if (FAILED(lpD3DRM->SetDefaultTextureColors(64)))

goto shades_error;

if (FAILED(lpD3DRM->SetDefaultTextureShades(256)))

goto shades_error;

break;

}

return TRUE;

shades_error:

Msg("A failure occurred while setting color shade information.\n");

return FALSE;

}

The FAILED macro is defined as follows in the Winerror.h header file:

#define FAILED(Status) ((HRESULT)(Status)<0)