#region File Description //----------------------------------------------------------------------------- // Font.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- #endregion #region Using Statements using System; using System.Collections.Generic; using System.Text; using Microsoft.Xna.Framework; using System.Diagnostics; using Microsoft.Xna.Framework.Graphics; #endregion namespace Spacewar { /// /// Different fonts for use in SpacewarGame /// public enum FontStyle { /// /// Large gold font for in game scores /// Score, /// /// Large font used on weapon selection screen /// WeaponLarge, /// /// Small font used on weapon selection screen /// WeaponSmall, /// /// Font used for countdown on game screen /// GameCountDown, /// /// Font that stores the 'player1' and 'player2' strings "1" and "2" /// GamePlayerNames, /// /// The blue score buttons "0" and "1" /// ScoreButtons, /// /// The weapon icons "01234" for player1 "56789" for player2 /// WeaponIcons, /// /// The 5 states of the healthbar "54321" /// HealthBar, /// /// The names of the 3 ships "012" /// ShipNames, } /// /// Utility wrapper for pulling digits from a small font sheet /// public static class Font { /// /// Assumes fonts are in single row in the order 01234567890$, /// private static SpriteBatch batch; private struct FontInfo { public string Filename; public string Characters; public int StartOffset; public int CharacterSpacing; public int CharacterWidth; public int CharacterHeight; public FontInfo(string fileName, string characters, int startOffset, int characterSpacing, int characterWidth, int characterHeight) { Filename = fileName; Characters = characters; CharacterHeight = characterHeight; StartOffset = startOffset; CharacterSpacing = characterSpacing; CharacterWidth = characterWidth; } } private static FontInfo[] _fontInfo = new FontInfo[] { new FontInfo(@"fonts\in-game_score", "0123456789", 0, 60, 58, 100), new FontInfo(@"fonts\weapon_large_font", "0123456789$,ptsx=", 0, 20, 18, 35), new FontInfo(@"fonts\weapon_small_font", "0123456789$,", 0, 15, 13, 30), new FontInfo(@"fonts\ingame_counter", "0123456789:", 0, 30, 24, 70), new FontInfo(@"fonts\in-game_player_text", "12", 0, 120, 120, 30), new FontInfo(@"fonts\hud_round_button", "01", 0, 28, 28, 22), new FontInfo(@"fonts\hud_weapon_icons", "0123456789", 0, 150, 150, 150), new FontInfo(@"fonts\health", "54321", 0, 50, 50, 70), new FontInfo(@"fonts\Ship_names", "012", 0, 200, 200, 30), }; public static void Init(Game game) { if (game != null) { IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService)); if (batch == null) batch = new SpriteBatch(graphicsService.GraphicsDevice); } } public static void Dispose() { if (batch != null) { batch.Dispose(); batch = null; } } /// /// Starts a batch for efficient font drawing /// public static void Begin() { batch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend); } /// /// Ends a batch of font draw calls /// public static void End() { batch.End(); } /// /// Draws some text from the given font /// /// Which font to use /// X position in screen pixel space /// Y position in screen pixel space /// The number to draw /// The color to draw it in public static void Draw(FontStyle fontStyle, int x, int y, int number, Vector4 color) { Draw(fontStyle, x, y, number.ToString(), color); } /// /// Draws some text from the given font /// /// Which font to use /// X position in screen pixel space /// Y position in screen pixel space /// The number to draw public static void Draw(FontStyle fontStyle, int x, int y, int number) { //No color - use 'white' i.e. use whatever is in the file Draw(fontStyle, x, y, number.ToString(), new Vector4(1f, 1f, 1f, 1f)); } /// /// Draws some text from the given font /// /// Which font to use /// X position in screen pixel space /// Y position in screen pixel space /// The characters to draw public static void Draw(FontStyle fontStyle, int x, int y, string digits) { //No color - use 'white' i.e. use whatever is in the file Draw(fontStyle, x, y, digits, new Vector4(1f, 1f, 1f, 1f)); } /// /// Draws some text from the given font /// /// Which font to use /// X position in screen pixel space /// Y position in screen pixel space /// The characters to draw /// The color to draw it in public static void Draw(FontStyle fontStyle, int x, int y, string digits, Vector4 color) { float xPosition = x; FontInfo fontInfo = _fontInfo[(int)fontStyle]; for (int i = 0; i < digits.Length; i++) { //Don't draw anything if its a space character if (digits[i] != ' ') { //Look up the character position int character = fontInfo.Characters.IndexOf(digits[i]); //Draw the correct character at the correct position batch.Draw( SpacewarGame.ContentManager.Load(SpacewarGame.Settings.MediaPath + fontInfo.Filename), new Vector2(xPosition, (float)y), new Rectangle(character * fontInfo.CharacterSpacing + fontInfo.StartOffset, 0, fontInfo.CharacterWidth, fontInfo.CharacterHeight), new Color(color)); } //Move the position of the next character. //If the character is a comma or colon then use a 'fudge factor' to make the font look a little proportional xPosition += (digits[i] == ',' || digits[i] == ':') ? fontInfo.CharacterWidth / 2 : fontInfo.CharacterWidth; } } } }