Skip to main content

FlixelSpriteBatch

View source

class

org.flixelgdx.graphics.FlixelSpriteBatch

public class FlixelSpriteBatch implements FlixelBatch

A multi-texture sprite batch that reduces GPU draw calls by binding up to FlixelSpriteBatch.getMaxTextureSlots() distinct textures per flush instead of one.

The standard libGDX SpriteBatch flushes its vertex buffer every time the active texture changes. FlixelSpriteBatch instead assigns each texture to an OpenGL texture unit (slot 0 through N-1) and records that slot index as a per-vertex attribute. The generated fragment shader reads from the correct sampler based on that index, so a single draw call can render quads from up to N different textures at once.

A flush is only triggered when:

  • All texture slots are occupied and a new, unseen texture is encountered.
  • The internal vertex buffer is full.
  • FlixelSpriteBatch.end() is called.
  • Blend state or matrices are changed mid-frame.

The slot count is determined at construction from GL_MAX_TEXTURE_IMAGE_UNITS, capped at 16 so the fragment shader's if-else chain stays reasonable on all drivers.

Usage example:

FlixelBatch batch = new FlixelSpriteBatch();
batch.setProjectionMatrix(camera.combined);
batch.begin();
batch.draw(texture, x, y, width, height);
batch.end();

Custom shaders: a shader passed to FlixelSpriteBatch.setShader(ShaderProgram) must declare the same vertex attributes as the built-in shader (a_position, a_color, a_texCoord0, a_texIndex) and the same sampler uniforms (u_texture0 through u_textureN-1) to work correctly with multi-texture draws.

Constructors

FlixelSpriteBatch()

public FlixelSpriteBatch()

Creates a batch with max quads and auto-detected texture slot count.


FlixelSpriteBatch(int)

public FlixelSpriteBatch(int maxQuads)

Creates a batch with the given sprite capacity.

Parameters:

NameDescription
maxQuadsMaximum quads (sprites) that can be batched before an automatic flush.

Methods

begin()

public void begin()

end()

public void end()

flush()

public void flush()

setColor(Color)

public void setColor(Color tint)

setColor(float, float, float, float)

public void setColor(float r, float g, float b, float a)

setPackedColor(float)

public void setPackedColor(float packed)

getColor()

public Color getColor()

getPackedColor()

public float getPackedColor()

draw(Texture, float, float, float, float, float, float, float, float, float, int, int, int, int, boolean, boolean)

public void draw(Texture texture, float x, float y, float originX, float originY, float w, float h, float scaleX, float scaleY, float rotation, int srcX, int srcY, int srcW, int srcH, boolean flipX, boolean flipY)

draw(Texture, float, float, float, float, int, int, int, int, boolean, boolean)

public void draw(Texture texture, float x, float y, float w, float h, int srcX, int srcY, int srcW, int srcH, boolean flipX, boolean flipY)

draw(Texture, float, float, int, int, int, int)

public void draw(Texture texture, float x, float y, int srcX, int srcY, int srcW, int srcH)

draw(Texture, float, float, float, float, float, float, float, float)

public void draw(Texture texture, float x, float y, float w, float h, float u, float v, float u2, float v2)

draw(Texture, float, float, float, float)

public void draw(Texture texture, float x, float y, float w, float h)

draw(Texture, float, float)

public void draw(Texture texture, float x, float y)

draw(Texture, float[], int, int)

public void draw(Texture texture, float[] spriteVertices, int offset, int count)

draw(TextureRegion, float, float)

public void draw(TextureRegion region, float x, float y)

draw(TextureRegion, float, float, float, float)

public void draw(TextureRegion region, float x, float y, float w, float h)

draw(TextureRegion, float, float, float, float, float, float, float, float, float)

public void draw(TextureRegion region, float x, float y, float originX, float originY, float w, float h, float scaleX, float scaleY, float rotation)

draw(TextureRegion, float, float, float, float, float, float, float, float, float, boolean)

public void draw(TextureRegion region, float x, float y, float originX, float originY, float w, float h, float scaleX, float scaleY, float rotation, boolean clockwise)

draw(TextureRegion, float, float, Affine2)

public void draw(TextureRegion region, float width, float height, Affine2 transform)

disableBlending()

public void disableBlending()

enableBlending()

public void enableBlending()

setBlendFunction(int, int)

public void setBlendFunction(int src, int dst)

setBlendFunctionSeparate(int, int, int, int)

public void setBlendFunctionSeparate(int srcColor, int dstColor, int srcAlpha, int dstAlpha)

getBlendSrcFunc()

public int getBlendSrcFunc()

getBlendDstFunc()

public int getBlendDstFunc()

getBlendSrcFuncAlpha()

public int getBlendSrcFuncAlpha()

getBlendDstFuncAlpha()

public int getBlendDstFuncAlpha()

getProjectionMatrix()

public Matrix4 getProjectionMatrix()

getTransformMatrix()

public Matrix4 getTransformMatrix()

setProjectionMatrix(Matrix4)

public void setProjectionMatrix(Matrix4 projection)

setTransformMatrix(Matrix4)

public void setTransformMatrix(Matrix4 transform)

setShader(ShaderProgram)

public void setShader(ShaderProgram shader)

getShader()

public ShaderProgram getShader()

isBlendingEnabled()

public boolean isBlendingEnabled()

isDrawing()

public boolean isDrawing()

dispose()

public void dispose()

getRenderCalls()

public int getRenderCalls()

getTotalRenderCalls()

public int getTotalRenderCalls()

getMaxTextureSlots()

public int getMaxTextureSlots()

Returns the number of texture slots this batch can hold before it must flush.