//----------------------------------------------------------------------------- // SpriteSheet.cs // // Microsoft XNA Community Game Platform // Copyright (C) Microsoft Corporation. All rights reserved. //----------------------------------------------------------------------------- using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Media; namespace TiledSprites { /// /// Stores entries for individual sprites on a single texture. /// public class SpriteSheet { private Texture2D texture; private Dictionary spriteDefinitions; /// /// Create a new Sprite Sheet /// public SpriteSheet(Texture2D sheetTexture) { texture = sheetTexture; spriteDefinitions = new Dictionary(); } /// /// Add a source sprite for fast retrieval /// public void AddSourceSprite(int key, Rectangle rect) { spriteDefinitions.Add(key, rect); } /// /// Get the source sprite texture /// public Texture2D Texture { get { return texture; } } /// /// Get the rectangle that defines the source sprite /// on the sheet. /// public Rectangle this[int i] { get { return spriteDefinitions[i]; } } /// /// A faster lookup using refs to avoid stack copies. /// public void GetRectangle(ref int i, out Rectangle rect) { rect = spriteDefinitions[i]; } } }