Xen Content Caches

Tired of calling the Content.Load<>() function all the time and managing all of the references? So were we. Which is why we created the XenCache!

Using the Cache

Xen currently supports caching for the following content types:
  • SpriteFont
  • Song
  • SoundEffect
  • Texture2D
  • Texture3D (Note: Not supported in Silverlight)
In order to use the content cache, you must create an integer enum set and load the cache. It uses a ContentIdentifier attribute to determine the enum value's mapping to the content.
Here is an example of a Texture2D cache for your sprites:
    // Define the texture enums
    public enum Textures : int
    {
        [ContentIdentifier( "textures\\explosion0" )]
        Explosion,
        [ContentIdentifier( "textures\\marker_blue" )]
        Marker_Blue,
        [ContentIdentifier( "textures\\marker_green" )]
        Marker_Green,
        [ContentIdentifier( "textures\\marker_red" )]
        Marker_Red
    }
    public class GameMain : Game
    {
        Texture2DCache _textureCache = null;
        StaticSprite _sprite = null;
        protected override void LoadContent()
        {
            // Load the texture cache
            _textureCache = new Texture2DCache( typeof( Textures ) ); // You can also load the cache into a specific ContentManager by passing it into the constructor.
            // Use the texture cache to create a sprite
            _sprite = StaticSprite.Acquire( _textureCache[ (int)Textures.Explosion ] );
        }

Creating your own Content Cache

To create your own cache, all that is required is to implement a XenCache class.
Take a look at how the SpriteFontCache is implemented:
    public class SpriteFontCache : XenCache<SpriteFont>
    {
        // SpriteFont Cache
        public SpriteFontCache( Type contentElementEnumeration ) : this( Globals.Content, contentElementEnumeration ) { }
        public SpriteFontCache( ContentManager content, Type contentElementEnumeration ) : base( content, contentElementEnumeration ) { }
    }
    public abstract class SpriteFontCache<T> : XenCache<SpriteFont, T>
    {
        public SpriteFontCache() : base( typeof( T ) ) { }
    }

Last edited Feb 7, 2011 at 5:50 AM by twohalf, version 2

Comments

No comments yet.