Font.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 System.Collections.Generic;
  12. using System.Text;
  13. using Microsoft.Xna.Framework;
  14. using System.Diagnostics;
  15. using Microsoft.Xna.Framework.Graphics;
  16. #endregion
  17. namespace Spacewar
  18. {
  19. /// <summary>
  20. /// Different fonts for use in SpacewarGame
  21. /// </summary>
  22. public enum FontStyle
  23. {
  24. /// <summary>
  25. /// Large gold font for in game scores
  26. /// </summary>
  27. Score,
  28. /// <summary>
  29. /// Large font used on weapon selection screen
  30. /// </summary>
  31. WeaponLarge,
  32. /// <summary>
  33. /// Small font used on weapon selection screen
  34. /// </summary>
  35. WeaponSmall,
  36. /// <summary>
  37. /// Font used for countdown on game screen
  38. /// </summary>
  39. GameCountDown,
  40. /// <summary>
  41. /// Font that stores the 'player1' and 'player2' strings "1" and "2"
  42. /// </summary>
  43. GamePlayerNames,
  44. /// <summary>
  45. /// The blue score buttons "0" and "1"
  46. /// </summary>
  47. ScoreButtons,
  48. /// <summary>
  49. /// The weapon icons "01234" for player1 "56789" for player2
  50. /// </summary>
  51. WeaponIcons,
  52. /// <summary>
  53. /// The 5 states of the healthbar "54321"
  54. /// </summary>
  55. HealthBar,
  56. /// <summary>
  57. /// The names of the 3 ships "012"
  58. /// </summary>
  59. ShipNames,
  60. }
  61. /// <summary>
  62. /// Utility wrapper for pulling digits from a small font sheet
  63. /// </summary>
  64. public static class Font
  65. {
  66. /// <summary>
  67. /// Assumes fonts are in single row in the order 01234567890$,
  68. /// </summary>
  69. private static SpriteBatch batch;
  70. private struct FontInfo
  71. {
  72. public string Filename;
  73. public string Characters;
  74. public int StartOffset;
  75. public int CharacterSpacing;
  76. public int CharacterWidth;
  77. public int CharacterHeight;
  78. public FontInfo(string fileName, string characters, int startOffset, int characterSpacing, int characterWidth, int characterHeight)
  79. {
  80. Filename = fileName;
  81. Characters = characters;
  82. CharacterHeight = characterHeight;
  83. StartOffset = startOffset;
  84. CharacterSpacing = characterSpacing;
  85. CharacterWidth = characterWidth;
  86. }
  87. }
  88. private static FontInfo[] _fontInfo = new FontInfo[]
  89. {
  90. new FontInfo(@"fonts\in-game_score", "0123456789", 0, 60, 58, 100),
  91. new FontInfo(@"fonts\weapon_large_font", "0123456789$,ptsx=", 0, 20, 18, 35),
  92. new FontInfo(@"fonts\weapon_small_font", "0123456789$,", 0, 15, 13, 30),
  93. new FontInfo(@"fonts\ingame_counter", "0123456789:", 0, 30, 24, 70),
  94. new FontInfo(@"fonts\in-game_player_text", "12", 0, 120, 120, 30),
  95. new FontInfo(@"fonts\hud_round_button", "01", 0, 28, 28, 22),
  96. new FontInfo(@"fonts\hud_weapon_icons", "0123456789", 0, 150, 150, 150),
  97. new FontInfo(@"fonts\health", "54321", 0, 50, 50, 70),
  98. new FontInfo(@"fonts\Ship_names", "012", 0, 200, 200, 30),
  99. };
  100. public static void Init(Game game)
  101. {
  102. if (game != null)
  103. {
  104. IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)game.Services.GetService(typeof(IGraphicsDeviceService));
  105. if (batch == null)
  106. batch = new SpriteBatch(graphicsService.GraphicsDevice);
  107. }
  108. }
  109. public static void Dispose()
  110. {
  111. if (batch != null)
  112. {
  113. batch.Dispose();
  114. batch = null;
  115. }
  116. }
  117. /// <summary>
  118. /// Starts a batch for efficient font drawing
  119. /// </summary>
  120. public static void Begin()
  121. {
  122. batch.Begin(SpriteSortMode.Texture, BlendState.AlphaBlend);
  123. }
  124. /// <summary>
  125. /// Ends a batch of font draw calls
  126. /// </summary>
  127. public static void End()
  128. {
  129. batch.End();
  130. }
  131. /// <summary>
  132. /// Draws some text from the given font
  133. /// </summary>
  134. /// <param name="fontStyle">Which font to use</param>
  135. /// <param name="x">X position in screen pixel space</param>
  136. /// <param name="y">Y position in screen pixel space</param>
  137. /// <param name="number">The number to draw</param>
  138. /// <param name="color">The color to draw it in</param>
  139. public static void Draw(FontStyle fontStyle, int x, int y, int number, Vector4 color)
  140. {
  141. Draw(fontStyle, x, y, number.ToString(), color);
  142. }
  143. /// <summary>
  144. /// Draws some text from the given font
  145. /// </summary>
  146. /// <param name="fontStyle">Which font to use</param>
  147. /// <param name="x">X position in screen pixel space</param>
  148. /// <param name="y">Y position in screen pixel space</param>
  149. /// <param name="number">The number to draw</param>
  150. public static void Draw(FontStyle fontStyle, int x, int y, int number)
  151. {
  152. //No color - use 'white' i.e. use whatever is in the file
  153. Draw(fontStyle, x, y, number.ToString(), new Vector4(1f, 1f, 1f, 1f));
  154. }
  155. /// <summary>
  156. /// Draws some text from the given font
  157. /// </summary>
  158. /// <param name="fontStyle">Which font to use</param>
  159. /// <param name="x">X position in screen pixel space</param>
  160. /// <param name="y">Y position in screen pixel space</param>
  161. /// <param name="digits">The characters to draw</param>
  162. public static void Draw(FontStyle fontStyle, int x, int y, string digits)
  163. {
  164. //No color - use 'white' i.e. use whatever is in the file
  165. Draw(fontStyle, x, y, digits, new Vector4(1f, 1f, 1f, 1f));
  166. }
  167. /// <summary>
  168. /// Draws some text from the given font
  169. /// </summary>
  170. /// <param name="fontStyle">Which font to use</param>
  171. /// <param name="x">X position in screen pixel space</param>
  172. /// <param name="y">Y position in screen pixel space</param>
  173. /// <param name="digits">The characters to draw</param>
  174. /// <param name="color">The color to draw it in</param>
  175. public static void Draw(FontStyle fontStyle, int x, int y, string digits, Vector4 color)
  176. {
  177. float xPosition = x;
  178. FontInfo fontInfo = _fontInfo[(int)fontStyle];
  179. for (int i = 0; i < digits.Length; i++)
  180. {
  181. //Don't draw anything if its a space character
  182. if (digits[i] != ' ')
  183. {
  184. //Look up the character position
  185. int character = fontInfo.Characters.IndexOf(digits[i]);
  186. //Draw the correct character at the correct position
  187. batch.Draw(
  188. SpacewarGame.ContentManager.Load<Texture2D>(SpacewarGame.Settings.MediaPath + fontInfo.Filename),
  189. new Vector2(xPosition, (float)y),
  190. new Rectangle(character * fontInfo.CharacterSpacing + fontInfo.StartOffset, 0, fontInfo.CharacterWidth, fontInfo.CharacterHeight),
  191. new Color(color));
  192. }
  193. //Move the position of the next character.
  194. //If the character is a comma or colon then use a 'fudge factor' to make the font look a little proportional
  195. xPosition += (digits[i] == ',' || digits[i] == ':') ? fontInfo.CharacterWidth / 2 : fontInfo.CharacterWidth;
  196. }
  197. }
  198. }
  199. }