All pastes #818521 Raw Edit

Mine

public text v1 · immutable
·published 2007-12-16 10:26 UTC
rendered paste body
// Drawtext.cs

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion

namespace Shooter
{
    public class DrawText
    {
        private Dictionary<string, SpriteFont> m_FontList
            = new Dictionary<string, SpriteFont>();

        private List<SpriteFont> m_SpriteList 
            = new List<SpriteFont>();

        private SpriteBatch m_Batch;
        private ContentManager m_Content;

        public DrawText(SpriteBatch batch, ContentManager content)
        {
            m_Batch = batch;
            m_Content = content;
        }

        public void AddFont(string name)
        {
            m_SpriteList.Add(m_Content.Load<SpriteFont>("Fonts/" + name));
            m_FontList.Add(name, m_SpriteList[m_FontList.Count]);
        }

        private bool ContainFont(string str)
        {
            if (m_FontList[str] != null)
                return true;
            return false;
        }

        public void Draw(Vector2 pos, string str, Color color, string fontName)
        {
            try
            {
                if (ContainFont(fontName))
                {
                    m_Batch.DrawString(m_FontList[fontName], 
                        str, pos, color);
                }
            }
            catch (KeyNotFoundException)
            {
                throw new Exception(fontName + " is not a valid font.");
            }
        }
    }
}


// Inside game1.cs

        // Declare a new DrawText
        private DrawText m_DrawText;


        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
                m_SpriteBatch = new SpriteBatch(m_Graphics.GraphicsDevice);
                // Setup your new drawtext thing
                m_DrawText = new DrawText(m_SpriteBatch, m_Content);
                // Add some fonts (that you added into your game)
                m_DrawText.AddFont("Arial");
                m_DrawText.AddFont("Fgm");
            }
        }

        // As long as you have access to that DrawText you declared, you can run it in the
        // Draw( .. ) function
        // eg: dt.Draw(new Vector2(0, 0), "Position: " + m_Player.Position, Color.White, "Arial");