GAM456-SpaceInvaders / SpaceInvaders / 1_Sprite / SpriteFont / SpriteFont.cs
SpriteFont.cs
Raw
using System;
using System.Diagnostics;


namespace SpaceInvaders
{
    public class SpriteFont : SpriteBase
    {
        //-------------------------------------------------------------------
        //  FIELDS
        //-------------------------------------------------------------------

        public Font.FontName fontName;
        public Glyph.GlyphName glyphName;

        private float x;
        private float y;
        private float sx;
        private float sy;
        private float spacing;

        private string pMessage;

        private Azul.Sprite poAzulSprite;
        private Azul.Rect poScreenRect;
        private Azul.Color poColor;


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

        public SpriteFont() : base()
        {
            //gets linked in set
            this.poAzulSprite = new Azul.Sprite();
            this.poScreenRect = new Azul.Rect();
            this.poColor = new Azul.Color(1.0f, 1.0f, 1.0f);

            Debug.Assert(this.poAzulSprite != null);
            Debug.Assert(this.poScreenRect != null);
            Debug.Assert(this.poColor != null);

            this.pMessage = null;
            this.glyphName = Glyph.GlyphName.Uninitialized;

            this.x = 0.0f;
            this.y = 0.0f;
            this.sx = 1.0f;
            this.sy = 1.0f;
            this.spacing = 2.0f;
        }


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

        public void SetSpriteFont(Font.FontName name, String pMessage, Glyph.GlyphName glyphName,
            float x, float y)
        {
            Debug.Assert(pMessage != null);
            this.pMessage = pMessage;

            this.x = x;
            this.y = y;

            this.fontName = name;
            this.glyphName = glyphName;

            Debug.Assert(this.poColor != null);
            this.poColor.Set(1.0f, 1.0f, 1.0f); //white
        }

        public void SetSpriteFont(Font.FontName fontName, Glyph.GlyphName glyphName, string msg, float x, float y, float sx = 1, float sy = 1, float spacing = 2, Azul.Color color = null)
        {
            Debug.Assert(msg != null);
            this.pMessage = msg;

            //set position & scale
            this.x = x;
            this.y = y;
            this.sx = sx;
            this.sy = sy;
            this.spacing = spacing;

            //asso font & glyph 
            this.fontName = fontName;
            this.glyphName = glyphName;

            if(color != null)
            {
                this.poColor.Set(color);
            }
        }

        public void UpdateMessage(String pMessage)
        {
            Debug.Assert(pMessage != null);
            this.pMessage = pMessage;
        }

        public override bool Compare(Node pNode)
        {
            Debug.Assert(pNode != null);

            SpriteFont pFontB = (SpriteFont)pNode;
            if(this.fontName == pFontB.fontName)
            {
                return true;
            }

            return false;
        }

        public override void Update()
        {
            //??????
            Debug.Assert(this.poAzulSprite != null);
        }

        public override void Render()
        {
            Debug.Assert(this.poAzulSprite != null);
            Debug.Assert(this.poColor != null);
            Debug.Assert(this.poScreenRect != null);
            Debug.Assert(this.pMessage != null);
            //Debug.Assert(this.pMessage.Length > 0);

            float tempx = this.x;
            float tempy = this.y;
            float endx = this.x;

            for (int i = 0; i < this.pMessage.Length; i++)
            {
                int key = Convert.ToByte(pMessage[i]);

                Glyph pGlyph = GlyphMan.FindGlyph(this.glyphName/*, key*/);
                Debug.Assert(pGlyph != null);

                Azul.Rect glyphRect = pGlyph.GetKey(key);

                tempx = endx + (glyphRect.width * sx / spacing);
                this.poScreenRect.Set(tempx, tempy, glyphRect.width * sx, glyphRect.height * sy);

                poAzulSprite.Swap(pGlyph.GetAzulTexture(), glyphRect, this.poScreenRect, this.poColor);
                poAzulSprite.Update();
                poAzulSprite.Render();

                //next character?
                endx = (glyphRect.width * sx / spacing) + tempx;
            }
        }

        public override object GetName()
        {
            return this.fontName;
        }

        public override void Reset()
        {
            this.Clear();
        }

        public override void PrintStats()
        {
            Debug.WriteLine("{0} {1}", this.fontName, this.GetHashCode());
            base.PrintStats();
        }


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

        private void Clear()
        {
            Debug.Assert(this.poAzulSprite != null);
            Debug.Assert(this.poColor != null);
            Debug.Assert(this.poScreenRect != null);

            this.poScreenRect.Set(0.0f, 0.0f, 0.0f, 0.0f);
            this.poColor.Set(1.0f, 1.0f, 1.0f);

            this.pMessage = null;
            this.glyphName = Glyph.GlyphName.Uninitialized;

            this.x = 0.0f;
            this.y = 0.0f;
        }


    } //end class

} //end namespace