Click or drag to resize
Ab4d.SharpEngine logo

SpriteBatch Class

SpriteBatch class defines a collection of sprites (textures) or texts that can be very quickly rendered to the 3D scene. SpriteBatch objects can be created by calling Scene.CreateBackgroundSpriteBatch(String), Scene.CreateOverlaySpriteBatch(String), SceneView.CreateBackgroundSpriteBatch(String) or SceneView.CreateOverlaySpriteBatch(String). When the SpriteBatch is created from SceneView, then it can use absolute coordinates when calling Draw methods.
Inheritance Hierarchy
SystemObject
  Ab4d.SharpEngine.UtilitiesSpriteBatch

Namespace: Ab4d.SharpEngine.Utilities
Assembly: Ab4d.SharpEngine (in Ab4d.SharpEngine.dll) Version: 2.0.8956+4c7684e186ca1be74e7a284fbe739d9a1b843d3c
Syntax
C#
public class SpriteBatch : IDisposable

The SpriteBatch type exposes the following members.

Properties
 NameDescription
Public propertyAreAbsoluteCoordinatesSupported Gets a Boolean that specifies if this SpriteBatch supports absolute coordinates (true when created from SceneView; false when created from Scene).
Public propertyIsUsingAbsoluteCoordinates Gets or sets a Boolean that specifies if absolute coordinates are used to define positions and sizes (max values are defined by SceneView's size) in the Draw calls that follow the change. When false then relative coordinates are used (in this case the values are from 0 to 1). Absolute coordinates can be used only when this SpriteBatch is created from SceneView and not for Scene (see AreAbsoluteCoordinatesSupported property). This value can be also set when calling Begin(Boolean, Int32) method. Default value is false.
Public propertyIsUsingDpiScale When set to true (false by default) than the absolute coordinates and sprite size are multiplied by dpi scale. When false, then absolute coordinates are specified in pixels. The value of this property is used for all sprites and texts in this SpriteBatch.
Top
Methods
 NameDescription
Public methodBegin Begin method must be called before any Draw or Set calls. The method sets common properties of all sprites that will be defined by Draw call. After all draw calls are done, the End method must be called.
Public methodCollectRenderingItems CollectRenderingItems adds the RenderingItem objects to the OverlayRenderingLayer or any custom RenderingLayer that is set in the Begin(Boolean, Int32) method.
Public methodDispose Disposes the resources that are created by this SpriteBatch (does not dispose the used GpuImages).
Public methodDrawBitmapText(String, Vector2, Single, Color4, Single, Single, Single, Single, Boolean, BitmapTextCreator) DrawBitmapText method draws the specified text to the specified position that defines the top-left corner of the text. See remarks for more info.
Public methodDrawBitmapText(String, Vector2, Single, Color4, Color4, Single, Single, Single, Single, Single, Single, Single, Single, Boolean, BitmapTextCreator) DrawBitmapText method draws the specified text to the specified position that defines the top-left corner of the text. This method also rendered the background with the specified color and applies the specified margin to the text. See remarks for more info.
Public methodDrawRectangle DrawRectangle method draws a rectangle at the specified position and size. The rectangle is filled by the specified color. When rotationAngleDegrees is specified (0 by default), then the rectangle is rotated around the center position.
Public methodDrawSprite(Vector2, Vector2, Single) DrawSprite method draws the current texture to the specified position, size and and rotation (0 by default). When size is set to (0, 0), then the actual size of the texture is used. See remarks for more info.
Public methodDrawSprite(Vector2, Single, Single, Single) DrawSprite method draws the current texture to the specified position and using the specified scale (1 by default) and rotation (0 by default). See remarks for more info.
Public methodDrawSprite(Vector2, Vector2, Color4, Single, Boolean) DrawSprite method draws the current texture to the specified position, size and and rotation (0 by default). When size is set to (0, 0), then the actual size of the texture is used. The colors in the texture are multiplied by the specified colorMask. See remarks for more info.
Public methodDrawSprite(Vector2, Color4, Single, Single, Single, Boolean) DrawSprite method draws the current texture to the specified position and using the specified scale (1 by default) and rotation (0 by default). The colors in the texture are multiplied by the specified colorMask. See remarks for more info.
Public methodEnd End method ends adding sprites.
Protected methodFinalize Destrictor
(Overrides ObjectFinalize)
Public methodGetBitmapTextSize Returns the size of the specified text.
Public methodGetCoordinateCenter Gets the current position of a coordinate center. See remarks for SetCoordinateCenter(PositionTypes) for more info.
Public methodSetCoordinateCenter SetCoordinateCenter can set a custom position of a coordinate center. By default the center position is set to TopLeft, but with this method you can set it to any corner or center of the screen. See remarks for more info.
Public methodSetSpriteTexture Sets the specified GpuImage as the current sprite texture
Public methodSetTransform(Matrix3x2) Sets the transformation of the sprites that will be drawn after this method is called. Note that the coordinate system goes from (0, 0) at top-left corner to (1, 1) at bottom right corner. To rotate the sprite, set the destinationRectangle in the Draw command so that the center of the sprite will be at (0, 0).
Public methodSetTransform(Matrix3x2) Sets the transformation of the sprites that will be drawn after this method is called
Public methodUpdate Update method is automatically called from Scene or SceneView. It sets the Scene or SceneViews dirty flags when any absolute coordinates are used and the SceneView's size is changed.
Top
Fields
 NameDescription
Public fieldName Name of this SpriteBatch
Public fieldRenderingLayer RenderingLayer that will render this SpriteBatch.
Top
Remarks

SpriteBatch class defines a collection of sprites (textures) or texts that can be very quickly rendered to the 3D scene.

SpriteBatch objects can be created by calling Scene.CreateBackgroundSpriteBatch(String), Scene.CreateOverlaySpriteBatch(String), SceneView.CreateBackgroundSpriteBatch(String) or SceneView.CreateOverlaySpriteBatch(String). When the SpriteBatch is created from SceneView, then it can use absolute coordinates when calling Draw methods. In this case AreAbsoluteCoordinatesSupported is true and Begin(Boolean, Int32) method can be called with useAbsoluteCoordinates set to true or the IsUsingAbsoluteCoordinates property can be set to true.

To start rendering sprites, first call Begin(Boolean, Int32) method. To draw sprite first call SetSpriteTexture(GpuImage) method and then one or more DrawSprite methods. To draw rectangle call DrawRectangle(Vector2, Vector2, Color4, Single, Boolean) method. To draw text call DrawBitmapText methods (there are overloads with or without a custom BitmapTextCreator and with or without background color with margin). To complete adding sprites call End method.

If you have static and animated sprites or text that changes often, then it is recommended to put static sprites and texts into a one SpriteBatch, and animated sprites and text that change often into another SpriteBatch. This way the RenderingItems for the static SpriteBatch are generated only once and only the animated SpriteBatch needs to recreate its RenderingItems.

See Also