Skip to content

Using libGDX

Catvert edited this page Nov 16, 2017 · 15 revisions

Setup

First of all, we need to use the LWJGL3 backend in our project to be able to use imgui. To do this, I recommend using gdx-setup by czyzby which allows you to easily add LWJGL3 to your new project.

Once the project is imported in your favorite IDE, go to the build.gradle file located in the root of your project. Then add the following repositories in the subprojects block:

subprojects {
  ...
  repositories {
    ...
    maven { url 'https://jitpack.io' }
    maven { url "https://dl.bintray.com/kotlin/kotlin-dev" } // Required since imgui uses version 1.2 of Kotlin
  }
}

Next, go to the build.gradle file located in the core module and add the following dependencies:

dependencies {
  ...
  compile "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
  compile "com.github.kotlin-graphics:imgui:-SNAPSHOT"
}

Now, check if all the gradle dependencies have been recovered (Idea -> View -> Window Tool -> Gradle -> Force refresh all linked Gradle projects)

All that remains is to initialize imgui. To do this, go to your game class and add at the end of the create method:

/* We are constrained to use the reflection because libGDX does not allow for the moment (upcoming change) any other method to access the windowHandle variable required by GlfwWindow. */
val handle = (Gdx.graphics as Lwjgl3Graphics).window.let {
    it::class.java.getDeclaredField("windowHandle").apply { isAccessible = true }.getLong(it)
}
LwjglGL3.init(GlfwWindow(handle), false)

In the render method:

super.render()
Gdx.gl.glClearColor(0f, 0f, 0f, 1f)
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT or GL20.GL_DEPTH_BUFFER_BIT)
LwjglGL3.newFrame()
ImGui.text("Hi")
ImGui.render()

Finally, do not forget to dispose imgui in the dispose method:

LwjglGL3.shutdown()

That's all!

Using imgui image with libGDX texture

In order to use image and imageButton from imgui, we need to recover the ID handle from our texture in order to be able to pass it to imgui.

To retrieve this handle, just call the getTextureObjectHandle method of our texture and pass the result to imgui for the userTextureId as for example:

ImGui.image(texture.textureObjectHandle, Vec2(imageWidth, imageHeight))
Clone this wiki locally