GAM456-SpaceInvaders / SpaceInvaders / 1_Image / ImageMan.cs
ImageMan.cs
Raw
using System;
using System.Diagnostics;


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

        private Image poCompare;
        private static ImageMan poInstance = null;


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

        private ImageMan(int initialReserve = 3, int growReserve = 2)
            : base(new DLinkMan(), new DLinkMan(), initialReserve, growReserve)
        {
            //LTN: owner, create one node to use for all comparisons
            this.poCompare = new Image();
            Debug.Assert(this.poCompare != null);
        }


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

        public static void Create(int initialReserve = 3, int growReserve = 2)
        {
            //LTN: owner, instance is static and lives for as long game runs
            if(ImageMan.poInstance == null)
            {
                ImageMan.poInstance = new ImageMan(initialReserve, growReserve);
                Debug.Assert(ImageMan.poInstance != null);
            }

            Image pHotPink = ImageMan.AddImage(Image.ImageName.HotPink, Texture.TextureName.HotPink, 0, 0, 128, 128);
            Debug.Assert(pHotPink != null);
        }

        public static void Destroy()
        {
            ImageMan pManager = ImageMan.GetInstance();
            Debug.Assert(pManager != null);

            //iterate and clear list?

            pManager.poCompare = null;
            ImageMan.poInstance = null;
            //TODO: stat collection, invalidate singleton? etc?
        }

        public static Image AddImage(Image.ImageName name, Texture.TextureName textureName, float x, float y, float sx, float sy)
        {
            //get manager
            ImageMan pManager = ImageMan.GetInstance();
            Debug.Assert(pManager != null);

            //pull node from reserve pool
            Image pImgNode = (Image)pManager.AddToFront();
            Debug.Assert(pImgNode != null);

            //set data
            Texture pSource = TextureMan.FindTexture(textureName);
            pImgNode.SetImage(name, pSource, x, y, sx, sy);

            return pImgNode;
        }

        public static void RemoveImage(Image pImg)
        {
            //check pointer
            Debug.Assert(pImg != null);

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

            //delegate remove to base
            pManager.Remove(pImg);
        }

        public static Image FindImage(Image.ImageName name)
        {
            //get manager
            ImageMan pManager = ImageMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.poCompare.name = name;
            Image pImg = (Image)pManager.Find(pManager.poCompare);

            return pImg;
        }

        public static void PrintImage()
        {
            ImageMan pManager = ImageMan.GetInstance();
            Debug.Assert(pManager != null);

            pManager.PrintStats();
        }

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

        protected override Node derivedCreate()
        {
            //LTN: owned by Manager (base) 
            Node pNode = new Image();
            Debug.Assert(pNode != null);

            return pNode;
        }


        //-------------------------------------------------------------------
        //  PRIVATE METHODS
        //-------------------------------------------------------------------

        private static ImageMan GetInstance()
        {
            Debug.Assert(ImageMan.poInstance != null);
            return ImageMan.poInstance;
        }


    } //end class

} //end namespace