APIdia Launch Partner
Top Description Fields Constructors Methods
com.badlogic.gdx.graphics.g2d

public Class SpriteBatch

extends Object
implements Batch
Class Inheritance
All Implemented Interfaces
com.badlogic.gdx.graphics.g2d.Batch, com.badlogic.gdx.utils.Disposable
Known Direct Subclasses
com.badlogic.gdx.graphics.g2d.CpuSpriteBatch
Imports
com.badlogic.gdx.Gdx, com.badlogic.gdx.graphics.Color, .GL20, .Mesh, .Mesh.VertexDataType, .Texture, .VertexAttribute, .VertexAttributes.Usage, com.badlogic.gdx.graphics.glutils.ShaderProgram, com.badlogic.gdx.math.Affine2, .MathUtils, .Matrix4, java.nio.Buffer

Draws batched quads using indices.
Authors
mzechner, Nathan Sweet
See Also
Batch

Field Summary

Modifier and TypeField and Description
private int
private int
private boolean
private int
private int
private final Color
pack-priv float
private final Matrix4
private Mesh.VertexDataType
private ShaderProgram
public static Mesh.VertexDataType
defaultVertexDataType

Deprecated Do not use, this field is for testing only and is likely to be removed.
pack-priv boolean
pack-priv int
pack-priv float
pack-priv float
pack-priv Texture
public int
maxSpritesInBatch

The maximum number of sprites rendered in one batch so far.

private Mesh
public static Mesh.VertexDataType
overrideVertexType

Deprecated
Used to completely override the vertex type used by SpriteBatch.
private boolean
private final Matrix4
public int
renderCalls

Number of render calls since the last begin().

private final ShaderProgram
public int
totalRenderCalls

Number of rendering calls, ever.

private final Matrix4
pack-priv final float[]

Constructor Summary

AccessConstructor and Description
public
SpriteBatch()

Constructs a new SpriteBatch with a size of 1000, one buffer, and the default shader.

public
SpriteBatch(int size)

Constructs a SpriteBatch with one buffer and the default shader.

public
SpriteBatch(int
The max number of sprites in a single batch. Max of 8191.
size
,
ShaderProgram
The default shader to use. This is not owned by the SpriteBatch and must be disposed separately.
defaultShader
)

Constructs a new SpriteBatch.

Method Summary

Modifier and TypeMethod and Description
public void
begin()

Implements com.badlogic.gdx.graphics.g2d.Batch.begin.

Sets up the Batch for drawing.

public static ShaderProgram
createDefaultShader()

Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is specified.

public void
disableBlending()

Implements com.badlogic.gdx.graphics.g2d.Batch.disableBlending.

Disables blending for drawing sprites.

public void
dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

