SpriteSheet.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //-----------------------------------------------------------------------------
  2. // SpriteSheet.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using Microsoft.Xna.Framework;
  10. using Microsoft.Xna.Framework.Audio;
  11. using Microsoft.Xna.Framework.Graphics;
  12. using Microsoft.Xna.Framework.Input;
  13. using Microsoft.Xna.Framework.Content;
  14. using Microsoft.Xna.Framework.Media;
  15. namespace TiledSprites
  16. {
  17. /// <summary>
  18. /// Stores entries for individual sprites on a single texture.
  19. /// </summary>
  20. public class SpriteSheet
  21. {
  22. private Texture2D texture;
  23. private Dictionary<int, Rectangle> spriteDefinitions;
  24. /// <summary>
  25. /// Create a new Sprite Sheet
  26. /// </summary>
  27. public SpriteSheet(Texture2D sheetTexture)
  28. {
  29. texture = sheetTexture;
  30. spriteDefinitions = new Dictionary<int, Rectangle>();
  31. }
  32. /// <summary>
  33. /// Add a source sprite for fast retrieval
  34. /// </summary>
  35. public void AddSourceSprite(int key, Rectangle rect)
  36. {
  37. spriteDefinitions.Add(key, rect);
  38. }
  39. /// <summary>
  40. /// Get the source sprite texture
  41. /// </summary>
  42. public Texture2D Texture
  43. {
  44. get
  45. {
  46. return texture;
  47. }
  48. }
  49. /// <summary>
  50. /// Get the rectangle that defines the source sprite
  51. /// on the sheet.
  52. /// </summary>
  53. public Rectangle this[int i]
  54. {
  55. get
  56. {
  57. return spriteDefinitions[i];
  58. }
  59. }
  60. /// <summary>
  61. /// A faster lookup using refs to avoid stack copies.
  62. /// </summary>
  63. public void GetRectangle(ref int i, out Rectangle rect)
  64. {
  65. rect = spriteDefinitions[i];
  66. }
  67. }
  68. }