Automates buffer segmentation when rendering 2D geometry with multiple unique textures.

Primary use-case is to allow usage of multiple textures without the need to manually manage them. Causes extra draw call each time a texture is swapped. Due to that, for production it is recommended to combine assets in atlases for optimal performance.

Depending on geometry type, vertex count should be in groups of 4 vertices per quad or 3 indices per triangle.

Constructor

new()

Create a new BatchDrawState instance.

Variables

read onlycurrentTexture:Texture

Current active texture of the BatchDrawState. Represents the most recent texture that was set with setTile or setTexture. Always null after state initialization or after clear call.

read onlytotalCount:Int

A total amount of vertices added to the BatchDrawState.

Methods

inlineadd(count:Int):Void

Add vertices to the state using currently active texture. Should be called when rendering buffers add more data in order to properly render the geometry.

Parameters:

count

The amount of vertices to add.

clear():Void

Resets the BatchDrawState by removing all texture references and zeroing vertex counter.

@:value({ length : -1, offset : 0 })drawIndexed(ctx:RenderContext, buffer:Buffer, indices:Indexes, offset:Int = 0, length:Int = -1):Void

Renders given indices as a set of triangles. Index data should be in groups of 3 vertices per quad.

Parameters:

ctx

The render context which performs the rendering. Rendering object should call h2d.RenderContext.beginDrawBatchState before calling drawQuads.

buffer

The vertex buffer used to render the state.

indices

Vertex indices used to render the state.

offset

An optional starting offset of the buffer to render in triangles.

length

An optional maximum limit of triangles to render. When offset and length are not provided or are default values, slightly faster rendering routine is used.

@:value({ length : -1, offset : 0 })drawQuads(ctx:RenderContext, buffer:Buffer, offset:Int = 0, length:Int = -1):Void

Renders given buffer as a set of quads. Buffer data should be in groups of 4 vertices per quad.

Parameters:

ctx

The render context which performs the rendering. Rendering object should call h2d.RenderContext.beginDrawBatchState before calling drawQuads.

buffer

The quad buffer used to render the state.

offset

An optional starting offset of the buffer to render in triangles (2 per quad).

length

An optional maximum limit of triangles to render. When offset and length are not provided or are default values, slightly faster rendering routine is used.

setTexture(texture:Texture):Void

Switches currently active texture to the given texture if it differs and splits the render state.

Parameters:

texture

The texture that should be used for the next set of vertices. Does nothing if null.

inlinesetTile(tile:Tile):Void

Switches currently active texture to one in the given tile if it differs and splits the render state.

Parameters:

tile

A Tile containing a texture that should be used for the next set of vertices. Does nothing if null.