libGDX in sbt (2021)

libGDX is/was the bestest way to do OpenGL in Java/Scala on Desktop/Android.


background

It can do a bunch of platforms but the biggest appeal to me was that it really-really did work on both Windows and Android. Lots of libraries were touted as “JVM and Android” and then … only ran on Android … which isn’t using the JVM.

libGDX is pretty low-level, but, does have conventions to reduce a lot of the tedium. It doesn’t do the same amount of stuff as Unreal or Unity does for you - so you will need to reinvent a lot of wheels.

I wanted to do something (I forget what) with image processing in GLSL and tried using a libGDX window to do it. Getting it up and running was a task/chore in 2021 so I wrote these notes.1

.sbt file

You’ll need a .sbt file … like this?

resolvers += Resolver.jcenterRepo

scalaVersion := "2.12.13"

// add libGDX stuff
val gdxVersion = "1.9.3"
libraryDependencies ++=
	Seq(
		"com.badlogicgames.gdx" % "gdx" % gdxVersion,
		"com.badlogicgames.gdx" % "gdx-backend-lwjgl" % gdxVersion,
		"com.badlogicgames.gdx" % "gdx-platform" % gdxVersion classifier "natives-desktop",
	)

main task

Here is an example class/function. It opens a window, runs your task, then exits.

package peterlavalle.mimic

import com.badlogic.gdx.ApplicationAdapter

object OneRun {

	/**
	 * runs a libGDX app then exits after one frame ... likely for gpGPU reasons
	 *
	 * @param action to run on the frame
	 */
	def apply()(action: => Unit): Unit = {

		object lock {
			var done = false
		}

		class Application extends ApplicationAdapter {

			import com.badlogic.gdx.Gdx

			override def render(): Unit = {
				action
				Gdx.app.exit()
			}

			override def dispose(): Unit = {
				lock.synchronized {
					lock.done = true
					lock.notifyAll()
				}
			}
		}
		import com.badlogic.gdx.backends.lwjgl.{LwjglApplication, LwjglApplicationConfiguration}
		lock.synchronized {
			val config = new LwjglApplicationConfiguration()
			new LwjglApplication(new Application(), config)

			while (!lock.done)
				lock.wait()
		}
	}
}

  1. Which I’m posting today in 2022 ↩︎

comments powered by Disqus
Peter LaValle avatar
Peter LaValle
Any links probably include affiliate ids for that sweet sweet kickback - and some programs require that I tell you. The contents of this blog are likely unrelated - as they include games, paints, and build tools.