Class FilterBatch

java.lang.Object
squidpony.squidgrid.gui.gdx.FilterBatch
All Implemented Interfaces:
com.badlogic.gdx.graphics.g2d.Batch, com.badlogic.gdx.utils.Disposable

public class FilterBatch
extends Object
implements com.badlogic.gdx.graphics.g2d.Batch
A drop-in substitute for SpriteBatch that filters any colors used to tint text or images using a FloatFilter. The filter may have an effect on speed in some cases, but even moderately complex filters like FloatFilters.YCbCrFilter seem to perform perfectly well, spiking at above 1000 FPS on SparseDemo with a filter that changes parameters. This class should be preferred over SpriteBatch when using SquidLib's style of frequently changing the Batch color; as of libGDX 1.9.9, SpriteBatch is somewhat slower than FilterBatch when FilterBatch is using the default FloatFilters.identityFilter. This is because 1.9.9 changed SpriteBatch to do more work when setting a color, and FilterBatch keeps the older, faster way of color changing. It may be slower if used for Tiled map renderers in libGDX or for NinePatch instances, which both need to reset a Color object frequently, but not much else in libGDX specifically needs the Color object getColor() returns, and many things do fine with getPackedColor(), which is faster. Note that for games that use 2D sprite graphics, there's very low overhead to 1.9.9's SpriteBatch, and it mostly has issues with overdraw (which every glyph of text drawn over a background contributes towards).
Most FloatFilter varieties don't have much overhead, and I encourage you to try some to quickly produce graphical effects in a game. As a simple example, FloatFilters.grayscaleFilter is a constant FloatFilter that makes all colors, well, grayscale; you could apply it during things like flashback sequences or when the player is dazed.
As a more complex example, changing the warmth of a color (as well as its lightness) is relatively easy with a FloatFilters.YCwCmFilter that adds to or subtracts from Cw (chromatic warmth) and may change Y (lightness). With that same filter, if the protagonist is partially blinded, you could set the multipliers for Cw and Cm (chromatic mildness; determines whether a color is closer to mild colors like green/yellow or bold colors like red/blue) to floats between 0.1 and 0.5 or so, which takes even intense colors and makes them diluted and grayish. You could also change Y to be brighter or darker depending on whether a bright light blinded the protagonist, or some mud was thrown in their eyes, etc.
Created by Tommy Ettinger on 8/2/2018.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    FloatFilter filter  
    int maxSpritesInBatch
    The maximum number of sprites rendered in one batch so far.
    int renderCalls
    Number of render calls since the last begin().
    int totalRenderCalls
    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
    FilterBatch()
    Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and no changes in the color filter.
    FilterBatch​(int size)
    Constructs a FilterBatch with one buffer, the default shader, and no changes in the color filter.
    FilterBatch​(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
    Constructs a new FilterBatch.
    FilterBatch​(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader, FloatFilter filter)
    Constructs a new FilterBatch.
    FilterBatch​(int size, FloatFilter filter)
    Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and the given color filter.
    FilterBatch​(FloatFilter filter)
    Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and the given color filter.
  • Method Summary

    Modifier and Type Method Description
    void begin()  
    static com.badlogic.gdx.graphics.glutils.ShaderProgram createDefaultShader()
    Returns a new instance of the default shader used by FilterBatch for GL2 when no shader is specified.
    void disableBlending()  
    void dispose()  
    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 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 enableBlending()  
    void end()  
    void flush()  
    int getBlendDstFunc()  
    int getBlendDstFuncAlpha()  
    int getBlendSrcFunc()  
    int getBlendSrcFuncAlpha()  
    com.badlogic.gdx.graphics.Color getColor()  
    FloatFilter getFilter()  
    float getPackedColor()  
    com.badlogic.gdx.math.Matrix4 getProjectionMatrix()  
    com.badlogic.gdx.graphics.glutils.ShaderProgram getShader()  
    com.badlogic.gdx.math.Matrix4 getTransformMatrix()  
    boolean isBlendingEnabled()  
    boolean isDrawing()  
    void setBlendFunction​(int srcFunc, int dstFunc)  
    void setBlendFunctionSeparate​(int srcFuncColor, int dstFuncColor, int srcFuncAlpha, int dstFuncAlpha)  
    void setColor​(float color)  
    void setColor​(float r, float g, float b, float a)  
    void setColor​(com.badlogic.gdx.graphics.Color tint)  
    void setFilter​(FloatFilter filter)  
    void setPackedColor​(float color)  
    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 switchTexture​(com.badlogic.gdx.graphics.Texture texture)  

    Methods inherited from class java.lang.Object

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

  • Constructor Details

    • FilterBatch

      public FilterBatch()
      Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and no changes in the color filter.
      See Also:
      FilterBatch(int, ShaderProgram)
    • FilterBatch

      public FilterBatch​(FloatFilter filter)
      Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and the given color filter.
      See Also:
      FilterBatch(int, ShaderProgram, FloatFilter)
    • FilterBatch

      public FilterBatch​(int size)
      Constructs a FilterBatch with one buffer, the default shader, and no changes in the color filter.
      See Also:
      FilterBatch(int, ShaderProgram, FloatFilter)
    • FilterBatch

      public FilterBatch​(int size, FloatFilter filter)
      Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and the given color filter.
      See Also:
      FilterBatch(int, ShaderProgram, FloatFilter)
    • FilterBatch

      public FilterBatch​(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader)
      Constructs a new FilterBatch. 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 than the ones expect for shaders set with setShader(ShaderProgram). See createDefaultShader().

      Parameters:
      size - The max number of sprites in a single batch. Max of 8191.
      defaultShader - The default shader to use. This is not owned by the FilterBatch and must be disposed separately.
    • FilterBatch

      public FilterBatch​(int size, com.badlogic.gdx.graphics.glutils.ShaderProgram defaultShader, FloatFilter filter)
      Constructs a new FilterBatch. 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 than the ones expect for shaders set with setShader(ShaderProgram). See createDefaultShader().

      Parameters:
      size - The max number of sprites in a single batch. Max of 8191.
      defaultShader - The default shader to use. This is not owned by the FilterBatch and must be disposed separately.
  • Method Details

    • createDefaultShader

      public static com.badlogic.gdx.graphics.glutils.ShaderProgram createDefaultShader()
      Returns a new instance of the default shader used by FilterBatch for GL2 when no shader is specified.
    • begin

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

      public void end()
      Specified by:
      end in interface com.badlogic.gdx.graphics.g2d.Batch
    • setColor

      public void setColor​(com.badlogic.gdx.graphics.Color tint)
      Specified by:
      setColor in interface com.badlogic.gdx.graphics.g2d.Batch
    • setColor

      public void setColor​(float r, float g, float b, float a)
      Specified by:
      setColor in interface com.badlogic.gdx.graphics.g2d.Batch
    • setColor

      public void setColor​(float color)
    • setPackedColor

      public void setPackedColor​(float color)
      Specified by:
      setPackedColor in interface com.badlogic.gdx.graphics.g2d.Batch
    • getColor

      public com.badlogic.gdx.graphics.Color getColor()
      Specified by:
      getColor in interface com.badlogic.gdx.graphics.g2d.Batch
    • getPackedColor

      public float getPackedColor()
      Specified by:
      getPackedColor in interface com.badlogic.gdx.graphics.g2d.Batch
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • 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
    • flush

      public void flush()
      Specified by:
      flush in interface com.badlogic.gdx.graphics.g2d.Batch
    • disableBlending

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

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

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

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

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

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

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

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

      public void dispose()
      Specified by:
      dispose in interface com.badlogic.gdx.utils.Disposable
    • getProjectionMatrix

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

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

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

      public void setTransformMatrix​(com.badlogic.gdx.math.Matrix4 transform)
      Specified by:
      setTransformMatrix in interface com.badlogic.gdx.graphics.g2d.Batch
    • switchTexture

      protected void switchTexture​(com.badlogic.gdx.graphics.Texture texture)
    • setShader

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

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

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

      public boolean isDrawing()
      Specified by:
      isDrawing in interface com.badlogic.gdx.graphics.g2d.Batch
    • getFilter

    • setFilter

      public void setFilter​(FloatFilter filter)