#region File Description
//-----------------------------------------------------------------------------
// SpriteEffect.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
namespace Microsoft.Xna.Framework.Graphics
{
///
/// The default effect used by SpriteBatch.
///
internal class SpriteEffect : Effect
{
#region Effect Parameters
EffectParameter matrixParam;
#endregion
#region Methods
///
/// Creates a new SpriteEffect.
///
public SpriteEffect(GraphicsDevice device)
: base(device, Resources.SpriteEffect)
{
CacheEffectParameters();
}
///
/// Creates a new SpriteEffect by cloning parameter settings from an existing instance.
///
protected SpriteEffect(SpriteEffect cloneSource)
: base(cloneSource)
{
CacheEffectParameters();
}
///
/// Creates a clone of the current SpriteEffect instance.
///
public override Effect Clone()
{
return new SpriteEffect(this);
}
///
/// Looks up shortcut references to our effect parameters.
///
void CacheEffectParameters()
{
matrixParam = Parameters["MatrixTransform"];
}
///
/// Lazily computes derived parameter values immediately before applying the effect.
///
protected internal override void OnApply()
{
Viewport viewport = GraphicsDevice.Viewport;
Matrix projection = Matrix.CreateOrthographicOffCenter(0, viewport.Width, viewport.Height, 0, 0, 1);
Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0);
matrixParam.SetValue(halfPixelOffset * projection);
}
#endregion
}
}