#region File Description
//-----------------------------------------------------------------------------
// SpriteSheetProcessor.cs
//
// Microsoft Game Technology Group
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion
#region Using Statements
using System.IO;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Content.Pipeline;
using Microsoft.Xna.Framework.Content.Pipeline.Graphics;
#endregion
namespace SpriteSheetPipeline
{
///
/// Custom content processor takes an array of individual sprite filenames (which
/// will typically be imported from an XML file), reads them all into memory,
/// arranges them onto a single larger texture, and returns the resulting sprite
/// sheet object.
///
[ContentProcessor]
public class SpriteSheetProcessor : ContentProcessor
{
///
/// Converts an array of sprite filenames into a sprite sheet object.
///
public override SpriteSheetContent Process(string[] input,
ContentProcessorContext context)
{
SpriteSheetContent spriteSheet = new SpriteSheetContent();
List sourceSprites = new List();
// Loop over each input sprite filename.
foreach (string inputFilename in input)
{
// Store the name of this sprite.
string spriteName = Path.GetFileNameWithoutExtension(inputFilename);
spriteSheet.SpriteNames.Add(spriteName, sourceSprites.Count);
// Load the sprite texture into memory.
ExternalReference textureReference =
new ExternalReference(inputFilename);
TextureContent texture =
context.BuildAndLoadAsset(textureReference, "TextureProcessor");
sourceSprites.Add(texture.Faces[0][0]);
}
// Pack all the sprites into a single large texture.
BitmapContent packedSprites = SpritePacker.PackSprites(sourceSprites,
spriteSheet.SpriteRectangles, context);
spriteSheet.Texture.Mipmaps.Add(packedSprites);
return spriteSheet;
}
}
}