Class TextureArrayPolygonSpriteBatch

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

public class TextureArrayPolygonSpriteBatch extends com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
TextureArrayPolygonSpriteBatch behaves like a SpriteBatch with the polygon drawing features of a PolygonSpriteBatch and optimizations for Batches that switch between Textures frequently. This can be useful when you need either PolygonSpriteBatch methods for drawing PolygonSprites or drawing from multiple Textures. If you use transform matrices, such as from scene2d Groups with transform enabled, you should prefer the subclass TextureArrayCpuPolygonSpriteBatch instead.

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.

Use this Batch if you frequently utilize more than a single texture between calling begin() and end(). An example would be if your Atlas is spread over multiple Textures or if you draw with individual Textures. This extends PolygonSpriteBatch), which makes it suitable for Spine animations. Consider using TextureArrayCpuPolygonSpriteBatch instead, which may perform better in GUIs.

Taken from Hyperlap2D's GitHub repo. Originally licensed under Apache 2.0, like TextraTypist and libGDX. The tint field, which modified the Batch color, has been removed because it was unused in TextraTypist.

See Also:
  • Batch
  • SpriteBatch
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    protected float
     
    int
    The maximum number of triangles rendered in one batch so far.
    int
    Number of render calls since the last begin().
    static final String
     
    int
    Number of rendering calls, ever.

    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 TextureArrayPolygonSpriteBatch with the default shader, 2000 vertices, and 4000 triangles.
    Constructs a TextureArrayPolygonSpriteBatch with the default shader, size vertices, and size * 2 triangles.
    TextureArrayPolygonSpriteBatch(int maxVertices, int maxTriangles, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
    Constructs a new PolygonSpriteBatch.
    TextureArrayPolygonSpriteBatch(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
    Constructs a TextureArrayPolygonSpriteBatch with the specified shader, size vertices and size * 2 triangles.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected int
    activateTexture(com.badlogic.gdx.graphics.Texture texture)
    Assigns Texture units and manages the LFU cache.
    void
     
    static com.badlogic.gdx.graphics.glutils.ShaderProgram
     
    void
     
    void
     
    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)
     
    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
     
    void
    end()
     
    void
     
    int
     
    int
     
    int
     
    int
     
    static int
    Queries the number of supported textures in a texture array by trying to create the default shader.
    The first call of this method is very expensive, after that it simply returns a cached value.
    float
     
    com.badlogic.gdx.math.Matrix4
     
    com.badlogic.gdx.graphics.glutils.ShaderProgram
     
    int
     
    int
     
    int
     
    com.badlogic.gdx.math.Matrix4
     
    boolean
     
    boolean
     
    void
    setBlendFunction(int srcFunc, int dstFunc)
     
    void
    setBlendFunctionSeparate(int srcFuncColor, int dstFuncColor, int srcFuncAlpha, int dstFuncAlpha)
     
    void
    setProjectionMatrix(com.badlogic.gdx.math.Matrix4 projection)
     
    void
    setShader(com.badlogic.gdx.graphics.glutils.ShaderProgram shader)
     
    void
    setTransformMatrix(com.badlogic.gdx.math.Matrix4 transform)
     
    protected void
     
    protected void
    switchTexture(com.badlogic.gdx.graphics.Texture texture)
     

    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
  • Field Details

    • TEXTURE_INDEX_ATTRIBUTE

      public static final String TEXTURE_INDEX_ATTRIBUTE
      See Also:
    • colorPacked

      protected float colorPacked
    • renderCalls

      public int renderCalls
      Number of render calls since the last begin().
    • totalRenderCalls

      public int totalRenderCalls
      Number of rendering calls, ever. Will not be reset unless set manually.
    • maxTrianglesInBatch

      public int maxTrianglesInBatch
      The maximum number of triangles rendered in one batch so far.
  • Constructor Details

    • TextureArrayPolygonSpriteBatch

      public TextureArrayPolygonSpriteBatch()
      Constructs a TextureArrayPolygonSpriteBatch with the default shader, 2000 vertices, and 4000 triangles.
      See Also:
    • TextureArrayPolygonSpriteBatch

      public TextureArrayPolygonSpriteBatch(int size)
      Constructs a TextureArrayPolygonSpriteBatch with the default shader, size vertices, and size * 2 triangles.
      Parameters:
      size - The max number of vertices and number of triangles in a single batch. Max of 32767.
      See Also:
    • TextureArrayPolygonSpriteBatch

      public TextureArrayPolygonSpriteBatch(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
      Constructs a TextureArrayPolygonSpriteBatch with the specified shader, size vertices and size * 2 triangles.
      Parameters:
      size - The max number of vertices and number of triangles in a single batch. Max of 32767.
      See Also:
    • TextureArrayPolygonSpriteBatch

      public TextureArrayPolygonSpriteBatch(int maxVertices, int maxTriangles, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
      Constructs a new PolygonSpriteBatch. 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 from the ones expect for shaders set with setShader(ShaderProgram). See SpriteBatch.createDefaultShader().

      Parameters:
      maxVertices - The max number of vertices in a single batch. Max of 32767.
      maxTriangles - The max number of triangles in a single batch.
      defaultShader - The default shader to use. This is not owned by the PolygonSpriteBatch and must be disposed separately. May be null to use the default shader.
  • Method Details

    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • draw

      public void draw(com.badlogic.gdx.graphics.Texture texture, float[] polygonVertices, int verticesOffset, int verticesCount, short[] polygonTriangles, int trianglesOffset, int trianglesCount)
      Specified by:
      draw in interface com.badlogic.gdx.graphics.g2d.PolygonBatch
      Overrides:
      draw in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • 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 com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • begin

      public void begin()
      Specified by:
      begin in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      begin in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • end

      public void end()
      Specified by:
      end in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      end in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getPackedColor

      public float getPackedColor()
      Specified by:
      getPackedColor in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getPackedColor in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getTextureLFUSwaps

      public int getTextureLFUSwaps()
      Returns:
      The number of texture swaps the LFU cache performed since calling begin().
    • getTextureLFUSize

      public int getTextureLFUSize()
      Returns:
      The current number of textures in the LFU cache. Gets reset when calling begin().
    • getTextureLFUCapacity

      public int getTextureLFUCapacity()
      Returns:
      The maximum number of textures that the LFU cache can hold. This limit is imposed by the driver.
    • disableBlending

      public void disableBlending()
      Specified by:
      disableBlending in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      disableBlending in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • enableBlending

      public void enableBlending()
      Specified by:
      enableBlending in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      enableBlending in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • setBlendFunction

      public void setBlendFunction(int srcFunc, int dstFunc)
      Specified by:
      setBlendFunction in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      setBlendFunction in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • setBlendFunctionSeparate

      public void setBlendFunctionSeparate(int srcFuncColor, int dstFuncColor, int srcFuncAlpha, int dstFuncAlpha)
      Specified by:
      setBlendFunctionSeparate in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      setBlendFunctionSeparate in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getBlendSrcFunc

      public int getBlendSrcFunc()
      Specified by:
      getBlendSrcFunc in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getBlendSrcFunc in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getBlendDstFunc

      public int getBlendDstFunc()
      Specified by:
      getBlendDstFunc in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getBlendDstFunc in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getBlendSrcFuncAlpha

      public int getBlendSrcFuncAlpha()
      Specified by:
      getBlendSrcFuncAlpha in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getBlendSrcFuncAlpha in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getBlendDstFuncAlpha

      public int getBlendDstFuncAlpha()
      Specified by:
      getBlendDstFuncAlpha in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getBlendDstFuncAlpha in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • dispose

      public void dispose()
      Specified by:
      dispose in interface com.badlogic.gdx.utils.Disposable
      Overrides:
      dispose in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getProjectionMatrix

      public com.badlogic.gdx.math.Matrix4 getProjectionMatrix()
      Specified by:
      getProjectionMatrix in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getProjectionMatrix in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getTransformMatrix

      public com.badlogic.gdx.math.Matrix4 getTransformMatrix()
      Specified by:
      getTransformMatrix in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getTransformMatrix in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • setProjectionMatrix

      public void setProjectionMatrix(com.badlogic.gdx.math.Matrix4 projection)
      Specified by:
      setProjectionMatrix in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      setProjectionMatrix in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • setTransformMatrix

      public void setTransformMatrix(com.badlogic.gdx.math.Matrix4 transform)
      Specified by:
      setTransformMatrix in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      setTransformMatrix in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • flush

      public void flush()
      Specified by:
      flush in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      flush in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • setupMatrices

      protected void setupMatrices()
      Overrides:
      setupMatrices in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • activateTexture

      protected int activateTexture(com.badlogic.gdx.graphics.Texture texture)
      Assigns Texture units and manages the LFU cache.
      Parameters:
      texture - The texture that shall be loaded into the cache, if it is not already loaded.
      Returns:
      The texture slot that has been allocated to the selected texture
    • setShader

      public void setShader(com.badlogic.gdx.graphics.glutils.ShaderProgram shader)
      Specified by:
      setShader in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      setShader in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getShader

      public com.badlogic.gdx.graphics.glutils.ShaderProgram getShader()
      Specified by:
      getShader in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      getShader in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • isBlendingEnabled

      public boolean isBlendingEnabled()
      Specified by:
      isBlendingEnabled in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      isBlendingEnabled in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • isDrawing

      public boolean isDrawing()
      Specified by:
      isDrawing in interface com.badlogic.gdx.graphics.g2d.Batch
      Overrides:
      isDrawing in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch
    • getMaxTextureUnits

      public static int getMaxTextureUnits()
      Queries the number of supported textures in a texture array by trying to create the default shader.
      The first call of this method is very expensive, after that it simply returns a cached value.
      Returns:
      the number of supported textures in a texture array or zero if this feature is unsupported on this device.
    • createDefaultShader

      public static com.badlogic.gdx.graphics.glutils.ShaderProgram createDefaultShader()
    • switchTexture

      protected void switchTexture(com.badlogic.gdx.graphics.Texture texture)
      Overrides:
      switchTexture in class com.badlogic.gdx.graphics.g2d.PolygonSpriteBatch