SpriteSheet.cs 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SpriteSheet.cs
  4. //
  5. // Microsoft Game Technology Group
  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 Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. using Microsoft.Xna.Framework.Content;
  15. #endregion
  16. namespace SpriteSheetRuntime
  17. {
  18. /// <summary>
  19. /// A sprite sheet contains many individual sprite images, packed into different
  20. /// areas of a single larger texture, along with information describing where in
  21. /// that texture each sprite is located. Sprite sheets can make your game drawing
  22. /// more efficient, because they reduce the number of times the graphics hardware
  23. /// needs to switch from one texture to another.
  24. /// </summary>
  25. public class SpriteSheet
  26. {
  27. // Single texture contains many separate sprite images.
  28. [ContentSerializer]
  29. Texture2D texture = null;
  30. // Remember where in the texture each sprite has been placed.
  31. [ContentSerializer]
  32. List<Rectangle> spriteRectangles = null;
  33. // Store the original sprite filenames, so we can look up sprites by name.
  34. [ContentSerializer]
  35. Dictionary<string, int> spriteNames = null;
  36. /// <summary>
  37. /// Gets the single large texture used by this sprite sheet.
  38. /// </summary>
  39. public Texture2D Texture
  40. {
  41. get { return texture; }
  42. }
  43. /// <summary>
  44. /// Looks up the location of the specified sprite within the big texture.
  45. /// </summary>
  46. public Rectangle SourceRectangle(string spriteName)
  47. {
  48. int spriteIndex = GetIndex(spriteName);
  49. return spriteRectangles[spriteIndex];
  50. }
  51. /// <summary>
  52. /// Looks up the location of the specified sprite within the big texture.
  53. /// </summary>
  54. public Rectangle SourceRectangle(int spriteIndex)
  55. {
  56. if ((spriteIndex < 0) || (spriteIndex >= spriteRectangles.Count))
  57. throw new ArgumentOutOfRangeException("spriteIndex");
  58. return spriteRectangles[spriteIndex];
  59. }
  60. /// <summary>
  61. /// Looks up the numeric index of the specified sprite. This is useful when
  62. /// implementing animation by cycling through a series of related sprites.
  63. /// </summary>
  64. public int GetIndex(string spriteName)
  65. {
  66. int index;
  67. if (!spriteNames.TryGetValue(spriteName, out index))
  68. {
  69. string error = "SpriteSheet does not contain a sprite named '{0}'.";
  70. throw new KeyNotFoundException(string.Format(error, spriteName));
  71. }
  72. return index;
  73. }
  74. }
  75. }