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
graphicsDeviceGraphicsDeviceThe GraphicsDevice, which will be used for sprite rendering.
Exceptions
- ArgumentNullException
Thrown when
graphicsDeviceis null.
SpriteBatch(GraphicsDevice, int)
Constructs a SpriteBatch.
public SpriteBatch(GraphicsDevice graphicsDevice, int capacity)
Parameters
graphicsDeviceGraphicsDeviceThe GraphicsDevice, which will be used for sprite rendering.
capacityintThe initial capacity of the internal array holding batch items (the value will be rounded to the next multiple of 64).
Exceptions
- ArgumentNullException
Thrown when
graphicsDeviceis 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
sortModeSpriteSortModeThe drawing order for sprite and text drawing. Deferred by default.
blendStateBlendStateState of the blending. Uses AlphaBlend if null.
samplerStateSamplerStateState of the sampler. Uses LinearClamp if null.
depthStencilStateDepthStencilStateState of the depth-stencil buffer. Uses None if null.
rasterizerStateRasterizerStateState of the rasterization. Uses CullCounterClockwise if null.
effectEffectA custom Effect to override the default sprite effect. Uses default sprite effect if null.
transformMatrixMatrix?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
disposingbooltrueto release both managed and unmanaged resources;falseto 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
textureTexture2DA texture.
destinationRectangleRectangleThe drawing bounds on screen.
colorColorA 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
textureTexture2DA texture.
destinationRectangleRectangleThe drawing bounds on screen.
sourceRectangleRectangle?An optional region on the texture which will be rendered. If null - draws full texture.
colorColorA 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
textureTexture2DA texture.
destinationRectangleRectangleThe drawing bounds on screen.
sourceRectangleRectangle?An optional region on the texture which will be rendered. If null - draws full texture.
colorColorA color mask.
rotationfloatA rotation of this sprite.
originVector2Center of the rotation. 0,0 by default.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA 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
textureTexture2DA texture.
positionVector2The drawing location on screen.
colorColorA 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
textureTexture2DA texture.
positionVector2The drawing location on screen.
sourceRectangleRectangle?An optional region on the texture which will be rendered. If null - draws full texture.
colorColorA 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
textureTexture2DA texture.
positionVector2The drawing location on screen.
sourceRectangleRectangle?An optional region on the texture which will be rendered. If null - draws full texture.
colorColorA color mask.
rotationfloatA rotation of this sprite.
originVector2Center of the rotation. 0,0 by default.
scaleVector2A scaling of this sprite.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA 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
textureTexture2DA texture.
positionVector2The drawing location on screen.
sourceRectangleRectangle?An optional region on the texture which will be rendered. If null - draws full texture.
colorColorA color mask.
rotationfloatA rotation of this sprite.
originVector2Center of the rotation. 0,0 by default.
scalefloatA scaling of this sprite.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA 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
spriteFontSpriteFontA font.
textstringThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA 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
spriteFontSpriteFontA font.
textstringThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scaleVector2A scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA 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
spriteFontSpriteFontA font.
textstringThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scaleVector2A scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA depth of the layer of this string.
rtlboolText 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
spriteFontSpriteFontA font.
textstringThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scalefloatA scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA 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
spriteFontSpriteFontA font.
textStringBuilderThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA 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
spriteFontSpriteFontA font.
textStringBuilderThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scaleVector2A scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA 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
spriteFontSpriteFontA font.
textStringBuilderThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scaleVector2A scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA depth of the layer of this string.
rtlboolText 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
spriteFontSpriteFontA font.
textStringBuilderThe text which will be drawn.
positionVector2The drawing location on screen.
colorColorA color mask.
rotationfloatA rotation of this string.
originVector2Center of the rotation. 0,0 by default.
scalefloatA scaling of this string.
effectsSpriteEffectsModificators for drawing. Can be combined.
layerDepthfloatA 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.