Class SpriteBatch
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
GraphicsDeviceThe 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
GraphicsDeviceThe GraphicsDevice, which will be used for sprite rendering.
capacity
intThe 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
SpriteSortModeThe drawing order for sprite and text drawing. Deferred by default.
blendState
BlendStateState of the blending. Uses AlphaBlend if null.
samplerState
SamplerStateState of the sampler. Uses LinearClamp if null.
depthStencilState
DepthStencilStateState of the depth-stencil buffer. Uses None if null.
rasterizerState
RasterizerStateState of the rasterization. Uses CullCounterClockwise if null.
effect
EffectA 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
booltrue
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
Texture2DA texture.
destinationRectangle
RectangleThe drawing bounds on screen.
color
ColorA 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
Texture2DA texture.
destinationRectangle
RectangleThe drawing bounds on screen.
sourceRectangle
Rectangle?An optional region on the texture which will be rendered. If null - draws full texture.
color
ColorA 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
Texture2DA texture.
destinationRectangle
RectangleThe drawing bounds on screen.
sourceRectangle
Rectangle?An optional region on the texture which will be rendered. If null - draws full texture.
color
ColorA color mask.
rotation
floatA rotation of this sprite.
origin
Vector2Center of the rotation. 0,0 by default.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA 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
Texture2DA texture.
position
Vector2The drawing location on screen.
color
ColorA 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
Texture2DA texture.
position
Vector2The drawing location on screen.
sourceRectangle
Rectangle?An optional region on the texture which will be rendered. If null - draws full texture.
color
ColorA 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
Texture2DA texture.
position
Vector2The drawing location on screen.
sourceRectangle
Rectangle?An optional region on the texture which will be rendered. If null - draws full texture.
color
ColorA color mask.
rotation
floatA rotation of this sprite.
origin
Vector2Center of the rotation. 0,0 by default.
scale
Vector2A scaling of this sprite.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA 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
Texture2DA texture.
position
Vector2The drawing location on screen.
sourceRectangle
Rectangle?An optional region on the texture which will be rendered. If null - draws full texture.
color
ColorA color mask.
rotation
floatA rotation of this sprite.
origin
Vector2Center of the rotation. 0,0 by default.
scale
floatA scaling of this sprite.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA 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
SpriteFontA font.
text
stringThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA 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
SpriteFontA font.
text
stringThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA color mask.
rotation
floatA rotation of this string.
origin
Vector2Center of the rotation. 0,0 by default.
scale
Vector2A scaling of this string.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA 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
SpriteFontA font.
text
stringThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA color mask.
rotation
floatA rotation of this string.
origin
Vector2Center of the rotation. 0,0 by default.
scale
Vector2A scaling of this string.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA depth of the layer of this string.
rtl
boolText 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
SpriteFontA font.
text
stringThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA color mask.
rotation
floatA rotation of this string.
origin
Vector2Center of the rotation. 0,0 by default.
scale
floatA scaling of this string.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA 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
SpriteFontA font.
text
StringBuilderThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA 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
SpriteFontA font.
text
StringBuilderThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA color mask.
rotation
floatA rotation of this string.
origin
Vector2Center of the rotation. 0,0 by default.
scale
Vector2A scaling of this string.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA 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
SpriteFontA font.
text
StringBuilderThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA color mask.
rotation
floatA rotation of this string.
origin
Vector2Center of the rotation. 0,0 by default.
scale
Vector2A scaling of this string.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA depth of the layer of this string.
rtl
boolText 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
SpriteFontA font.
text
StringBuilderThe text which will be drawn.
position
Vector2The drawing location on screen.
color
ColorA color mask.
rotation
floatA rotation of this string.
origin
Vector2Center of the rotation. 0,0 by default.
scale
floatA scaling of this string.
effects
SpriteEffectsModificators for drawing. Can be combined.
layerDepth
floatA depth of the layer of this string.
End()
Flushes all batched text and sprites to the screen.
public void End()
Remarks
This command should be called after Begin(SpriteSortMode, BlendState, SamplerState, DepthStencilState, RasterizerState, Effect, Matrix?) and drawing commands.