SpriteSheet.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #region File Description
  2. //-----------------------------------------------------------------------------
  3. // SpriteSheet.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. #if IPHONE
  13. using Microsoft.Xna.Framework;
  14. using Microsoft.Xna.Framework.Audio;
  15. using Microsoft.Xna.Framework.GamerServices;
  16. using Microsoft.Xna.Framework.Graphics;
  17. using Microsoft.Xna.Framework.Input;
  18. using Microsoft.Xna.Framework.Storage;
  19. using Microsoft.Xna.Framework.Content;
  20. using Microsoft.Xna.Framework.Media;
  21. #else
  22. using Microsoft.Xna.Framework;
  23. using Microsoft.Xna.Framework.Audio;
  24. using Microsoft.Xna.Framework.GamerServices;
  25. using Microsoft.Xna.Framework.Graphics;
  26. using Microsoft.Xna.Framework.Input;
  27. using Microsoft.Xna.Framework.Storage;
  28. using Microsoft.Xna.Framework.Content;
  29. using Microsoft.Xna.Framework.Media;
  30. #endif
  31. #endregion
  32. namespace TiledSprites
  33. {
  34. /// <summary>
  35. /// Stores entries for individual sprites on a single texture.
  36. /// </summary>
  37. public class SpriteSheet
  38. {
  39. #region Fields
  40. private Texture2D texture;
  41. private Dictionary<int, Rectangle> spriteDefinitions;
  42. #endregion
  43. #region Constructors
  44. /// <summary>
  45. /// Create a new Sprite Sheet
  46. /// </summary>
  47. public SpriteSheet(Texture2D sheetTexture)
  48. {
  49. texture = sheetTexture;
  50. spriteDefinitions = new Dictionary<int, Rectangle>();
  51. }
  52. #endregion
  53. #region Methods
  54. /// <summary>
  55. /// Add a source sprite for fast retrieval
  56. /// </summary>
  57. public void AddSourceSprite(int key, Rectangle rect)
  58. {
  59. spriteDefinitions.Add(key, rect);
  60. }
  61. /// <summary>
  62. /// Get the source sprite texture
  63. /// </summary>
  64. public Texture2D Texture
  65. {
  66. get
  67. {
  68. return texture;
  69. }
  70. }
  71. /// <summary>
  72. /// Get the rectangle that defines the source sprite
  73. /// on the sheet.
  74. /// </summary>
  75. public Rectangle this[int i]
  76. {
  77. get
  78. {
  79. return spriteDefinitions[i];
  80. }
  81. }
  82. /// <summary>
  83. /// A faster lookup using refs to avoid stack copies.
  84. /// </summary>
  85. public void GetRectangle(ref int i, out Rectangle rect)
  86. {
  87. rect = spriteDefinitions[i];
  88. }
  89. #endregion
  90. }
  91. }