Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Sep 7th, 2011  |  syntax: C#  |  size: 7.84 KB  |  views: 87  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #region Using Statements
  2. // System
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Globalization;
  6. // XNA
  7. using Microsoft.Xna.Framework;
  8. using Microsoft.Xna.Framework.Graphics;
  9. // 3rd Party
  10. #endregion
  11.  
  12.  
  13. namespace IgnLib.Extensions
  14. {
  15.     /// <summary>
  16.     /// Extension methods appended to the SpriteBatch class.
  17.     /// </summary>
  18.     public static class SpriteBatchExtensions
  19.     {
  20.         /// <summary>
  21.         /// Draws a int or long without generating garbage ( = w/o using .ToString() )
  22.         /// </summary>
  23.         #region DrawGarbageFreeInt
  24.         #region DrawIntXX Fields
  25.         private static string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  26.  
  27.         private static string[] intCharBuffer = new string[10];
  28.         private static float[] intXPositionBuffer = new float[10];
  29.         private static readonly string intMinValue = Int32.MinValue.ToString(CultureInfo.InvariantCulture);
  30.  
  31.  
  32.         private static string[] longCharBuffer = new string[20];
  33.         private static float[] longXPositionBuffer = new float[20];
  34.         private static readonly string longMinValue = Int64.MinValue.ToString(CultureInfo.InvariantCulture);
  35.         #endregion
  36.  
  37.         #region DrawInt32
  38.         /// <summary>  
  39.         /// Extension method for SpriteBatch that draws an integer without allocating  
  40.         /// any memory. This function avoids garbage collections that are normally caused  
  41.         /// by calling Int32.ToString or String.Format.  
  42.         /// </summary>  
  43.         /// <param name="spriteBatch">The SpriteBatch instance whose DrawString method will be invoked.</param>  
  44.         /// <param name="spriteFont">The SpriteFont to draw the integer value with.</param>  
  45.         /// <param name="value">The integer value to draw.</param>  
  46.         /// <param name="position">The screen position specifying where to draw the value.</param>  
  47.         /// <param name="color">The color of the text drawn.</param>  
  48.         /// <returns>The next position on the line to draw text. This value uses position.Y and position.X plus the equivalent of calling spriteFont.MeasureString on value.ToString(CultureInfo.InvariantCulture).</returns>  
  49.         public static Vector2 DrawInt32(
  50.             this SpriteBatch spriteBatch,
  51.             SpriteFont spriteFont,
  52.             int value,
  53.             Vector2 position,
  54.             Color color,
  55.             float layerDepth)
  56.         {
  57.             if (spriteBatch == null)
  58.             {
  59.                 throw new ArgumentNullException("spriteBatch");
  60.             }
  61.  
  62.             if (spriteFont == null)
  63.             {
  64.                 throw new ArgumentNullException("spriteFont");
  65.             }
  66.  
  67.             Vector2 nextPosition = position;
  68.  
  69.             if (value == Int32.MinValue)
  70.             {
  71.                 nextPosition.X = nextPosition.X + spriteFont.MeasureString(intMinValue).X;
  72.                 spriteBatch.DrawString(spriteFont, intMinValue, position, color);
  73.                 position = nextPosition;
  74.             }
  75.             else
  76.             {
  77.                 if (value < 0)
  78.                 {
  79.                     nextPosition.X = nextPosition.X + spriteFont.MeasureString("-").X;
  80.                     spriteBatch.DrawString(spriteFont, "-", position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  81.                     value = -value;
  82.                     position = nextPosition;
  83.                 }
  84.  
  85.                 int index = 0;
  86.  
  87.                 do
  88.                 {
  89.                     int modulus = value % 10;
  90.                     value = value / 10;
  91.  
  92.                     intCharBuffer[index] = digits[modulus];
  93.                     intXPositionBuffer[index] = spriteFont.MeasureString(digits[modulus]).X;
  94.                     index += 1;
  95.                 } while (value > 0);
  96.  
  97.                 for (int i = index - 1; i >= 0; --i)
  98.                 {
  99.                     nextPosition.X = nextPosition.X + intXPositionBuffer[i];
  100.                     spriteBatch.DrawString(spriteFont, intCharBuffer[i], position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  101.                     position = nextPosition;
  102.                 }
  103.             }
  104.             return position;
  105.         }
  106.         #endregion
  107.  
  108.         #region DrawInt64
  109.         /// <summary>  
  110.         /// Extension method for SpriteBatch that draws a long integer without allocating  
  111.         /// any memory. This function avoids garbage collections that are normally caused  
  112.         /// by calling Int64.ToString or String.Format.  
  113.         /// </summary>  
  114.         /// <param name="spriteBatch">The SpriteBatch instance whose DrawString method will be invoked.</param>  
  115.         /// <param name="spriteFont">The SpriteFont to draw the integer value with.</param>  
  116.         /// <param name="value">The long value to draw.</param>  
  117.         /// <param name="position">The screen position specifying where to draw the value.</param>  
  118.         /// <param name="color">The color of the text drawn.</param>  
  119.         /// <returns>The next position on the line to draw text. This value uses position.Y and position.X plus the equivalent of calling spriteFont.MeasureString on value.ToString(CultureInfo.InvariantCulture).</returns>  
  120.         public static Vector2 DrawInt64(
  121.             this SpriteBatch spriteBatch,
  122.             SpriteFont spriteFont,
  123.             long value,
  124.             Vector2 position,
  125.             Color color,
  126.             float layerDepth)
  127.         {
  128.             if (spriteBatch == null)
  129.             {
  130.                 throw new ArgumentNullException("spriteBatch");
  131.             }
  132.  
  133.             if (spriteFont == null)
  134.             {
  135.                 throw new ArgumentNullException("spriteFont");
  136.             }
  137.  
  138.             Vector2 nextPosition = position;
  139.  
  140.             if (value == Int64.MinValue)
  141.             {
  142.                 nextPosition.X = nextPosition.X + spriteFont.MeasureString(longMinValue).X;
  143.                 spriteBatch.DrawString(spriteFont, longMinValue, position, color);
  144.                 position = nextPosition;
  145.             }
  146.             else
  147.             {
  148.                 if (value < 0)
  149.                 {
  150.                     nextPosition.X = nextPosition.X + spriteFont.MeasureString("-").X;
  151.                     spriteBatch.DrawString(spriteFont, "-", position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  152.                     value = -value;
  153.                     position = nextPosition;
  154.                 }
  155.  
  156.                 int index = 0;
  157.  
  158.                 do
  159.                 {
  160.                     long modulus = value % 10;
  161.                     value = value / 10;
  162.  
  163.                     longCharBuffer[index] = digits[modulus];
  164.                     longXPositionBuffer[index] = spriteFont.MeasureString(digits[modulus]).X;
  165.                     index += 1;
  166.                 } while (value > 0);
  167.  
  168.                 for (int i = index - 1; i >= 0; --i)
  169.                 {
  170.                     nextPosition.X = nextPosition.X + longXPositionBuffer[i];
  171.                     spriteBatch.DrawString(spriteFont, longCharBuffer[i], position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  172.                     position = nextPosition;
  173.                 }
  174.             }
  175.             return position;
  176.         }
  177.         #endregion
  178.         #endregion
  179.  
  180.         /// <summary>
  181.         /// Draws shadowed string
  182.         /// </summary>
  183.         #region Shadowed String
  184.         public static void DrawShadowedString(this SpriteBatch sb, SpriteFont font, string text, Vector2 position)
  185.         {
  186.             sb.DrawString(font, text, position, Color.Black, 0f, font.MeasureString(text) / 2f, 1.05f, SpriteEffects.None, 1f); // BLACK
  187.             sb.DrawString(font, text, position, Color.White, 0f, font.MeasureString(text) / 2f, 1f, SpriteEffects.None, 1f); // WHITE
  188.         }
  189.         #endregion
  190.     }
  191. }
clone this paste RAW Paste Data