What's libgdx? It's a game/application development framework in Java. It allows you to write an app, and then run it on Android, on desktop and in the browser. Awesome! If you want to know more, read what its creators say about it.
In this tutorial I'll show you how to run a game written with libgdx in your browser (as a Java applet). So why Minecraft, you might ask - well, Minecraft runs as an applet! (and it's written with Lwjgl - more on that later) And talking about Minecraft is so snazzy these times, you know.
I'm going to use libgdx's "Hello world" as an example "game". I won't cover setting up the project here, Mario (father of libgdx) did it already here.
You should start with something like that:
For applet we'll need to use Lwjgl backend (instead of default Jogl). Don't worry, just one change is required. Edit the
HelloWorldDesktop.javafile and make it look like that:
1 2 3 4 5 6 7 8 910
Next step: add a new file called
HelloWorldApplet.java(in the same directory as other source files). It's similar to what we had before:
1 2 3 4 5 6 7 8 910111213
Create a directory called
appletin your project dir. It will contain all files needed to run your game through a browser. Put the following files in there:
lwjgl_util_applet.jar- this is the actual applet launcher. The Lwjgl guys made a sophisticated tool for that. It is configurable via an html file, which we will add in a moment. Get this launcher by downloading the latest Lwjgl distribution. It's 2.7.1 as I write it. Copy
helloworld.jar- this is your game. Just export the whole project as jar in Eclipse:
index.html- this is the actual page that your browser will open. You should set some options for
lwjgl_util_applet.jarhere. I'm using these:
1 2 3 4 5 6 7 8 910111213141516171819202122232425262728293031323334353637<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><title>HelloApplet!</title></head><body><div id="applet_container"><applet code="org.lwjgl.util.applet.AppletLoader" archive="lwjgl_util_applet.jar" codebase="." width="800" height="480"><param name="al_title" value="HelloApplet"><param name="al_main" value="com.badlogic.gdx.helloworld.HelloWorldApplet"><param name="al_logo" value="appletlogo.png"><param name="al_progressbar" value="appletprogress.gif"><param name="al_bgcolor" value="000000"><param name="al_fgcolor" value="ffffff"><param name="al_jars" value="helloworld.jar, gdx.jar, gdx-backend-lwjgl.jar"><param name="al_windows" value="gdx-natives.jar, gdx-backend-lwjgl-natives.jar"><param name="al_linux" value="gdx-natives.jar, gdx-backend-lwjgl-natives.jar"><param name="al_mac" value="gdx-natives.jar, gdx-backend-lwjgl-natives.jar"><param name="al_solaris" value="gdx-natives.jar, gdx-backend-lwjgl-natives.jar"><param name="codebase_lookup" value="false"><param name="java_arguments" value="-Dsun.java2d.noddraw=true -Dsun.awt.noerasebackground=true -Dsun.java2d.d3d=false -Dsun.java2d.opengl=false -Dsun.java2d.pmoffscreen=false -Xmx800M"><param name="lwjgl_arguments" value="-Dorg.lwjgl.input.Mouse.allowNegativeMouseCoords=true"><param name="separate_jvm" value="true"><param name="codebase_lookup" value="false"></applet></div></body></html>
index.htmlwith your browser and... it won't work. Why?
By default applets are allowed to use a restricted subset of Java. This is not enough for libgdx/lwjgl, so you need to sign ALL jars in the
appletdir. It can be done by standard JDK tools (they may be not in your path though). First, create a keystore with one key. Run the following command in the
.keystorefile, which will contain a single key named "gdxkey". I recommend setting the keystore password the same as your key password ("gdxpassword" in this case).
Now you can (and should) sign all jars. I'm using the following batchfile for that (copy the code and save it as
After signing the jars fire up
index.htmlagain. Answer some security question and enjoy libgdx in your browser!
I know it looks pretty complicated, but one you've set it all up, releasing a new applet version of your game is easy:
- Export .jar containing your game
- Sign this jar
The last step: show me your game! You need to upload everything except
sign-applets.bat(so basically all jars and
index.html) to some web server and give me the link! :)
I recommend Amazon S3 for hosting purposes (I'm using it for Pixel Slaughter!, Minecraft's using it too), but you can even publish it in your Dropbox. I did this too with my example project.