FlixelSpriteBatch
View sourceclass
org.flixelgdx.graphics.FlixelSpriteBatch
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()
Creates a batch with max quads and auto-detected texture slot count.
FlixelSpriteBatch(int)
Creates a batch with the given sprite capacity.
Parameters:
| Name | Description |
|---|---|
maxQuads | Maximum quads (sprites) that can be batched before an automatic flush. |
Methods
begin()
end()
flush()
setColor(Color)
setColor(float, float, float, float)
setPackedColor(float)
getColor()
getPackedColor()
draw(Texture, float, float, float, float, float, float, float, float, float, int, int, int, int, boolean, boolean)
draw(Texture, float, float, float, float, int, int, int, int, boolean, boolean)
draw(Texture, float, float, int, int, int, int)
draw(Texture, float, float, float, float, float, float, float, float)
draw(Texture, float, float, float, float)
draw(Texture, float, float)
draw(Texture, float[], int, int)
draw(TextureRegion, float, float)
draw(TextureRegion, float, float, float, float)
draw(TextureRegion, float, float, float, float, float, float, float, float, float)
draw(TextureRegion, float, float, float, float, float, float, float, float, float, boolean)
draw(TextureRegion, float, float, Affine2)
disableBlending()
enableBlending()
setBlendFunction(int, int)
setBlendFunctionSeparate(int, int, int, int)
getBlendSrcFunc()
getBlendDstFunc()
getBlendSrcFuncAlpha()
getBlendDstFuncAlpha()
getProjectionMatrix()
getTransformMatrix()
setProjectionMatrix(Matrix4)
setTransformMatrix(Matrix4)
setShader(ShaderProgram)
getShader()
isBlendingEnabled()
isDrawing()
dispose()
getRenderCalls()
getTotalRenderCalls()
getMaxTextureSlots()
Returns the number of texture slots this batch can hold before it must flush.