The world of gamedev tutorials and Java is a hazardous one. The internet is full of outdated, broken tutorials that use long-abandoned bindings to OpenGL, engines that use out of date libraries, shit “personal toolkits” that work fine for a particular developer but no-one else and all other kinds of nonsense. How do I know this? Well, I decided that I wanted to investigate how OpenGL fits together with Java a bit more, and thus I ventured down this rabbit hole, getting highly frustrated with what I found before finally managing to distil all the knowledge found in various sources into one thing that finally worked.
I decided that whatever I wanted to do, I was going to go with the Lightweight Java Game Library, or LJWGL as it’s more commonly known. LJWGL is a library that offers low-level access from Java to OpenGL, OpenCL and OpenAL, and if you’re looking for a decent library for doing experiments with low-level graphics coding seems to be the only game in town. I investigated JOGL, but that seemed to be dead.
As I mentioned previously, the journey to getting that wonderful Hello World window proved somewhat… frustrating. There are quite a few tutorials out there, but none that seem to cover all of the bits needed leading to a lot of frustration when trying to get things such as a basic window working. So, here’s how to get everything set up in LJWGL 3, using Maven.
I’m assuming that you have the following installed:
- JDK 1.8
- Apache Maven
Provided you have those two requirements, you should be able to do everything I do, and get that Hello World window working correctly. My experience is also on Ubuntu 16.04 as well, so your mileage may vary on other platforms. I might go and try it out on Windows at some point, as I suspect the experience is highly similar although things might be different. I’m also not intending to explicitly teach how to use Maven here, although I guess it could work as a nice Hello World for that too.
If you just want to get straight down to the example, you can find the code on my Github here. Anyway…
So, to get started, go to the directory you want to start your project in, and generate the Maven project on the command line:
$ mvn archetype:generate -DgroupId=com.theanswers42 -DartifactId=lwjgltest -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false
This should create a project called
lwjgltest in the directory, with a file called
App.java created in the
src/main/java/com/theanswers42/ directory. We’re just going to use the example on the LWJGL site to get it up and running, so change
HelloWorld.java, and then paste the code from the LWJGL getting started guide in over the generated code in
With that out of the way, we now come to the Maven
pom.xml file. Not going to lie, I found this a pain in the arse. But we can hack the
pom.xml file so it’ll compile properly. The first part of the file is the
artifactId and the packaging method. They’re here just so you know where to go…
So after this, we now get into something more interesting. Next, we’re going to bring in the awesome
exec-maven plugin from Mojohaus. This is the secret sauce
of the whole thing, as most tutorials I’d encountered about getting started tended to use Netbeans or Eclipse to make sure that the classpath included the LJWGL binaries. But we want to do it from the commandline, so…
This should bring in the
exec-maven plugin, and allow you to execute it after compilation by merely typing
mvn exec:java at the command line.
The important part to get right is your
mainClass, this being
com.theanswers42.HelloWorld for this example. Next, bring in the project properties from the LWJGL download page. It has a pretty cool configurator which gives you the correct XML for your
pom, and I just used the minimal OpenGL configuration.
mvn compile at the commandline to build, and then
mvn exec:java, and you should see:
Yeah, that isn’t too exciting, but as Nathan Drake might say, sic parvis magna, greatness from small beginnings. You’ve now got a properly working LJWGL hello world project to start from! Where do we go from here? Well, I’ve got some ideas, but I’m not sharing yet (also, they still need developed). For the moment, just knowing that we’ve got a valid, working example of LJWGL with no “hidden steps” or reliance on Netbeans will have to be enough…