Kilgamore Castle is now free!

It's a game created by me for the Android platform. It's been on the Market for a while (as a paid app), but it is now available for free!

Download and play!

And the QR code:
qrcode

Being like Minecraft (or how to run your libgdx app in web browser)

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.java file and make it look like that:
 1 2 3 4 5 6 7 8 910
package com.badlogic.gdx.helloworld;
//import com.badlogic.gdx.backends.jogl.JoglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
public class HelloWorldDesktop {
public static void main (String[] argv) {
new LwjglApplication(new HelloWorld(), "Hello World", 480, 320, false);
}
}
Now you're using Lwjgl as your backend! All the cool kids use it, so you can consider yourself as cool too. Cool, eh?
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
package com.badlogic.gdx.helloworld;
import com.badlogic.gdx.backends.lwjgl.LwjglApplet;
public class HelloWorldApplet extends LwjglApplet
{
private static final long serialVersionUID = 1L;
public HelloWorldApplet()
{
super(new HelloWorld(), false);
}
}

Create a directory called applet in your project dir. It will contain all files needed to run your game through a browser. Put the following files in there:
  • gdx.jar
  • gdx-backend-lwjgl.jar
  • gdx-backend-lwjgl-natives.jar
  • gdx-natives.jar
You can copy them from your libgdx distribution. But that's not all! You will need a few more files:
  • 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 lwjgl-2.7.1/jar/lwjgl_util_applet.jar to your applet dir.
  • helloworld.jar - this is your game. Just export the whole project as jar in Eclipse:
    You can click "Finish" in this window.
  • index.html - this is the actual page that your browser will open. You should set some options for lwjgl_util_applet.jar here. 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>
    All the options are documented here.
Now we've got everything we need! Open index.html with 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 applet dir. 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 applet dir:
1
keytool -genkey -alias gdxkey -keypass gdxpassword -validity 360 -keystore .keystore
This command will create a .keystore file, 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 sign-applets.bat in the applet dir):
1234
@for /f %%i in ("%0") do @set curpath=%%~dpi
@cd /d %curpath%
@for /r . %%X in (*.jar) do jarsigner -keystore .keystore -storepass gdxpassword %%X gdxkey
Of course this will work for Windows users only. Linux users are capable enough to write a bash script or anything similar, and Mac users... well, sorry. :)
After signing the jars fire up index.html again. 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:
  1. Export .jar containing your game
  2. Sign this jar
If you're smart, you can write an Ant script for that. Obviously I am smart, but I'm also too lazy to do that.

The last step: show me your game! You need to upload everything except .keystore and 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.

Powered by Blogger