SpriteBatch class
The SpriteBatch API allows for rendering multiple items at once.
This class allows for optimization when you want to draw many parts of an image onto the canvas. It is more efficient than using multiple calls to Canvas.drawImageRect and provides more functionality by allowing each BatchItem to have their own transform rotation and color.
By collecting all the necessary transforms on a single image and sending those transforms in a single batch to the GPU, we can render multiple parts of a single image at once.
Note: If you are experiencing problems with ghost lines, the less
performant Canvas.drawImageRect can be used instead of Canvas.drawAtlas.
To activate this mode, pass in useAtlas = false to the constructor or
load method that you are using and each BatchItem will be rendered using
the Canvas.drawImageRect method instead.
Constructors
- SpriteBatch(Image atlas, {RSTransform? defaultTransform, bool useAtlas = true, Color? defaultColor, BlendMode? defaultBlendMode, Images? imageCache, String? imageKey})
Properties
- allocatedCount → int
-
The total number of allocated handles.
no setter
- atlas ↔ Image
-
The atlas used by the SpriteBatch.
getter/setter pair
-
colors
→ UnmodifiableListView<
Color> -
no setter
- defaultBlendMode → BlendMode?
-
The default blend mode, used for blending a batch item.
final
- defaultColor → Color?
-
The default color, used as a background color for a BatchItem on web.
final
- defaultTransform → RSTransform?
-
The default transform, used when a transform was not supplied for a
BatchItem.
final
- freeCount → int
-
The number of free handles.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- height → int
-
The height of the atlas.
no setter
- imageCache → Images
-
When the
_imageCacheisn't specified, the global Flame.images is used.no setter - imageKey → String
-
When the
_imageKeyisn't specified imageKey will return either the key for the atlas stored in imageCache or a key generated from the identityHashCode.no setter - isEmpty → bool
-
Does this batch contain any operations?
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- size → Vector2
-
The size of the atlas.
no setter
-
sources
→ UnmodifiableListView<
Rect> -
no setter
-
transforms
→ UnmodifiableListView<
RSTransform> -
no setter
- useAtlas → bool
-
Whether to use Canvas.drawAtlas or not.
final
- usedCount → int
-
The number of used handles.
no setter
- width → int
-
The width of the atlas.
no setter
Methods
-
add(
{required Rect source, double scale = 1.0, Vector2? anchor, double rotation = 0, Vector2? offset, bool flip = false, Color? color}) → int - Add a new batch item.
-
addTransform(
{required Rect source, RSTransform? transform, bool flip = false, Color? color}) → int - Add a new batch item using a RSTransform.
-
clear(
) → void - Clear the SpriteBatch so it can be reused.
-
getBatchItem(
int index) → BatchItem -
Gets the BatchItem at the given
index. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
removeAt(
int index) → void -
Removes the batch item at the given
index. -
render(
Canvas canvas, {BlendMode? blendMode, Rect? cullRect, Paint? paint}) → void -
replace(
int index, {Rect? source, Color? color, RSTransform? transform}) → void -
Replaces the parameters of the batch item at the given
index. At least one of the parameters must be different from null. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
load(
String path, {RSTransform? defaultTransform, Images? images, Color? defaultColor, BlendMode? defaultBlendMode, bool useAtlas = true}) → Future< SpriteBatch> - Takes a path of an image, and optional arguments for the SpriteBatch.