Package com.badlogic.gdx.graphics.g2d
Class CpuSpriteBatch
- java.lang.Object
-
- com.badlogic.gdx.graphics.g2d.SpriteBatch
-
- com.badlogic.gdx.graphics.g2d.CpuSpriteBatch
-
- All Implemented Interfaces:
Batch
,Disposable
public class CpuSpriteBatch extends SpriteBatch
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.- See Also:
SpriteBatch.renderCalls
,Group.setTransform()
-
-
Field Summary
-
Fields inherited from class com.badlogic.gdx.graphics.g2d.SpriteBatch
defaultVertexDataType, maxSpritesInBatch, renderCalls, totalRenderCalls
-
-
Constructor Summary
Constructors Constructor Description CpuSpriteBatch()
Constructs a CpuSpriteBatch with a size of 1000 and the default shader.CpuSpriteBatch(int size)
Constructs a CpuSpriteBatch with the default shader.CpuSpriteBatch(int size, ShaderProgram defaultShader)
Constructs a CpuSpriteBatch with a custom shader.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
draw(TextureRegion region, float x, float y)
Draws a rectangle with the bottom left corner at x,y having the width and height of the region.void
draw(TextureRegion region, float x, float y, float width, float height)
Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.void
draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.void
draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)
Draws a rectangle with the texture coordinates rotated 90 degrees.void
draw(TextureRegion region, float width, float height, Affine2 transform)
Draws a rectangle transformed by the given matrix.void
draw(Texture texture, float[] spriteVertices, int offset, int count)
Draws a rectangle using the given vertices.void
draw(Texture texture, float x, float y)
Draws a rectangle with the bottom left corner at x,y having the width and height of the texture.void
draw(Texture texture, float x, float y, float width, float height)
Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.void
draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)
Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.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)
Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.void
draw(Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)
Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.void
draw(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)
Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels.void
flushAndSyncTransformMatrix()
Flushes the batch and realigns the real matrix on the GPU.Matrix4
getTransformMatrix()
Returns the current transform matrix.void
setTransformMatrix(Affine2 transform)
Sets the transform matrix to be used by this Batch.void
setTransformMatrix(Matrix4 transform)
Sets the transform matrix to be used by this Batch.-
Methods inherited from class com.badlogic.gdx.graphics.g2d.SpriteBatch
begin, createDefaultShader, disableBlending, dispose, enableBlending, end, flush, getBlendDstFunc, getBlendDstFuncAlpha, getBlendSrcFunc, getBlendSrcFuncAlpha, getColor, getPackedColor, getProjectionMatrix, getShader, isBlendingEnabled, isDrawing, setBlendFunction, setBlendFunctionSeparate, setColor, setColor, setPackedColor, setProjectionMatrix, setShader, setupMatrices, switchTexture
-
-
-
-
Constructor Detail
-
CpuSpriteBatch
public CpuSpriteBatch()
Constructs a CpuSpriteBatch with a size of 1000 and the default shader.- See Also:
SpriteBatch()
-
CpuSpriteBatch
public CpuSpriteBatch(int size)
Constructs a CpuSpriteBatch with the default shader.- See Also:
SpriteBatch(int)
-
CpuSpriteBatch
public CpuSpriteBatch(int size, ShaderProgram defaultShader)
Constructs a CpuSpriteBatch with a custom shader.- See Also:
SpriteBatch(int, ShaderProgram)
-
-
Method Detail
-
flushAndSyncTransformMatrix
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()
-
getTransformMatrix
public Matrix4 getTransformMatrix()
Description copied from interface:Batch
Returns the current transform matrix. Changing this withinBatch.begin()
/Batch.end()
results in undefined behaviour.- Specified by:
getTransformMatrix
in interfaceBatch
- Overrides:
getTransformMatrix
in classSpriteBatch
-
setTransformMatrix
public void setTransformMatrix(Matrix4 transform)
Sets the transform matrix to be used by this Batch. Even if this is called inside aSpriteBatch.begin()
/SpriteBatch.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 callingflushAndSyncTransformMatrix()
.- Specified by:
setTransformMatrix
in interfaceBatch
- Overrides:
setTransformMatrix
in classSpriteBatch
-
setTransformMatrix
public void setTransformMatrix(Affine2 transform)
Sets the transform matrix to be used by this Batch. Even if this is called inside aSpriteBatch.begin()
/SpriteBatch.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 callingflushAndSyncTransformMatrix()
orSpriteBatch.end()
.
-
draw
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)
Description copied from interface:Batch
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 theTexture
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.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
x
- the x-coordinate in screen spacey
- the y-coordinate in screen spaceoriginX
- the x-coordinate of the scaling and rotation origin relative to the screen space coordinatesoriginY
- the y-coordinate of the scaling and rotation origin relative to the screen space coordinateswidth
- the width in pixelsheight
- the height in pixelsscaleX
- the scale of the rectangle around originX/originY in xscaleY
- the scale of the rectangle around originX/originY in yrotation
- the angle of counter clockwise rotation of the rectangle around originX/originYsrcX
- the x-coordinate in texel spacesrcY
- the y-coordinate in texel spacesrcWidth
- the source with in texelssrcHeight
- the source height in texelsflipX
- whether to flip the sprite horizontallyflipY
- whether to flip the sprite vertically
-
draw
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)
Description copied from interface:Batch
Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels. The portion of theTexture
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.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
x
- the x-coordinate in screen spacey
- the y-coordinate in screen spacewidth
- the width in pixelsheight
- the height in pixelssrcX
- the x-coordinate in texel spacesrcY
- the y-coordinate in texel spacesrcWidth
- the source with in texelssrcHeight
- the source height in texelsflipX
- whether to flip the sprite horizontallyflipY
- whether to flip the sprite vertically
-
draw
public void draw(Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)
Description copied from interface:Batch
Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels. The portion of theTexture
given by srcX, srcY and srcWidth, srcHeight are used. These coordinates and sizes are given in texels.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
x
- the x-coordinate in screen spacey
- the y-coordinate in screen spacesrcX
- the x-coordinate in texel spacesrcY
- the y-coordinate in texel spacesrcWidth
- the source with in texelssrcHeight
- the source height in texels
-
draw
public void draw(Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)
Description copied from interface:Batch
Draws a rectangle with the bottom left corner at x,y having the given width and height in pixels. The portion of theTexture
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 tintColor
.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
x
- the x-coordinate in screen spacey
- the y-coordinate in screen spacewidth
- the width in pixelsheight
- the height in pixels
-
draw
public void draw(Texture texture, float x, float y)
Description copied from interface:Batch
Draws a rectangle with the bottom left corner at x,y having the width and height of the texture.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
x
- the x-coordinate in screen spacey
- the y-coordinate in screen space
-
draw
public void draw(Texture texture, float x, float y, float width, float height)
Description copied from interface:Batch
Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
-
draw
public void draw(TextureRegion region, float x, float y)
Description copied from interface:Batch
Draws a rectangle with the bottom left corner at x,y having the width and height of the region.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
-
draw
public void draw(TextureRegion region, float x, float y, float width, float height)
Description copied from interface:Batch
Draws a rectangle with the bottom left corner at x,y and stretching the region to cover the given width and height.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
-
draw
public void draw(TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
Description copied from interface:Batch
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.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
-
draw
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)
Description copied from interface:Batch
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.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
clockwise
- If true, the texture coordinates are rotated 90 degrees clockwise. If false, they are rotated 90 degrees counter clockwise.
-
draw
public void draw(Texture texture, float[] spriteVertices, int offset, int count)
Description copied from interface:Batch
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. TheBatch.getColor()
from the Batch is not applied.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
-
draw
public void draw(TextureRegion region, float width, float height, Affine2 transform)
Description copied from interface:Batch
Draws a rectangle transformed by the given matrix.- Specified by:
draw
in interfaceBatch
- Overrides:
draw
in classSpriteBatch
-
-