public void
draw(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.

public void
draw(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.

public void
draw(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.

public void
draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.

public void
draw(Texture texture, float x, float y)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the width and height of the texture.

public void
draw(Texture texture, float x, float y, float width, float height)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.

public void
draw(Texture texture, float[] spriteVertices, int offset, int count)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle using the given vertices.

public void
draw(TextureRegion region, float x, float y)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the width and height of the region.

public void
draw(TextureRegion region, float x, float y, float width, float height)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.

public void
draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.

public void
draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the texture coordinates rotated 90 degrees.

public void
draw(TextureRegion region, float width, float height, Affine2 transform)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle transformed by the given matrix.

public void
enableBlending()

Implements com.badlogic.gdx.graphics.g2d.Batch.enableBlending.

Enables blending for drawing sprites.

public void
end()

Implements com.badlogic.gdx.graphics.g2d.Batch.end.

Finishes off rendering.

public void
flush()

Implements com.badlogic.gdx.graphics.g2d.Batch.flush.

Causes any pending sprites to be rendered, without ending the Batch.

public int
public int
public int
public int
public Color
public float
public Matrix4
getProjectionMatrix()

Implements com.badlogic.gdx.graphics.g2d.Batch.getProjectionMatrix.

Returns the current projection matrix.

public ShaderProgram
public Matrix4
getTransformMatrix()

Implements com.badlogic.gdx.graphics.g2d.Batch.getTransformMatrix.

Returns the current transform matrix.

public boolean
public boolean
public void
setBlendFunction(int srcFunc, int dstFunc)

Implements com.badlogic.gdx.graphics.g2d.Batch.setBlendFunction.

Sets the blending function to be used when rendering sprites.

public void
setBlendFunctionSeparate(int srcFuncColor, int dstFuncColor, int srcFuncAlpha, int dstFuncAlpha)

Implements com.badlogic.gdx.graphics.g2d.Batch.setBlendFunctionSeparate.

Sets separate (color/alpha) blending function to be used when rendering sprites.

public void
setColor(Color tint)

Implements com.badlogic.gdx.graphics.g2d.Batch.setColor.

Sets the color used to tint images when they are added to the Batch.

public void
setColor(float r, float g, float b, float a)

Implements com.badlogic.gdx.graphics.g2d.Batch.setColor.

public void
setPackedColor(float packedColor)

Implements com.badlogic.gdx.graphics.g2d.Batch.setPackedColor.

Sets the rendering color of this Batch, expanding the alpha from 0-254 to 0-255.

public void
setProjectionMatrix(Matrix4 projection)

Implements com.badlogic.gdx.graphics.g2d.Batch.setProjectionMatrix.

Sets the projection matrix to be used by this Batch.

public void
setShader(ShaderProgram shader)

Implements com.badlogic.gdx.graphics.g2d.Batch.setShader.

Sets the shader to be used in a GLES 2.0 environment.

public void
setTransformMatrix(Matrix4 transform)

Implements com.badlogic.gdx.graphics.g2d.Batch.setTransformMatrix.

Sets the transform matrix to be used by this Batch.

protected void
protected void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blendDstFuncback to summary
private int blendDstFunc
blendDstFuncAlphaback to summary
private int blendDstFuncAlpha
blendingDisabledback to summary
private boolean blendingDisabled
blendSrcFuncback to summary
private int blendSrcFunc
blendSrcFuncAlphaback to summary
private int blendSrcFuncAlpha
colorback to summary
private final Color color
colorPackedback to summary
pack-priv float colorPacked
combinedMatrixback to summary
private final Matrix4 combinedMatrix
currentDataTypeback to summary
private Mesh.VertexDataType currentDataType
customShaderback to summary
private ShaderProgram customShader
defaultVertexDataTypeback to summary
public static Mesh.VertexDataType defaultVertexDataType

Deprecated

Do not use, this field is for testing only and is likely to be removed. Sets the VertexDataType to be used when gles 3 is not available, defaults to VertexDataType#VertexArray.

Annotations
@Deprecated
drawingback to summary
pack-priv boolean drawing
idxback to summary
pack-priv int idx
invTexHeightback to summary
pack-priv float invTexHeight
invTexWidthback to summary
pack-priv float invTexWidth
lastTextureback to summary
pack-priv Texture lastTexture
maxSpritesInBatchback to summary
public int maxSpritesInBatch

The maximum number of sprites rendered in one batch so far.

meshback to summary
private Mesh mesh
overrideVertexTypeback to summary
public static Mesh.VertexDataType overrideVertexType

Deprecated

Used to completely override the vertex type used by SpriteBatch. This is useful for picking a specific vertex data type on construction of the sprite batch. Recommended to reset this back to defaultVertexDataType Once the batch has been created with this flag

Annotations
@Deprecated
ownsShaderback to summary
private boolean ownsShader
projectionMatrixback to summary
private final Matrix4 projectionMatrix
renderCallsback to summary
public int renderCalls

Number of render calls since the last begin().

shaderback to summary
private final ShaderProgram shader
totalRenderCallsback to summary
public int totalRenderCalls

Number of rendering calls, ever. Will not be reset unless set manually.

transformMatrixback to summary
private final Matrix4 transformMatrix
verticesback to summary
pack-priv final float[] vertices

Constructor Detail

SpriteBatchback to summary
public SpriteBatch()

Constructs a new SpriteBatch with a size of 1000, one buffer, and the default shader.

See Also
SpriteBatch#SpriteBatch(int, ShaderProgram)
SpriteBatchback to summary
public SpriteBatch(int size)

Constructs a SpriteBatch with one buffer and the default shader.

See Also
SpriteBatch#SpriteBatch(int, ShaderProgram)
SpriteBatchback to summary
public SpriteBatch(int size, ShaderProgram defaultShader)

Constructs a new SpriteBatch. Sets the projection matrix to an orthographic projection with y-axis point upwards, x-axis point to the right and the origin being in the bottom left corner of the screen. The projection will be pixel perfect with respect to the current screen resolution.

The defaultShader specifies the shader to use. Note that the names for uniforms for this default shader are different than the ones expect for shaders set with setShader(ShaderProgram). See createDefaultShader().

Parameters
size:int

The max number of sprites in a single batch. Max of 8191.

defaultShader:ShaderProgram

The default shader to use. This is not owned by the SpriteBatch and must be disposed separately.

Method Detail

beginback to summary
public void begin()

Implements com.badlogic.gdx.graphics.g2d.Batch.begin.

Doc from com.badlogic.gdx.graphics.g2d.Batch.begin.

Sets up the Batch for drawing. This will disable depth buffer writing. It enables blending and texturing. If you have more texture units enabled than the first one you have to disable them before calling this. Uses a screen coordinate system by default where everything is given in pixels. You can specify your own projection and modelview matrices via setProjectionMatrix(Matrix4) and setTransformMatrix(Matrix4).

Annotations
@Override
createDefaultShaderback to summary
public static ShaderProgram createDefaultShader()

Returns a new instance of the default shader used by SpriteBatch for GL2 when no shader is specified.

disableBlendingback to summary
public void disableBlending()

Implements com.badlogic.gdx.graphics.g2d.Batch.disableBlending.

Doc from com.badlogic.gdx.graphics.g2d.Batch.disableBlending.

Disables blending for drawing sprites. Calling this within begin()/end() will flush the batch.

Annotations
@Override
disposeback to summary
public void dispose()

Implements com.badlogic.gdx.utils.Disposable.dispose.

Doc from com.badlogic.gdx.utils.Disposable.dispose.

Releases all resources of this object.

Annotations
@Override
drawback to summary
public void draw(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels. The rectangle is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter clockwise rotation of the rectangle around originX, originY. The portion of the Texture given by srcX, srcY and srcWidth, srcHeight is used. These coordinates and sizes are given in texels. FlipX and flipY specify whether the texture portion should be flipped horizontally or vertically.

Annotations
@Override
drawback to summary
public void draw(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels. The portion of the Texture given by srcX, srcY and srcWidth, srcHeight is used. These coordinates and sizes are given in texels. FlipX and flipY specify whether the texture portion should be flipped horizontally or vertically.

Annotations
@Override
drawback to summary
public void draw(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels. The portion of the Texture given by srcX, srcY and srcWidth, srcHeight are used. These coordinates and sizes are given in texels.

Annotations
@Override
drawback to summary
public void draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels. The portion of the Texture given by u, v and u2, v2 are used. These coordinates and sizes are given in texture size percentage. The rectangle will have the given tint Color.

Annotations
@Override
drawback to summary
public void draw(Texture texture, float x, float y)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the width and height of the texture.

Annotations
@Override
drawback to summary
public void draw(Texture texture, float x, float y, float width, float height)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.

Annotations
@Override
drawback to summary
public void draw(Texture texture, float[] spriteVertices, int offset, int count)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle using the given vertices. There must be 4 vertices, each made up of 5 elements in this order: x, y, color, u, v. The getColor() from the Batch is not applied.

Annotations
@Override
drawback to summary
public void draw(TextureRegion region, float x, float y)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y having the width and height of the region.

Annotations
@Override
drawback to summary
public void draw(TextureRegion region, float x, float y, float width, float height)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.

Annotations
@Override
drawback to summary
public void draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height. The rectangle is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter clockwise rotation of the rectangle around originX, originY.

Annotations
@Override
drawback to summary
public void draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle with the texture coordinates rotated 90 degrees. The bottom left corner at x,y and stretching the region to cover the given width and height. The rectangle is offset by originX, originY relative to the origin. Scale specifies the scaling factor by which the rectangle should be scaled around originX, originY. Rotation specifies the angle of counter clockwise rotation of the rectangle around originX, originY.

Annotations
@Override
drawback to summary
public void draw(TextureRegion region, float width, float height, Affine2 transform)

Implements com.badlogic.gdx.graphics.g2d.Batch.draw.

Doc from com.badlogic.gdx.graphics.g2d.Batch.draw.

Draws a rectangle transformed by the given matrix.

Annotations
@Override
enableBlendingback to summary
public void enableBlending()

Implements com.badlogic.gdx.graphics.g2d.Batch.enableBlending.

Doc from com.badlogic.gdx.graphics.g2d.Batch.enableBlending.

Enables blending for drawing sprites. Calling this within begin()/end() will flush the batch.

Annotations
@Override
endback to summary
public void end()

Implements com.badlogic.gdx.graphics.g2d.Batch.end.

Doc from com.badlogic.gdx.graphics.g2d.Batch.end.

Finishes off rendering. Enables depth writes, disables blending and texturing. Must always be called after a call to begin()

Annotations
@Override
flushback to summary
public void flush()

Implements com.badlogic.gdx.graphics.g2d.Batch.flush.

Doc from com.badlogic.gdx.graphics.g2d.Batch.flush.

Causes any pending sprites to be rendered, without ending the Batch.

Annotations
@Override
getBlendDstFuncback to summary
public int getBlendDstFunc()

Implements com.badlogic.gdx.graphics.g2d.Batch.getBlendDstFunc.

Annotations
@Override
getBlendDstFuncAlphaback to summary
public int getBlendDstFuncAlpha()

Implements com.badlogic.gdx.graphics.g2d.Batch.getBlendDstFuncAlpha.

Annotations
@Override
getBlendSrcFuncback to summary
public int getBlendSrcFunc()

Implements com.badlogic.gdx.graphics.g2d.Batch.getBlendSrcFunc.

Annotations
@Override
getBlendSrcFuncAlphaback to summary
public int getBlendSrcFuncAlpha()

Implements com.badlogic.gdx.graphics.g2d.Batch.getBlendSrcFuncAlpha.

Annotations
@Override
getColorback to summary
public Color getColor()

Implements com.badlogic.gdx.graphics.g2d.Batch.getColor.

Annotations
@Override
getPackedColorback to summary
public float getPackedColor()

Implements com.badlogic.gdx.graphics.g2d.Batch.getPackedColor.

Annotations
@Override
getProjectionMatrixback to summary
public Matrix4 getProjectionMatrix()

Implements com.badlogic.gdx.graphics.g2d.Batch.getProjectionMatrix.

Doc from com.badlogic.gdx.graphics.g2d.Batch.getProjectionMatrix.

Returns the current projection matrix. Changing this within begin()/end() results in undefined behaviour.

Annotations
@Override
getShaderback to summary
public ShaderProgram getShader()

Implements com.badlogic.gdx.graphics.g2d.Batch.getShader.

Annotations
@Override
getTransformMatrixback to summary
public Matrix4 getTransformMatrix()

Implements com.badlogic.gdx.graphics.g2d.Batch.getTransformMatrix.

Doc from com.badlogic.gdx.graphics.g2d.Batch.getTransformMatrix.

Returns the current transform matrix. Changing this within begin()/end() results in undefined behaviour.

Annotations
@Override
isBlendingEnabledback to summary
public boolean isBlendingEnabled()

Implements com.badlogic.gdx.graphics.g2d.Batch.isBlendingEnabled.

Annotations
@Override
isDrawingback to summary
public boolean isDrawing()

Implements com.badlogic.gdx.graphics.g2d.Batch.isDrawing.

setBlendFunctionback to summary
public void setBlendFunction(int srcFunc, int dstFunc)

Implements com.badlogic.gdx.graphics.g2d.Batch.setBlendFunction.

Doc from com.badlogic.gdx.graphics.g2d.Batch.setBlendFunction.

Sets the blending function to be used when rendering sprites.

Annotations
@Override
setBlendFunctionSeparateback to summary
public void setBlendFunctionSeparate(int srcFuncColor, int dstFuncColor, int srcFuncAlpha, int dstFuncAlpha)

Implements com.badlogic.gdx.graphics.g2d.Batch.setBlendFunctionSeparate.

Doc from com.badlogic.gdx.graphics.g2d.Batch.setBlendFunctionSeparate.

Sets separate (color/alpha) blending function to be used when rendering sprites.

Annotations
@Override
setColorback to summary
public void setColor(Color tint)

Implements com.badlogic.gdx.graphics.g2d.Batch.setColor.

Doc from com.badlogic.gdx.graphics.g2d.Batch.setColor.

Sets the color used to tint images when they are added to the Batch. Default is Color#WHITE.

Annotations
@Override
setColorback to summary
public void setColor(float r, float g, float b, float a)

Implements com.badlogic.gdx.graphics.g2d.Batch.setColor.

Annotations
@Override
setPackedColorback to summary
public void setPackedColor(float packedColor)

Implements com.badlogic.gdx.graphics.g2d.Batch.setPackedColor.

Doc from com.badlogic.gdx.graphics.g2d.Batch.setPackedColor.

Sets the rendering color of this Batch, expanding the alpha from 0-254 to 0-255.

Annotations
@Override
setProjectionMatrixback to summary
public void setProjectionMatrix(Matrix4 projection)

Implements com.badlogic.gdx.graphics.g2d.Batch.setProjectionMatrix.

Doc from com.badlogic.gdx.graphics.g2d.Batch.setProjectionMatrix.

Sets the projection matrix to be used by this Batch. If this is called inside a begin()/end() block, the current batch is flushed to the gpu.

Annotations
@Override
setShaderback to summary
public void setShader(ShaderProgram shader)

Implements com.badlogic.gdx.graphics.g2d.Batch.setShader.

Doc from com.badlogic.gdx.graphics.g2d.Batch.setShader.

Sets the shader to be used in a GLES 2.0 environment. Vertex position attribute is called "a_position", the texture coordinates attribute is called "a_texCoord0", the color attribute is called "a_color". See ShaderProgram#POSITION_ATTRIBUTE, ShaderProgram#COLOR_ATTRIBUTE and ShaderProgram#TEXCOORD_ATTRIBUTE which gets "0" appended to indicate the use of the first texture unit. The combined transform and projection matrx is uploaded via a mat4 uniform called "u_projTrans". The texture sampler is passed via a uniform called "u_texture".

Call this method with a null argument to use the default shader.

This method will flush the batch before setting the new shader, you can call it in between begin() and end().

Annotations
@Override
setTransformMatrixback to summary
public void setTransformMatrix(Matrix4 transform)

Implements com.badlogic.gdx.graphics.g2d.Batch.setTransformMatrix.

Doc from com.badlogic.gdx.graphics.g2d.Batch.setTransformMatrix.

Sets the transform matrix to be used by this Batch.

Annotations
@Override
setupMatricesback to summary
protected void setupMatrices()
switchTextureback to summary
protected void switchTexture(Texture texture)