Skip to content

GraphicsAPI reference

Class SpriteAtlasBuilder

Namespace: MicroEngine.Graphics
Assembly: MicroEngine.Graphics2D.dll
Guide: Graphics

Packs many procedurally authored PixelImages into one texture and exposes them by name. Keeps draw calls batched and art code independent of layout.

C#
public sealed class SpriteAtlasBuilder

Inheritance

object ← SpriteAtlasBuilder

Examples

C#
var hero = new PixelImage(16, 16);
hero.FillRect(4, 2, 8, 12, Color.White);
var atlas = new SpriteAtlasBuilder()
.Add("hero", hero, origin: new Vector2(8, 16))
.Build(Graphics);
// In Draw:
renderer.Draw(atlas["hero"], new Vector2(160, 120));

Remarks

Add images with Add, AddFrames or AddGrid, then call Build once from Game.Load (it needs the graphics device). Names are case-sensitive.

Constructors

SpriteAtlasBuilder()

C#
public SpriteAtlasBuilder()

Properties

Count

Number of sprites added so far (each frame counts as one).

C#
public int Count { get; }

Property Value

int

Methods

Add(string, PixelImage, Vector2?)

Adds an image under name. The image is copied into the atlas when Build runs, so keep it unchanged until then.

C#
public SpriteAtlasBuilder Add(string name, PixelImage image, Vector2? origin = null)

Parameters

name string

Unique, non-empty sprite name.

image PixelImage

The pixels.

origin Vector2?

The sprite’s anchor in its own pixels (see Origin); null for the top-left corner.

Returns

SpriteAtlasBuilder

This builder, for chaining.

Exceptions

ArgumentException

name is empty or already added.

AddFrames(string, IEnumerable<PixelImage>, Vector2?)

Adds each frame as “name0”, “name1”, … sharing the same origin.

C#
public SpriteAtlasBuilder AddFrames(string name, IEnumerable<PixelImage> frames, Vector2? origin = null)

Parameters

name string

frames IEnumerable<PixelImage>

origin Vector2?

Returns

SpriteAtlasBuilder

AddGrid(string, PixelImage, int, int, int?, Vector2?)

Slices a sprite sheet laid out on a regular grid (row-major) into frames “name0”, “name1”, … count limits the number of cells (default: every cell); fully transparent cells are skipped only when they are trailing cells of the last row.

C#
public SpriteAtlasBuilder AddGrid(string name, PixelImage sheet, int cellWidth, int cellHeight, int? count = null, Vector2? origin = null)

Parameters

name string

sheet PixelImage

cellWidth int

cellHeight int

count int?

origin Vector2?

Returns

SpriteAtlasBuilder

Build(IGraphicsDevice, TextureFilter?)

Shelf-packs every image into the smallest power-of-two width that fits, with a 1-pixel gap between images. Sampled linearly, each image also gets a 1-pixel border repeating its edge pixels, so scaled sprites and tiles never show their neighbours’ colours or seams.

C#
public SpriteAtlas Build(IGraphicsDevice device, TextureFilter? filter = null)

Parameters

device IGraphicsDevice

Creates the texture.

filter TextureFilter?

The texture’s filter; null for the device’s DefaultFilter.

Returns

SpriteAtlas