#region License /* MIT License Copyright 2006 Rob Loach (http://www.robloach.net) http://www.openxna.net All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /* slight modification to use content loader instead of fromfile * made LineHeight public * changed code for 1.0 compatibility * * -Priyesh Dixit (pndixit@uncc.edu) */ #endregion License using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Storage; using System.IO; namespace RobLoach { public enum Alignment { Left, Center, Right } class BitmapFont { public BitmapFont(GraphicsDevice device, ContentManager content) { this.content = content; this.m_Sprites = new SpriteBatch(device); } private string m_ImageFilename = string.Empty; private ContentManager content; public string ImageFilename { get { return m_ImageFilename; } set { m_ImageFilename = value; } } private string m_DataFilename = string.Empty; public string DataFilename { get { return m_DataFilename; } set { m_DataFilename = value; } } private SpriteBatch m_Sprites; private byte[] m_Widths; public Texture2D m_Texture; //private IGraphicsDeviceService m_GraphicsDeviceService; private void LoadResources() { m_Texture = content.Load(m_ImageFilename); using (FileStream stream = new FileStream(m_DataFilename, FileMode.Open)) { m_Widths = new byte[stream.Length]; stream.Read(m_Widths, 0, (int)stream.Length); stream.Close(); } } private void ReleaseContent() { if (m_Texture != null) { m_Texture.Dispose(); m_Texture = null; } if (m_Sprites != null) { m_Sprites.Dispose(); m_Sprites = null; } m_Widths = null; } public void Start() { /* TODO: Add your start up code here m_GraphicsDeviceService = this.Game.GameServices.GetService(); m_GraphicsDeviceService.DeviceReset += new EventHandler(GraphicsDeviceService_DeviceReset); m_GraphicsDeviceService.DeviceResetting += new EventHandler(GraphicsDeviceService_DeviceResetting); m_GraphicsDeviceService.DeviceCreated += new EventHandler(GraphicsDeviceService_DeviceCreated); m_GraphicsDeviceService.DeviceDisposing += new EventHandler(GraphicsDeviceService_DeviceDisposing); */ LoadResources(); } void GraphicsDeviceService_DeviceDisposing(object sender, EventArgs e) { ReleaseContent(); } void GraphicsDeviceService_DeviceResetting(object sender, EventArgs e) { ReleaseContent(); } void GraphicsDeviceService_DeviceCreated(object sender, EventArgs e) { LoadResources(); } void GraphicsDeviceService_DeviceReset(object sender, EventArgs e) { LoadResources(); } private SpriteBlendMode m_BlendMode = SpriteBlendMode.AlphaBlend; public SpriteBlendMode BlendMode { get { return m_BlendMode; } set { m_BlendMode = value; } } public int TextWidth(string text) { float width = 0f; CharEnumerator chars = text.GetEnumerator(); while (chars.MoveNext()) width += m_Widths[(int)chars.Current * 2]; return (int)width; } public int LineHeight { get { return (int)m_Texture.Height / 16; } } public void Draw(float x, float y, string text, Alignment alignment) { Draw(x, y, text, alignment, Color.White); } public void Draw(float x, float y, string text, Alignment alignment, Color color) { if (text == null) return; if (text.Length == 0) return; switch (alignment) { case Alignment.Center: x -= TextWidth(text) / 2f; break; case Alignment.Right: x -=(int)TextWidth(text); break; } // Fix to integer precision to fix the blending mode. x = (int)x; y = (int)y; // Draw the text if (m_Sprites != null) { m_Sprites.Begin(m_BlendMode); { float xLocation = x; CharEnumerator chars = text.GetEnumerator(); while (chars.MoveNext()) { byte ascii = (byte)chars.Current; int charX = (ascii % 16) * m_Texture.Width / 16; int charY = (ascii / 16) * m_Texture.Height / 16; m_Sprites.Draw(m_Texture, new Vector2(xLocation, y), new Rectangle(charX, charY, (int)m_Widths[ascii * 2], m_Texture.Width / 16), color); xLocation += (float)m_Widths[ascii * 2]; } } m_Sprites.End(); } } public void Draw(float x, float y, string text) { Draw(x, y, text, Alignment.Left); } public void Draw(Vector2 location, string text) { Draw(location.X, location.Y, text, Alignment.Left); } public void Draw(Vector2 location, string text, Alignment alignment) { Draw(location.X, location.Y, text, alignment); } public void Draw(string text) { Draw(0f, 0f, text, Alignment.Left); } } }