GAM456-SpaceInvaders / SpaceInvaders / 1_Texture / TextureMan.cs
TextureMan.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class TextureMan : Manager
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        private static TextureMan poInstance = null;
        private Texture poCompare;


        //-------------------------------------------------------------------
        //  CONSTRUCTION
        //-------------------------------------------------------------------

        private TextureMan(int initialReserve = 3, int growReserve = 2) 
            : base(new DLinkMan(), new DLinkMan(), initialReserve, growReserve)
        {
            //LTN: own by texture manager
            this.poCompare = new Texture();
            Debug.Assert(this.poCompare != null);
        }


        //------------------------------------------------------------------
        //  PUBLIC METHODS
        //------------------------------------------------------------------

        public static void Create(int initialReserve = 3, int growReserve = 2)
        {
            //LTN: own by texture manager
            if (TextureMan.poInstance == null)
            {
                TextureMan.poInstance = new TextureMan(initialReserve, growReserve);
                Debug.Assert(TextureMan.poInstance != null);
            }

            //default
            Texture pDefault = TextureMan.AddTexture(Texture.TextureName.HotPink, "HotPink.tga");
            Debug.Assert(pDefault != null);
        }

        public static void Destroy()
        {
            //get instance & check its not null
            TextureMan pManager = TextureMan.GetInstance();
            Debug.Assert(pManager != null);

            //clear list?

            pManager.poCompare = null;
            TextureMan.poInstance = null;

            //TODO: decide if you want stats, etc
        }

        public static Texture AddTexture(Texture.TextureName name, string ptga)
        {
            //get instance & make sure its valid
            TextureMan pManager = TextureMan.GetInstance();
            Debug.Assert(pManager != null);

            //add
            Texture pNode = (Texture)pManager.AddToFront();
            Debug.Assert(pNode != null);

            //set data
            pNode.SetTexture(name, ptga);
            return pNode;
        }

        public static void RemoveTexture(Texture pNode)
        {
            //check texture exists
            Debug.Assert(pNode != null);

            TextureMan pManager = TextureMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.Remove(pNode);
        }

        public static Texture FindTexture(Texture.TextureName name)
        {
            TextureMan pManager = TextureMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.poCompare.name = name;
            Texture pData = (Texture)pManager.Find(pManager.poCompare);

            return pData;
        }

        public static void PrintTexture()
        {
            TextureMan pManager = TextureMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }


        //-------------------------------------------------------------------
        //  PROTECTED METHODS
        //-------------------------------------------------------------------

        protected override Node derivedCreate()
        {
            //LTN: owned by texture man
            Node pNode = new Texture();
            Debug.Assert(pNode != null);

            return pNode;
        }


        //-------------------------------------------------------------------
        //  PROTECTED METHODS
        //-------------------------------------------------------------------

        private static TextureMan GetInstance()
        {
            //Debug.Assert(pInstance != null);
            if (poInstance == null)
            {
                Create();
            }

            return poInstance;
        }


    } //end class

} // end namespace