#region Using directives

using System;
using System.IO;
using System.Collections;
using System.Drawing;
using Microsoft.DirectX;
using Microsoft.DirectX.Direct3D;
using System.Windows.Forms;

#endregion

namespace PunkMan.Shared
{
    /// <summary>
    /// Map texture manager
    /// </summary>
    public class TextureManager
    {
        /// <summary>
        /// Direct3D device
        /// </summary>
        private Device dev;
        /// <summary>
        /// Texture list
        /// </summary>
        private Texture[] textureList;
        /// <summary>
        /// Texture list
        /// </summary>
        /// <value></value>
        public Texture[] TextureList
        {
            get { return textureList; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="dev">Direct3D device</param>
        /// <param name="path">texture repository path</param>
        public TextureManager(Device dev, string path)
        {
            this.dev = dev;

            StreamReader reader = new StreamReader(new FileStream(path + @"\texture.list", FileMode.Open));
			
            ArrayList texList = new ArrayList();
			
            while (reader.Peek() > 0)
            {
                string fullName = path + "\\" + reader.ReadLine();
                if (!File.Exists(fullName)) continue;

                

                texList.Add(TextureLoader.FromFile(dev, fullName));
            }
            reader.Close();

            textureList = new Texture[texList.Count];
            for (int i = 0; i < texList.Count; i++) 
                textureList[i] = (Texture)texList[i];


        }

        

        /// <summary>
        /// Activate texture 
        /// </summary>
        /// <param name="texNo">index of texture</param>
        public void Activate(int texNo)
        {
            if (texNo < 0 || texNo >= textureList.Length) return;
            dev.SetTexture(0, textureList[texNo]);
            dev.TextureState[0].ColorOperation = TextureOperation.Modulate;
            dev.TextureState[0].ColorArgument1 = TextureArgument.TextureColor;
            dev.TextureState[0].ColorArgument2 = TextureArgument.Diffuse;
            dev.TextureState[0].AlphaOperation = TextureOperation.Disable;
        }
    }
}
