#region Using directives

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

#endregion

namespace PunkMan
{
    /// <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));
            Collection<Texture> texList = new Collection<Texture>();
            while (!reader.EndOfStream)
            {
                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] = 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;
        }
    }
}
