Font.cs 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // Font.cs
  4. //
  5. // Microsoft XNA Community Game Platform
  6. // Copyright (C) Microsoft Corporation. All rights reserved.
  7. //-----------------------------------------------------------------------------
  8. #endregion
  9. #region Using Statements
  10. using System;
  11. using Microsoft.Xna.Framework;
  12. using Microsoft.Xna.Framework.Graphics;
  13. #endregion
  14. namespace Marblets
  15. {
  16. /// <summary>
  17. /// Different fonts for use in Marblets
  18. /// </summary>
  19. public enum FontStyle
  20. {
  21. /// <summary>
  22. /// Small font
  23. /// </summary>
  24. Small,
  25. /// <summary>
  26. /// Large font
  27. /// </summary>
  28. Large,
  29. }
  30. /// <summary>
  31. /// Utility wrapper for pulling digits from a small font sheet
  32. /// </summary>
  33. public static class Font
  34. {
  35. private struct FontInfo
  36. {
  37. public string Filename;
  38. public string Characters;
  39. public int StartOffset;
  40. public int CharacterSpacing;
  41. public int CharacterWidth;
  42. public int CharacterHeight;
  43. public FontInfo(string fileName, string characters, int startOffset,
  44. int characterSpacing, int characterWidth,
  45. int characterHeight)
  46. {
  47. Filename = fileName;
  48. Characters = characters;
  49. StartOffset = startOffset;
  50. CharacterSpacing = characterSpacing;
  51. CharacterWidth = characterWidth;
  52. CharacterHeight = characterHeight;
  53. }
  54. }
  55. private static FontInfo[] fontInfo = new FontInfo[]
  56. {
  57. new FontInfo("Textures/numbers_small", "1234567890,", 10, 32, 18, 32),
  58. new FontInfo("Textures/numbers_large", "1234567890,", 20, 64, 30, 64),
  59. };
  60. private static Texture2D[] fontTextures = new Texture2D[fontInfo.Length];
  61. /// <summary>
  62. /// Load graphics content.
  63. /// </summary>
  64. public static void LoadContent()
  65. {
  66. //Load all the font textures
  67. int fontCount = 0;
  68. foreach(FontInfo font in fontInfo)
  69. {
  70. fontTextures[fontCount++] =
  71. MarbletsGame.Content.Load<Texture2D>(font.Filename);
  72. }
  73. }
  74. /// <summary>
  75. /// Draws some text from the given font
  76. /// </summary>
  77. /// <param name="spriteBatch">The sprite batch to use</param>
  78. /// <param name="fontStyle">Which font to use</param>
  79. /// <param name="x">X position in screen pixel space</param>
  80. /// <param name="y">Y position in screen pixel space</param>
  81. /// <param name="number">The number to draw</param>
  82. public static void Draw(RelativeSpriteBatch spriteBatch, FontStyle fontStyle,
  83. int x, int y, int number)
  84. {
  85. //No color - use 'white' i.e. use whatever is in the file
  86. Draw(spriteBatch, fontStyle, x, y, number.ToString(), Color.White);
  87. }
  88. /// <summary>
  89. /// Draws some text from the given font
  90. /// </summary>
  91. /// <param name="spriteBatch">The sprite batch to use</param>
  92. /// <param name="fontStyle">Which font to use</param>
  93. /// <param name="x">X position in screen pixel space</param>
  94. /// <param name="y">Y position in screen pixel space</param>
  95. /// <param name="digits">The characters to draw</param>
  96. public static void Draw(RelativeSpriteBatch spriteBatch, FontStyle fontStyle,
  97. int x, int y, string digits)
  98. {
  99. //No color - use 'white' i.e. use whatever is in the file
  100. Draw(spriteBatch, fontStyle, x, y, digits, Color.White);
  101. }
  102. /// <summary>
  103. /// Draws some text from the given font
  104. /// </summary>
  105. /// <param name="spriteBatch">The sprite batch to use</param>
  106. /// <param name="fontStyle">Which font to use</param>
  107. /// <param name="position">A vector x,y position</param>
  108. /// <param name="number">A number to draw</param>
  109. /// <param name="color">The color of the text</param>
  110. public static void Draw(RelativeSpriteBatch spriteBatch, FontStyle fontStyle,
  111. Vector2 position, int number, Color color)
  112. {
  113. Draw(spriteBatch, fontStyle, (int)position.X, (int)position.Y,
  114. number.ToString(), color);
  115. }
  116. /// <summary>
  117. /// Draws some text from the given font
  118. /// </summary>
  119. /// <param name="spriteBatch">The sprite batch to use</param>
  120. /// <param name="fontStyle">Which font to use</param>
  121. /// <param name="x">X position in screen pixel space</param>
  122. /// <param name="y">Y position in screen pixel space</param>
  123. /// <param name="digits">The characters to draw</param>
  124. /// <param name="color">The color to draw it in</param>
  125. public static void Draw(RelativeSpriteBatch spriteBatch, FontStyle fontStyle,
  126. int x, int y, string digits, Color color)
  127. {
  128. float xPosition = x;
  129. FontInfo thisFont = fontInfo[(int)fontStyle];
  130. for(int i = 0; i < digits.Length; i++)
  131. {
  132. //Don't draw anything if its a space character
  133. if(digits[i] != ' ')
  134. {
  135. //Look up the character position
  136. int character = thisFont.Characters.IndexOf(digits[i]);
  137. //Draw the correct character at the correct position
  138. spriteBatch.Draw(fontTextures[(int)fontStyle],
  139. new Vector2(xPosition, (float)y),
  140. new Rectangle(character * thisFont.CharacterSpacing +
  141. thisFont.StartOffset, 0, thisFont.CharacterWidth,
  142. thisFont.CharacterHeight), color);
  143. }
  144. //Move the position of the next character.
  145. //If the character is a comma or colon then use a 'fudge factor' to make
  146. //the font look a little proportional
  147. xPosition += ((digits[i] == ',' || digits[i] == ':') ?
  148. thisFont.CharacterWidth / 2 : thisFont.CharacterWidth);
  149. }
  150. }
  151. }
  152. }