Пример #1
0
        /// <summary>
        /// Generates a collection of frames from a sprite sheet with identical sprite sizes.
        /// </summary>
        /// <param name="size">The size of a single sprite.</param>
        /// <param name="spritesheet">The sprite sheet to load from.</param>
        /// <returns>A frame collection.</returns>
        public static FrameCollection FromSpriteSheet(Texture2D spritesheet, Point size)
        {
            if (spritesheet == null)
            {
                throw new ArgumentNullException("spritesheet");
            }

            if (size.X <= 0 || size.Y <= 0)
            {
                throw new ArgumentException("The size of a sprite must be positive.");
            }

            FrameCollection collection = new FrameCollection();

            for (int y = 0; y + size.Y <= spritesheet.Height; y += size.Y)
            {
                for (int x = 0; x + size.X <= spritesheet.Width; x += size.X)
                {
                    collection.Add(new Frame(spritesheet, new Rectangle(x, y, size.X, size.Y)));
                }
            }


            return(collection);
        }
Пример #2
0
        /// <summary>
        /// Creates a <see cref="SpriteSheet"/>.
        /// </summary>
        /// <param name="position">The position of the <see cref="SpriteSheet"/>.</param>
        /// <param name="batch">The <see cref="Microsoft.Xna.Framework.Graphics.SpriteBatch"/> to render to.</param>
        /// <param name="frames">The collection of frames that makes up this <see cref="SpriteSheet"/>.</param>
        public SpriteSheet(Vector2 position, SpriteBatch batch, IEnumerable <Frame> frames)
            : base(null, position, batch)
        {
            if (frames == null)
            {
                throw new ArgumentNullException("frames");
            }

            _frames = new FrameCollection();

            foreach (Frame frame in frames)
            {
                if (frame == null)
                {
                    throw new ArgumentException("The frames array cannot contain null elements.");
                }

                Frames.Add(frame);
            }
        }
Пример #3
0
        /// <summary>
        /// Generates a collection of frames from a sprite sheet.
        /// </summary>
        /// <param name="sprites">The bounding boxes of the sprites within the sprite sheet.</param>
        /// <param name="spritesheet">The sprite sheet to load from.</param>
        /// <returns>A frame collection.</returns>
        public static FrameCollection FromSpriteSheet(Texture2D spritesheet, params Rectangle[] sprites)
        {
            if (spritesheet == null)
            {
                throw new ArgumentNullException("spritesheet");
            }

            if (sprites == null)
            {
                throw new ArgumentNullException("sprites");
            }

            FrameCollection collection = new FrameCollection();

            for (int i = 0; i < sprites.Length; i++)
            {
                collection.Add(new Frame(spritesheet, sprites[i]));
            }

            return(collection);
        }