Class 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()
    • 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 java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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)
    • 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()
      • setTransformMatrix

        public void setTransformMatrix​(Matrix4 transform)
        Sets the transform matrix to be used by this Batch. Even if this is called inside a SpriteBatch.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 calling flushAndSyncTransformMatrix().
        Specified by:
        setTransformMatrix in interface Batch
        Overrides:
        setTransformMatrix in class SpriteBatch
      • setTransformMatrix

        public void setTransformMatrix​(Affine2 transform)
        Sets the transform matrix to be used by this Batch. Even if this is called inside a SpriteBatch.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 calling flushAndSyncTransformMatrix() or SpriteBatch.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 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.
        Specified by:
        draw in interface Batch
        Overrides:
        draw in class SpriteBatch
        x - the x-coordinate in screen space
        y - the y-coordinate in screen space
        originX - the x-coordinate of the scaling and rotation origin relative to the screen space coordinates
        originY - the y-coordinate of the scaling and rotation origin relative to the screen space coordinates
        width - the width in pixels
        height - the height in pixels
        scaleX - the scale of the rectangle around originX/originY in x
        scaleY - the scale of the rectangle around originX/originY in y
        rotation - the angle of counter clockwise rotation of the rectangle around originX/originY
        srcX - the x-coordinate in texel space
        srcY - the y-coordinate in texel space
        srcWidth - the source with in texels
        srcHeight - the source height in texels
        flipX - whether to flip the sprite horizontally
        flipY - 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 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.
        Specified by:
        draw in interface Batch
        Overrides:
        draw in class SpriteBatch
        x - the x-coordinate in screen space
        y - the y-coordinate in screen space
        width - the width in pixels
        height - the height in pixels
        srcX - the x-coordinate in texel space
        srcY - the y-coordinate in texel space
        srcWidth - the source with in texels
        srcHeight - the source height in texels
        flipX - whether to flip the sprite horizontally
        flipY - 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 the Texture given by srcX, srcY and srcWidth, srcHeight are used. These coordinates and sizes are given in texels.
        Specified by:
        draw in interface Batch
        Overrides:
        draw in class SpriteBatch
        x - the x-coordinate in screen space
        y - the y-coordinate in screen space
        srcX - the x-coordinate in texel space
        srcY - the y-coordinate in texel space
        srcWidth - the source with in texels
        srcHeight - 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 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.
        Specified by:
        draw in interface Batch
        Overrides:
        draw in class SpriteBatch
        x - the x-coordinate in screen space
        y - the y-coordinate in screen space
        width - the width in pixels
        height - 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 interface Batch
        Overrides:
        draw in class SpriteBatch
        x - the x-coordinate in screen space
        y - 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 interface Batch
        Overrides:
        draw in class SpriteBatch
      • 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 interface Batch
        Overrides:
        draw in class SpriteBatch
      • 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 interface Batch
        Overrides:
        draw in class SpriteBatch
      • 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 interface Batch
        Overrides:
        draw in class SpriteBatch
      • 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 interface Batch
        Overrides:
        draw in class SpriteBatch
        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. The Batch.getColor() from the Batch is not applied.
        Specified by:
        draw in interface Batch
        Overrides:
        draw in class SpriteBatch
      • 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 interface Batch
        Overrides:
        draw in class SpriteBatch