/*
 	                    GraphiXNA Shading Library
    Copyright (C) 2007-2008 Mahdi Khodadadi Fard (mahdi3466@yahoo.com)

    Permission is hereby granted, free of charge, to any person obtaining a copy of
    this software and associated documentation files (the "Software"), to deal in
    the Software without restriction, including without limitation the rights to
    use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
    the Software, and to permit persons to whom the Software is furnished to do so,
    subject to the following conditions:

    The above copyright notice and this permission notice shall be included in all
    copies or substantial portions of the Software.

    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
 */
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using GraphiXNA.Effect2D;
using Microsoft.Xna.Framework;

namespace GraphiXNA.Utils
{
    public class DrawTarget
    {
        RenderTarget2D target = null;
        Texture2D Texture = null;
        int DrawTargetIndex = 0;

        int m_Width = 0;
        int m_Height = 0;
        internal GraphicsDevice GraphicsDevice = null;

        SurfaceFormat m_Format = SurfaceFormat.Color;

        public DrawTarget(GraphicsDevice graphicsDevice, int width, int height)
        {
            GraphicsDevice = graphicsDevice;
            m_Width = width;
            m_Height = height;
            target = new RenderTarget2D(GraphicsDevice, m_Width, m_Height, 1, m_Format);
        }

        public DrawTarget(GraphicsDevice graphicsDevice, int width, int height, SurfaceFormat format)
        {
            GraphicsDevice = graphicsDevice;
            m_Width = width;
            m_Height = height;
            m_Format = format;
            target = new RenderTarget2D(GraphicsDevice, m_Width, m_Height, 1, m_Format);
        }

        public static implicit operator Texture2D (DrawTarget rtt)
        {
            return rtt.Texture;
        }

        public void Begin()
        {
            Begin(0);
        }

        public void Begin(int renderTargetIndex)
        {
            DrawTargetIndex = renderTargetIndex;
            GraphicsDevice.SetRenderTarget(DrawTargetIndex, target);
        }

        public void End()
        {
            GraphicsDevice.SetRenderTarget(DrawTargetIndex, null);
            Texture = target.GetTexture();
        }

        public void ApplyEffect(Effect2D.Effect2D effect)
        {
            Begin();
            effect.Draw(Texture);
            End();
        }

        public Texture2D GetTexture()
        {
            return Texture;
        }
    }
}
