Find file History
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Improved ktx-graphics module. #138 Apr 29, 2018 Improved ktx-graphics module. #138 Apr 29, 2018
build.gradle Add ShapeRenderer utilities method Mar 27, 2018 Improved ktx-graphics module. #138 Apr 29, 2018

KTX : Graphics utilities

General utilities for handling LibGDX graphics-related API.


While LibGDX API is not particularly bad in this case and the KTX module provides only minor improvements, Kotlin build-in features can greatly simplify some common operations related to graphics and prevent some common bugs such as forgetting to start or end batch rendering.


Miscellaneous utilities

  • color factory methods allows to use idiomatic named parameters to construct LibGDX Color instances.
  • copy extension method added to Color. It allows to create a new Color with copied color values. Supports values overriding with optional, named parameters.
  • use inlined extension methods added to Batch and ShaderProgram. They allow to omit the begin() and end() calls before using batches and shader programs.


ShapeRenderer API, which normally consumes float primitives, was extended to support Vector2 and Vector3 instances instead of x, y and z position parameters. The extension methods have the same names as the original ShapeRenderer methods and perform the same actions. The methods include:

  • arc
  • box
  • circle
  • cone
  • ellipse
  • rect
  • rectLine
  • rotate
  • scale
  • translate
  • triange

Usage examples

Using a Batch:


val batch = SpriteBatch()
val font = BitmapFont()
// Drawing the font with batch:
batch.use {
  font.draw(it, "KTX!", 100f, 100f)

/* The snippet above is an equivalent to:

  font.draw(batch, "KTX!", 100f, 100f)

Using a ShaderProgram:


shaderProgram.use {
  // Operate on shaderProgram instance.

/* The snippet above is an equivalent to:

  // Operate on shaderProgram instance.

Creating Color instances:


val color = color(red = 1f, green = 0.5f, blue = 0.75f, alpha = 0.25f)
// Fourth parameter - alpha - is optional and defaults to 1f:
val nonTransparentGray = color(0.8f, 0.8f, 0.8f)

Copying Color instances:


val blue = Color.BLUE.copy()
// `blue` has same values as `Color.BLUE`, but it's not the same instance.

// You can optionally override chosen copied values:
val violet = blue.copy(red = 1f)

Using ShapeRenderer with vectors:

import com.badlogic.gdx.math.Vector2

fun drawCircle(renderer: ShapeRenderer) {
    val position = Vector2(1f, 0f), radius=5f)


Use ktx-math for Vector2 and Vector3 extensions, including idiomatic Kotlin factory methods and operator overloads.


There are some general purpose LibGDX utility libraries out there, but most lack first-class Kotlin support.

  • Kiwi is a general purpose Guava-inspired LibGDX Java utilities library with some utilities similar to ktx-graphics/

Additional documentation