Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.33 KB | None | 0 0
  1. // System
  2. using System;
  3. using System.Globalization;
  4.  
  5. // XNA
  6. using Microsoft.Xna.Framework;
  7. using Microsoft.Xna.Framework.Graphics;
  8.  
  9. namespace ExtensionLibrary
  10. {
  11.     /// <summary>
  12.     /// Extension methods appended to the SpriteBatch class.
  13.     /// </summary>
  14.     public static class SpriteBatchExtensions
  15.     {
  16.         private static string[] digits = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" };
  17.  
  18.         private static string[] intCharBuffer = new string[10];
  19.         private static float[] intXPositionBuffer = new float[10];
  20.         private static readonly string intMinValue = Int32.MinValue.ToString(CultureInfo.InvariantCulture);
  21.  
  22.  
  23.         private static string[] longCharBuffer = new string[20];
  24.         private static float[] longXPositionBuffer = new float[20];
  25.         private static readonly string longMinValue = Int64.MinValue.ToString(CultureInfo.InvariantCulture);
  26.  
  27.         /// <summary>  
  28.         /// Extension method for SpriteBatch that draws an integer without allocating  
  29.         /// any memory. This function avoids garbage collections that are normally caused  
  30.         /// by calling Int32.ToString or String.Format.  
  31.         /// </summary>  
  32.         /// <param name="spriteBatch">The SpriteBatch instance whose DrawString method will be invoked.</param>  
  33.         /// <param name="spriteFont">The SpriteFont to draw the integer value with.</param>  
  34.         /// <param name="value">The integer value to draw.</param>  
  35.         /// <param name="position">The screen position specifying where to draw the value.</param>  
  36.         /// <param name="color">The color of the text drawn.</param>  
  37.         /// <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>  
  38.         public static Vector2 DrawInt32(
  39.             this SpriteBatch spriteBatch,
  40.             SpriteFont spriteFont,
  41.             int value,
  42.             Vector2 position,
  43.             Color color,
  44.             float layerDepth)
  45.         {
  46.             if (spriteBatch == null)
  47.             {
  48.                 throw new ArgumentNullException("spriteBatch");
  49.             }
  50.  
  51.             if (spriteFont == null)
  52.             {
  53.                 throw new ArgumentNullException("spriteFont");
  54.             }
  55.  
  56.             Vector2 nextPosition = position;
  57.  
  58.             if (value == Int32.MinValue)
  59.             {
  60.                 nextPosition.X = nextPosition.X + spriteFont.MeasureString(intMinValue).X;
  61.                 spriteBatch.DrawString(spriteFont, intMinValue, position, color);
  62.                 position = nextPosition;
  63.             }
  64.             else
  65.             {
  66.                 if (value < 0)
  67.                 {
  68.                     nextPosition.X = nextPosition.X + spriteFont.MeasureString("-").X;
  69.                     spriteBatch.DrawString(spriteFont, "-", position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  70.                     value = -value;
  71.                     position = nextPosition;
  72.                 }
  73.  
  74.                 int index = 0;
  75.  
  76.                 do
  77.                 {
  78.                     int modulus = value % 10;
  79.                     value = value / 10;
  80.  
  81.                     intCharBuffer[index] = digits[modulus];
  82.                     intXPositionBuffer[index] = spriteFont.MeasureString(digits[modulus]).X;
  83.                     index += 1;
  84.                 } while (value > 0);
  85.  
  86.                 for (int i = index - 1; i >= 0; --i)
  87.                 {
  88.                     nextPosition.X = nextPosition.X + intXPositionBuffer[i];
  89.                     spriteBatch.DrawString(spriteFont, intCharBuffer[i], position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  90.                     position = nextPosition;
  91.                 }
  92.             }
  93.             return position;
  94.         }
  95.  
  96.         /// <summary>  
  97.         /// Extension method for SpriteBatch that draws a long integer without allocating  
  98.         /// any memory. This function avoids garbage collections that are normally caused  
  99.         /// by calling Int64.ToString or String.Format.  
  100.         /// </summary>  
  101.         /// <param name="spriteBatch">The SpriteBatch instance whose DrawString method will be invoked.</param>  
  102.         /// <param name="spriteFont">The SpriteFont to draw the integer value with.</param>  
  103.         /// <param name="value">The long value to draw.</param>  
  104.         /// <param name="position">The screen position specifying where to draw the value.</param>  
  105.         /// <param name="color">The color of the text drawn.</param>  
  106.         /// <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>  
  107.         public static Vector2 DrawInt64(
  108.             this SpriteBatch spriteBatch,
  109.             SpriteFont spriteFont,
  110.             long value,
  111.             Vector2 position,
  112.             Color color,
  113.             float layerDepth)
  114.        
  115.         {
  116.             if (spriteBatch == null)
  117.             {
  118.                 throw new ArgumentNullException("spriteBatch");
  119.             }
  120.  
  121.             if (spriteFont == null)
  122.             {
  123.                 throw new ArgumentNullException("spriteFont");
  124.             }
  125.  
  126.             Vector2 nextPosition = position;
  127.  
  128.             if (value == Int64.MinValue)
  129.             {
  130.                 nextPosition.X = nextPosition.X + spriteFont.MeasureString(longMinValue).X;
  131.                 spriteBatch.DrawString(spriteFont, longMinValue, position, color);
  132.                 position = nextPosition;
  133.             }
  134.             else
  135.             {
  136.                 if (value < 0)
  137.                 {
  138.                     nextPosition.X = nextPosition.X + spriteFont.MeasureString("-").X;
  139.                     spriteBatch.DrawString(spriteFont, "-", position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  140.                     value = -value;
  141.                     position = nextPosition;
  142.                 }
  143.  
  144.                 int index = 0;
  145.  
  146.                 do
  147.                 {
  148.                     long modulus = value % 10;
  149.                     value = value / 10;
  150.  
  151.                     longCharBuffer[index] = digits[modulus];
  152.                     longXPositionBuffer[index] = spriteFont.MeasureString(digits[modulus]).X;
  153.                     index += 1;
  154.                 } while (value > 0);
  155.  
  156.                 for (int i = index - 1; i >= 0; --i)
  157.                 {
  158.                     nextPosition.X = nextPosition.X + longXPositionBuffer[i];
  159.                     spriteBatch.DrawString(spriteFont, longCharBuffer[i], position, color, 0.0f, Vector2.Zero, 1.0f, SpriteEffects.None, layerDepth);
  160.                     position = nextPosition;
  161.                 }
  162.             }
  163.             return position;
  164.         }
  165.  
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement