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

public Class CpuSpriteBatch

extends SpriteBatch
Class Inheritance
Imports
com.badlogic.gdx.graphics.Texture, com.badlogic.gdx.graphics.glutils.ShaderProgram, com.badlogic.gdx.math.Affine2, .MathUtils, .Matrix4, com.badlogic.gdx.utils.GdxRuntimeException

CpuSpriteBatch behaves like SpriteBatch, except it doesn't flush automatically whenever the transformation matrix changes. Instead, the vertices get adjusted on subsequent draws to match the running batch. This can improve performance through longer batches, for example when drawing Groups with transform enabled.
Author
Valentin Milea
See Also
SpriteBatch#renderCalls, Group.setTransform()

Field Summary

Modifier and TypeField and Description
private final Affine2
private boolean
private boolean
private final Affine2
private final Matrix4
Inherited from com.badlogic.gdx.graphics.g2d.SpriteBatch:
colorPackeddefaultVertexDataTypedrawingidxinvTexHeightinvTexWidthlastTexturemaxSpritesInBatchoverrideVertexTyperenderCallstotalRenderCallsvertices

Constructor Summary

AccessConstructor and Description
public
CpuSpriteBatch()

Constructs a CpuSpriteBatch with a size of 1000 and the default shader.

public
CpuSpriteBatch(int size)

Constructs a CpuSpriteBatch with the default shader.

public
CpuSpriteBatch(int size, ShaderProgram defaultShader)

Constructs a CpuSpriteBatch with a custom shader.

Method Summary

Modifier and TypeMethod and Description
private static boolean
private static boolean
checkEqual(Matrix4 matrix, Affine2 affine)

private static boolean
checkIdt(Matrix4 matrix)

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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

Draws a rectangle with the texture coordinates rotated 90 degrees.

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

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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

Draws a rectangle using the given vertices.

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

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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

Draws a rectangle transformed by the given matrix.

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

private void
drawAdjusted(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)

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

private void
drawAdjusted(TextureRegion region, float width, float height, Affine2 transform)

private void
drawAdjusted(Texture texture, float[] spriteVertices, int offset, int count)

private void
drawAdjustedUV(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, float u, float v, float u2, float v2, boolean flipX, boolean flipY)

public void
flushAndSyncTransformMatrix()

Flushes the batch and realigns the real matrix on the GPU.

public Matrix4
public void
public void
setTransformMatrix(Affine2 transform)

Sets the transform matrix to be used by this Batch.

Inherited from com.badlogic.gdx.graphics.g2d.SpriteBatch:
begincreateDefaultShaderdisableBlendingdisposeenableBlendingendflushgetBlendDstFuncgetBlendDstFuncAlphagetBlendSrcFuncgetBlendSrcFuncAlphagetColorgetPackedColorgetProjectionMatrixgetShaderisBlendingEnabledisDrawingsetBlendFunctionsetBlendFunctionSeparatesetColorsetColorsetPackedColorsetProjectionMatrixsetShadersetupMatricesswitchTexture

Field Detail

adjustAffineback to summary
private final Affine2 adjustAffine
adjustNeededback to summary
private boolean adjustNeeded
haveIdentityRealMatrixback to summary
private boolean haveIdentityRealMatrix
tmpAffineback to summary
private final Affine2 tmpAffine
virtualMatrixback to summary
private final Matrix4 virtualMatrix

Constructor Detail

CpuSpriteBatchback to summary
public CpuSpriteBatch()

Constructs a CpuSpriteBatch with a size of 1000 and the default shader.

See Also
SpriteBatch#SpriteBatch()
CpuSpriteBatchback to summary
public CpuSpriteBatch(int size)

Constructs a CpuSpriteBatch with the default shader.

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

Constructs a CpuSpriteBatch with a custom shader.

See Also
SpriteBatch#SpriteBatch(int, ShaderProgram)

Method Detail

checkEqualback to summary
private static boolean checkEqual(Matrix4 a, Matrix4 b)
checkEqualback to summary
private static boolean checkEqual(Matrix4 matrix, Affine2 affine)
checkIdtback to summary
private static boolean checkIdt(Matrix4 matrix)
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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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(Texture texture, float[] spriteVertices, int offset, int count)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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 width, float height, Affine2 transform)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.draw.

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
drawAdjustedback to summary
private void drawAdjusted(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
drawAdjustedback to summary
private void drawAdjusted(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)
drawAdjustedback to summary
private void drawAdjusted(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)
drawAdjustedback to summary
private void drawAdjusted(TextureRegion region, float width, float height, Affine2 transform)
drawAdjustedback to summary
private void drawAdjusted(Texture texture, float[] spriteVertices, int offset, int count)
drawAdjustedUVback to summary
private void drawAdjustedUV(Texture texture, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, float u, float v, float u2, float v2, boolean flipX, boolean flipY)
flushAndSyncTransformMatrixback to summary
public void flushAndSyncTransformMatrix()

Flushes the batch and realigns the real matrix on the GPU. Subsequent draws won't need adjustment and will be slightly faster as long as the transform matrix is not changed.

Note

The real transform matrix must be invertible. If a singular matrix is detected, GdxRuntimeException will be thrown.

See Also
SpriteBatch#flush()
getTransformMatrixback to summary
public Matrix4 getTransformMatrix()

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.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
setTransformMatrixback to summary
public void setTransformMatrix(Matrix4 transform)

Overrides com.badlogic.gdx.graphics.g2d.SpriteBatch.setTransformMatrix.

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

Sets the transform matrix to be used by this Batch. Even if this is called inside a begin()/end() block, the current batch is not flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by restoring the original matrix, or by calling flushAndSyncTransformMatrix().

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

Sets the transform matrix to be used by this Batch. Even if this is called inside a begin()/end() block, the current batch is not flushed to the GPU. Instead, for every subsequent draw() the vertices will be transformed on the CPU to match the original batch matrix. This adjustment must be performed until the matrices are realigned by restoring the original matrix, or by calling flushAndSyncTransformMatrix() or end().