Package squidpony.squidgrid.gui.gdx
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
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,
As a more complex example, changing the warmth of a color (as well as its lightness) is relatively easy with a
Created by Tommy Ettinger on 8/2/2018.
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 lastbegin()
.int
totalRenderCalls
Number of rendering calls, ever. -
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)
-
Field Details
-
filter
-
renderCalls
Number of render calls since the lastbegin()
. -
totalRenderCalls
Number of rendering calls, ever. Will not be reset unless set manually. -
maxSpritesInBatch
The maximum number of sprites rendered in one batch so far.
-
-
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
Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and the given color filter. -
FilterBatch
Constructs a FilterBatch with one buffer, the default shader, and no changes in the color filter. -
FilterBatch
Constructs a new FilterBatch with a size of 1000, one buffer, the default shader, and the given color filter. -
FilterBatch
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)
. SeecreateDefaultShader()
.- 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)
. SeecreateDefaultShader()
.- 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
Returns a new instance of the default shader used by FilterBatch for GL2 when no shader is specified. -
begin
- Specified by:
begin
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
end
- Specified by:
end
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
setColor
- Specified by:
setColor
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
setColor
- Specified by:
setColor
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
setColor
-
setPackedColor
- Specified by:
setPackedColor
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getColor
- Specified by:
getColor
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getPackedColor
- Specified by:
getPackedColor
in interfacecom.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 interfacecom.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 interfacecom.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 interfacecom.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 interfacecom.badlogic.gdx.graphics.g2d.Batch
-
draw
- Specified by:
draw
in interfacecom.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 interfacecom.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 interfacecom.badlogic.gdx.graphics.g2d.Batch
-
draw
- Specified by:
draw
in interfacecom.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 interfacecom.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 interfacecom.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 interfacecom.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 interfacecom.badlogic.gdx.graphics.g2d.Batch
-
flush
- Specified by:
flush
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
disableBlending
- Specified by:
disableBlending
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
enableBlending
- Specified by:
enableBlending
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
setBlendFunction
- Specified by:
setBlendFunction
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
setBlendFunctionSeparate
public void setBlendFunctionSeparate(int srcFuncColor, int dstFuncColor, int srcFuncAlpha, int dstFuncAlpha)- Specified by:
setBlendFunctionSeparate
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getBlendSrcFunc
- Specified by:
getBlendSrcFunc
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getBlendDstFunc
- Specified by:
getBlendDstFunc
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getBlendSrcFuncAlpha
- Specified by:
getBlendSrcFuncAlpha
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getBlendDstFuncAlpha
- Specified by:
getBlendDstFuncAlpha
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
dispose
- Specified by:
dispose
in interfacecom.badlogic.gdx.utils.Disposable
-
getProjectionMatrix
- Specified by:
getProjectionMatrix
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getTransformMatrix
- Specified by:
getTransformMatrix
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
setProjectionMatrix
- Specified by:
setProjectionMatrix
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
setTransformMatrix
- Specified by:
setTransformMatrix
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
switchTexture
-
setShader
- Specified by:
setShader
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getShader
- Specified by:
getShader
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
isBlendingEnabled
- Specified by:
isBlendingEnabled
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
isDrawing
- Specified by:
isDrawing
in interfacecom.badlogic.gdx.graphics.g2d.Batch
-
getFilter
-
setFilter
-