Record and Apply StateBlocks
This example demonstrates how to use state blocks to set and retrieve a device's state.
A StateBlock instance can be used to return a device to a default state. It also can be stored as a member of a class to quickly restore a desired rendering state for that class. A StateBlock is most useful when used to save default device state once a device is created. This allows for rapid restoration of default state when needed during rendering.
To record and apply a state block:
- Call Device.BeginStateBlock to begin recording device state.
- Tell the device the states and values to record.
- Call Device.EndStateBlock to stop recording device state and return a StateBlock instance containing the recorded states.
- Capture the device's current state by calling StateBlock.Capture using the previously obtained StateBlock instance.
- Do some processing that changes device state.
- Restore the device's saved states by calling StateBlock.Apply.
In the following C# code example, device is assumed to be the rendering Device.
[C#]
// begin recording device state
device.BeginStateBlock();
// tell device the states and values to record
device.RenderState.FogStart = 0.3f;
device.RenderState.FogEnd = 100.0f;
device.RenderState.FogColor = Color.Gray;
// stop recording device state
StateBlock sb = device.EndStateBlock();
// save device states recorded above
sb.Capture;
// change device states
device.RenderState.FogStart = 2000.0f;
device.RenderState.FogEnd = 35000.0f;
device.RenderState.FogColor = Color.Yellow;
// render scene w/changed states
device.DrawPrimitives();
// restore device's saved states
sb.Apply();
// render scene w/saved states
device.DrawPrimitives();