Thursday, May 13, 2010

XNA Restore SpriteBatch changes on GraphicsDevice

Restore SpriteBatch changes of RenderState and SamplerStates on GraphicsDevice to default:

public void SetGraphicsDeviceStates()
{
// SpriteBatch changes:
//_graphicsDevice.RenderState.CullMode = CullMode.CullCounterClockwiseFace;
//_graphicsDevice.RenderState.DepthBufferEnable = false;
//_graphicsDevice.RenderState.AlphaBlendEnable = true;
//_graphicsDevice.RenderState.AlphaBlendOperation = BlendFunction.Add;
//_graphicsDevice.RenderState.SourceBlend = Blend.SourceAlpha;
//_graphicsDevice.RenderState.DestinationBlend = Blend.InverseSourceAlpha;
//_graphicsDevice.RenderState.SeparateAlphaBlendEnabled = false;
//_graphicsDevice.RenderState.AlphaTestEnable = true;
//_graphicsDevice.RenderState.AlphaFunction = CompareFunction.Greater;
//_graphicsDevice.RenderState.ReferenceAlpha = 0;
//_graphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Clamp;
//_graphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Clamp;
//_graphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Linear;
//_graphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Linear;
//_graphicsDevice.SamplerStates[0].MipFilter = TextureFilter.Linear;
//_graphicsDevice.SamplerStates[0].MipMapLevelOfDetailBias = 0.0f;
//_graphicsDevice.SamplerStates[0].MaxMipLevel = 0;

// Correct SpriteBatch changes:
_graphicsDevice.RenderState.DepthBufferEnable = true;
_graphicsDevice.RenderState.AlphaBlendEnable = false;

_graphicsDevice.RenderState.SourceBlend = Blend.One;
_graphicsDevice.RenderState.DestinationBlend = Blend.Zero;

_graphicsDevice.RenderState.AlphaTestEnable = false;
_graphicsDevice.RenderState.AlphaFunction = CompareFunction.Always;

_graphicsDevice.SamplerStates[0].AddressU = TextureAddressMode.Wrap;
_graphicsDevice.SamplerStates[0].AddressV = TextureAddressMode.Wrap;
_graphicsDevice.SamplerStates[0].MagFilter = TextureFilter.Point;
_graphicsDevice.SamplerStates[0].MinFilter = TextureFilter.Point;
_graphicsDevice.SamplerStates[0].MipFilter = TextureFilter.None;
}