Table of Contents

Class SpriteBatch

Namespace
Microsoft.Xna.Framework.Graphics
Assembly
MonoGame.Framework.dll

Helper class for drawing text strings and sprites in one or more optimized batches.

public class SpriteBatch : GraphicsResource, IDisposable
Inheritance
SpriteBatch
Implements
Inherited Members

Constructors

SpriteBatch(GraphicsDevice)

Constructs a SpriteBatch.

public SpriteBatch(GraphicsDevice graphicsDevice)

Parameters

graphicsDevice GraphicsDevice

The GraphicsDevice, which will be used for sprite rendering.

Exceptions

ArgumentNullException

Thrown when graphicsDevice is null.

SpriteBatch(GraphicsDevice, int)

Constructs a SpriteBatch.

public SpriteBatch(GraphicsDevice graphicsDevice, int capacity)

Parameters

graphicsDevice GraphicsDevice

The GraphicsDevice, which will be used for sprite rendering.

capacity int

The initial capacity of the internal array holding batch items (the value will be rounded to the next multiple of 64).

Exceptions

ArgumentNullException

Thrown when graphicsDevice is null.

Methods

Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?)

Begins a new sprite and text batch with the specified render state.

public void Begin(SpriteSortMode sortMode = SpriteSortMode.Deferred, BlendState blendState = null, SamplerState samplerState = null, DepthStencilState depthStencilState = null, RasterizerState rasterizerState = null, Effect effect = null, Matrix? transformMatrix = null)

Parameters

sortMode SpriteSortMode

The drawing order for sprite and text drawing. Deferred by default.

blendState BlendState

State of the blending. Uses AlphaBlend if null.

samplerState SamplerState

State of the sampler. Uses LinearClamp if null.

depthStencilState DepthStencilState

State of the depth-stencil buffer. Uses None if null.

rasterizerState RasterizerState

State of the rasterization. Uses CullCounterClockwise if null.

effect Effect

A custom Effect to override the default sprite effect. Uses default sprite effect if null.

transformMatrix Matrix?

An optional matrix used to transform the sprite geometry. Uses Identity if null.

Remarks

This method uses optional parameters.

Exceptions

InvalidOperationException

Thrown if Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?) is called next time without previous End().

Dispose(bool)

Immediately releases the unmanaged resources used by this object.

protected override void Dispose(bool disposing)

Parameters

disposing bool

true to release both managed and unmanaged resources; false to release only unmanaged resources.

Draw(Texture2D, Rectangle, Color)

Submit a sprite for drawing in the current batch.

public void Draw(Texture2D texture, Rectangle destinationRectangle, Color color)

Parameters

texture Texture2D

A texture.

destinationRectangle Rectangle

The drawing bounds on screen.

color Color

A color mask.

Draw(Texture2D, Rectangle, Rectangle?, Color)

Submit a sprite for drawing in the current batch.

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color)

Parameters

texture Texture2D

A texture.

destinationRectangle Rectangle

The drawing bounds on screen.

sourceRectangle Rectangle?

An optional region on the texture which will be rendered. If null - draws full texture.

color Color

A color mask.

Draw(Texture2D, Rectangle, Rectangle?, Color, float, Vector2, SpriteEffects, float)

Submit a sprite for drawing in the current batch.

public void Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth)

Parameters

texture Texture2D

A texture.

destinationRectangle Rectangle

The drawing bounds on screen.

sourceRectangle Rectangle?

An optional region on the texture which will be rendered. If null - draws full texture.

color Color

A color mask.

rotation float

A rotation of this sprite.

origin Vector2

Center of the rotation. 0,0 by default.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this sprite.

Draw(Texture2D, Vector2, Color)

Submit a sprite for drawing in the current batch.

public void Draw(Texture2D texture, Vector2 position, Color color)

Parameters

texture Texture2D

A texture.

position Vector2

The drawing location on screen.

color Color

A color mask.

Draw(Texture2D, Vector2, Rectangle?, Color)

Submit a sprite for drawing in the current batch.

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color)

Parameters

texture Texture2D

A texture.

position Vector2

The drawing location on screen.

sourceRectangle Rectangle?

An optional region on the texture which will be rendered. If null - draws full texture.

color Color

A color mask.

Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, Vector2, SpriteEffects, float)

Submit a sprite for drawing in the current batch.

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)

Parameters

texture Texture2D

A texture.

position Vector2

The drawing location on screen.

sourceRectangle Rectangle?

An optional region on the texture which will be rendered. If null - draws full texture.

color Color

A color mask.

rotation float

A rotation of this sprite.

origin Vector2

Center of the rotation. 0,0 by default.

scale Vector2

A scaling of this sprite.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this sprite.

Draw(Texture2D, Vector2, Rectangle?, Color, float, Vector2, float, SpriteEffects, float)

Submit a sprite for drawing in the current batch.

public void Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)

Parameters

texture Texture2D

A texture.

position Vector2

The drawing location on screen.

sourceRectangle Rectangle?

An optional region on the texture which will be rendered. If null - draws full texture.

color Color

A color mask.

rotation float

A rotation of this sprite.

origin Vector2

Center of the rotation. 0,0 by default.

scale float

A scaling of this sprite.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this sprite.

DrawString(SpriteFont, string, Vector2, Color)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color)

Parameters

spriteFont SpriteFont

A font.

text string

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

DrawString(SpriteFont, string, Vector2, Color, float, Vector2, Vector2, SpriteEffects, float)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)

Parameters

spriteFont SpriteFont

A font.

text string

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale Vector2

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this string.

DrawString(SpriteFont, string, Vector2, Color, float, Vector2, Vector2, SpriteEffects, float, bool)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, bool rtl)

Parameters

spriteFont SpriteFont

A font.

text string

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale Vector2

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this string.

rtl bool

Text is Right to Left.

DrawString(SpriteFont, string, Vector2, Color, float, Vector2, float, SpriteEffects, float)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)

Parameters

spriteFont SpriteFont

A font.

text string

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale float

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this string.

DrawString(SpriteFont, StringBuilder, Vector2, Color)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color)

Parameters

spriteFont SpriteFont

A font.

text StringBuilder

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

DrawString(SpriteFont, StringBuilder, Vector2, Color, float, Vector2, Vector2, SpriteEffects, float)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)

Parameters

spriteFont SpriteFont

A font.

text StringBuilder

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale Vector2

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this string.

DrawString(SpriteFont, StringBuilder, Vector2, Color, float, Vector2, Vector2, SpriteEffects, float, bool)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth, bool rtl)

Parameters

spriteFont SpriteFont

A font.

text StringBuilder

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale Vector2

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this string.

rtl bool

Text is Right to Left.

DrawString(SpriteFont, StringBuilder, Vector2, Color, float, Vector2, float, SpriteEffects, float)

Submit a text string of sprites for drawing in the current batch.

public void DrawString(SpriteFont spriteFont, StringBuilder text, Vector2 position, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth)

Parameters

spriteFont SpriteFont

A font.

text StringBuilder

The text which will be drawn.

position Vector2

The drawing location on screen.

color Color

A color mask.

rotation float

A rotation of this string.

origin Vector2

Center of the rotation. 0,0 by default.

scale float

A scaling of this string.

effects SpriteEffects

Modificators for drawing. Can be combined.

layerDepth float

A depth of the layer of this string.

End()

Flushes all batched text and sprites to the screen.

public void End()

Remarks