Appearance
MonoGame exporter
How it works
Packrat writes a generated C# atlas class and the image files it loads through MonoGame's ContentManager. The class exposes keyed frames, ordered animation lists, trim metadata, and a SpriteBatch draw helper. It performs the atlas coordinate and trim calculations for the caller.
The class name follows the export base; the examples use atlas, which generates PackratAtlas.
API
The generated class is PackratAtlas when the export base is atlas. It contains these public data types and properties:
csharp
public sealed class Frame {
public string Key { get; }
public string Name { get; }
public int Index { get; }
public int Page { get; }
public Rectangle SourceRectangle { get; }
public Point OriginalSize { get; }
public Point Offset { get; }
public bool Rotated { get; }
}
public sealed class Animation {
public string Name { get; }
public IReadOnlyList<Frame> Frames { get; }
}
public int PageCount { get; }
public IReadOnlyList<Frame> Frames { get; }
public IReadOnlyList<Animation> Animations { get; }The generated class provides these exact methods:
csharp
public static PackratAtlas Load(ContentManager content);
public Frame Get(string name);
public Frame Get(string name, int index);
public Animation GetAnimation(string name);
public Texture2D GetTexture(Frame frame);
public void Draw(
SpriteBatch spriteBatch,
string name,
Vector2 position,
Color? color = null,
float rotation = 0f,
Vector2? scale = null,
SpriteEffects effects = SpriteEffects.None,
float layerDepth = 0f);
public void Draw(
SpriteBatch spriteBatch,
Frame frame,
Vector2 position,
Color? color = null,
float rotation = 0f,
Vector2? scale = null,
SpriteEffects effects = SpriteEffects.None,
float layerDepth = 0f);Get(name) accepts either a full frame key or a base animation name and returns the first matching frame. Missing names or indices throw KeyNotFoundException. Load loads every page through the supplied ContentManager; Draw applies trim metadata, rotation, scaling, and SpriteEffects.
Integrate
Compile the generated .cs file into the game and add every generated image to the MonoGame Content project using the asset names in the generated Content.Load<Texture2D> calls:
csharp
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
PackratAtlas atlas = PackratAtlas.Load(Content);
PackratAtlas.Animation run = atlas.GetAnimation("run_side");
PackratAtlas.Frame frame = run.Frames[0];
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
atlas.Draw(spriteBatch, frame, new Vector2(400, 300), Color.White,
0f, Vector2.One * 2f, SpriteEffects.None, 0f);
spriteBatch.End();Advance through run.Frames in your update loop to animate. Use SpriteEffects.FlipHorizontally or SpriteEffects.FlipVertically when drawing the opposite direction. Replace .png in the generated asset names with the image extension selected for the export.