Class TextureArrayCpuPolygonSpriteBatch

java.lang.Object
com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
com.github.tommyettinger.textra.TextureArrayPolygonSpriteBatch
com.github.tommyettinger.textra.TextureArrayCpuPolygonSpriteBatch
All Implemented Interfaces:
com.badlogic.gdx.graphics.g2d.Batch, com.badlogic.gdx.graphics.g2d.PolygonBatch, com.badlogic.gdx.utils.Disposable

public class TextureArrayCpuPolygonSpriteBatch extends TextureArrayPolygonSpriteBatch
TextureArrayCpuPolygonSpriteBatch behaves like a SpriteBatch with the polygon drawing features of a PolygonSpriteBatch, the transformation matrix optimizations of a CpuSpriteBatch, and optimizations for Batches that switch between Textures frequently. This can be useful when you need any of: PolygonSpriteBatch methods for drawing PolygonSprites, scene2d Groups with transform enabled, and/or drawing from multiple Textures.

If you're using this Batch to draw Fonts with a non-STANDARD Font.DistanceFieldType, you should read the documentation for TextureArrayShaders and use its TextureArrayShaders.initializeTextureArrayShaders() method after creating this Batch, but before using any KnownFonts methods.

This is an optimized version of the PolygonSpriteBatch that maintains an LFU texture-cache to combine draw calls with different textures effectively. It also uses CpuSpriteBatch's optimizations that avoid flushing when the transform matrix changes.

Use this Batch if you frequently utilize more than a single texture between calling TextureArrayPolygonSpriteBatch.begin() and TextureArrayPolygonSpriteBatch.end(). An example would be if your Atlas is spread over multiple Textures or if you draw with individual Textures. This can be a good "default" Batch implementation if you expect to use multiple Textures often, or use scene2d Groups often. In TextraTypist, typically each Font has its own large Texture, and if you use emoji or icons, those typically use a different large Texture. Switching between them has a performance cost, which is essentially eliminated by this Batch. There is more logic in this Batch, and each vertex needs slightly more data, which counterbalances the performance gains from more efficient Texture swaps. If you only use one Texture and don't use Groups where transform is enabled, this Batch is expected to perform somewhat worse than a SpriteBatch. Using many Textures or using Group transformation makes this perform relatively better than SpriteBatch in those cases. This is also a PolygonSpriteBatch (and it extends PolygonSpriteBatch), which makes it suitable for Spine animations.

Taken from Hyperlap2D's GitHub repo. Originally licensed under Apache 2.0, like TextraTypist and libGDX.

See Also:
  • Batch
  • SpriteBatch
  • Field Summary

    Fields inherited from interface com.badlogic.gdx.graphics.g2d.Batch

    C1, C2, C3, C4, U1, U2, U3, U4, V1, V2, V3, V4, X1, X2, X3, X4, Y1, Y2, Y3, Y4
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a CpuSpriteBatch with a size of 2000 and the default shader.
    Constructs a CpuSpriteBatch with the default shader.
    TextureArrayCpuPolygonSpriteBatch(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
    Constructs a CpuSpriteBatch with a custom shader.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    draw(com.badlogic.gdx.graphics.g2d.PolygonRegion region, float x, float y)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.PolygonRegion region, float x, float y, float width, float height)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.PolygonRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y, float width, float height)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)
     
    void
    draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float width, float height, com.badlogic.gdx.math.Affine2 transform)
     
    void
    draw(com.badlogic.gdx.graphics.Texture texture, float[] spriteVertices, int offset, int count)
     
    void
    draw(com.badlogic.gdx.graphics.Texture texture, float[] polygonVertices, int verticesOffset, int verticesCount, short[] polygonTriangles, int trianglesOffset, int trianglesCount)
    Draws the polygon using the given vertices and triangles.
    void
    draw(com.badlogic.gdx.graphics.Texture texture, float x, float y)
     
    void
    draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, float width, float height)
     
    void
    draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)
     
    void
    draw(com.badlogic.gdx.graphics.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)
     
    void
    draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)
     
    void
    draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)
     
    void
    Flushes the batch and realigns the real matrix on the GPU.
    com.badlogic.gdx.math.Matrix4
     
    void
    setTransformMatrix(com.badlogic.gdx.math.Affine2 transform)
    Sets the transform matrix to be used by this Batch.
    void
    setTransformMatrix(com.badlogic.gdx.math.Matrix4 transform)
    Sets the transform matrix to be used by this Batch.

    Methods inherited from class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch

    getColor, setColor, setColor, setPackedColor

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • TextureArrayCpuPolygonSpriteBatch

      public TextureArrayCpuPolygonSpriteBatch()
      Constructs a CpuSpriteBatch with a size of 2000 and the default shader.
      See Also:
      • SpriteBatch()
    • TextureArrayCpuPolygonSpriteBatch

      public TextureArrayCpuPolygonSpriteBatch(int size)
      Constructs a CpuSpriteBatch with the default shader.
      See Also:
      • SpriteBatch(int)
    • TextureArrayCpuPolygonSpriteBatch

      public TextureArrayCpuPolygonSpriteBatch(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
      Constructs a CpuSpriteBatch with a custom shader.
      See Also:
      • SpriteBatch(int, ShaderProgram)
  • Method Details

    • 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 com.badlogic.gdx.math.Matrix4 getTransformMatrix()
      Specified by:
      getTransformMatrix in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getTransformMatrix in class TextureArrayPolygonSpriteBatch
    • setTransformMatrix

      public void setTransformMatrix(com.badlogic.gdx.math.Matrix4 transform)
      Sets the transform matrix to be used by this Batch. Even if this is called inside a TextureArrayPolygonSpriteBatch.begin()/TextureArrayPolygonSpriteBatch.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 com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      setTransformMatrix in class TextureArrayPolygonSpriteBatch
    • setTransformMatrix

      public void setTransformMatrix(com.badlogic.gdx.math.Affine2 transform)
      Sets the transform matrix to be used by this Batch. Even if this is called inside a TextureArrayPolygonSpriteBatch.begin()/TextureArrayPolygonSpriteBatch.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 TextureArrayPolygonSpriteBatch.end().
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.PolygonRegion region, float x, float y)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.PolygonBatch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.PolygonRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.PolygonBatch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.PolygonRegion region, float x, float y, float width, float height)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.PolygonBatch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float x, float y)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.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)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, float width, float height)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, float width, float height, float u, float v, float u2, float v2)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, float width, float height, int srcX, int srcY, int srcWidth, int srcHeight, boolean flipX, boolean flipY)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float x, float y, int srcX, int srcY, int srcWidth, int srcHeight)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float[] polygonVertices, int verticesOffset, int verticesCount, short[] polygonTriangles, int trianglesOffset, int trianglesCount)
      Draws the polygon using the given vertices and triangles. Each vertex must be made up of 5 elements in this order: x, y, color, u, v.
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.PolygonBatch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float[] spriteVertices, int offset, int count)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float width, float height, com.badlogic.gdx.math.Affine2 transform)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y, float originX, float originY, float width, float height, float scaleX, float scaleY, float rotation, boolean clockwise)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.g2d.TextureRegion region, float x, float y, float width, float height)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      draw in class TextureArrayPolygonSpriteBatch