using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace Kav
{
    public class SimpleDepthEffectInstanced : Effect
    {
        EffectParameter viewProjectionParam;

        Matrix view;
        Matrix projection;

        EffectDirtyFlags dirtyFlags = EffectDirtyFlags.All;

        public Matrix View
        {
            get { return view; }
            set
            {
                view = value;
                dirtyFlags |= EffectDirtyFlags.ViewProj;
            }
        }

        public Matrix Projection
        {
            get { return projection; }
            set
            {
                projection = value;
                dirtyFlags |= EffectDirtyFlags.ViewProj;
            }
        }

        public SimpleDepthEffectInstanced(GraphicsDevice graphicsDevice) : base(graphicsDevice, Resources.SimpleDepthEffectInstanced)
        {
            CacheEffectParameters();
        }

        protected override void OnApply()
        {
            if ((dirtyFlags & EffectDirtyFlags.ViewProj) != 0)
            {
                Matrix.Multiply(ref view, ref projection, out Matrix viewProjection);
                viewProjectionParam.SetValue(viewProjection);

                dirtyFlags &= ~EffectDirtyFlags.ViewProj;
            }
        }

        private void CacheEffectParameters()
        {
            viewProjectionParam = Parameters["ViewProjection"];
        }
    }
}