Thursday, October 27, 2011

Slick Applet

I wanted to take the game that I'm creating and run it as an applet.  Slick actually makes it very easy to do so, but the hard part is gathering all the information you need to do it.  I thought I would post everything I had to do to get it to work.  Hopefully it will help someone else from hours of research to get it to work.

The great part about slick is once you get your game running as an application, you don't have to change a single line of code to run it as an applet. You just have to set things up properly (jars, html, etc). I'll assume that you have your game already functioning and just describe how to get that game to run as an applet.

First, you should get the necessary jar files.  I went straight to Lwjgl's download site and found the lwjgl_applet.zip file.  I copied all of these jar files into my applet folder (/var/www/myApplet for me because I'm using apache on linux).  You will also need to copy your slick.jar file in there.

Next up, you'll need to compile your game into a jar file.  I used an ant script to take care of that.  Here is what my build and jar targets look like:

    <target name="build" description="build library">
        <mkdir dir="build"/>
        <javac includeantruntime="false" srcdir="src/"
               encoding="utf-8" includes="**" destdir="build/" source="1.5">
            <classpath>
                <pathelement location="lib/lwjgl.jar"/>
                <pathelement location="lib/slick.jar"/>
            </classpath>
        </javac>
    </target>


    <target name="jar" depends="build">
        <mkdir dir="dist"/>
     <jar destfile="dist/rpg.jar">
      <fileset dir="build" includes="**"></fileset>
             <fileset dir="." includes="media/**"></fileset>
     </jar>
    </target>

My jar target needed to include all of the classes that were compiled (in the /build folder) and all of the media that my game uses (images, sounds, etc. located in the /media folder).  This creates a jar file (named rpg.jar) in the /dist directory.  Now we can copy that to our applet folder.

Next we need to create an html file to tell the browser how to load the applet.  Here is the html I used:

<html>
 <body>
  <applet code="org.lwjgl.util.applet.AppletLoader"
    archive="lwjgl_util_applet.jar"
    codebase="."
    width="800" height="600">
  
    <param name="al_title" value="AppletTitle">
    <param name="al_main" value="org.newdawn.slick.AppletGameContainer">
    <param name="game" value="myPackage.MyMainClass">  
  
    <param name="al_jars" value="rpg.jar, lwjgl.jar, slick.jar">
  
    <param name="al_windows" value="windows_natives.jar">
    <param name="al_linux" value="linux_natives.jar">
    <param name="al_mac" value="macosx_natives.jar">
  
    <param name="separate_jvm" value="true">
  </applet>
 </body>
</html>

Now that we have that part. The only thing that is left is to sign the jar files. I guess you don't really need to sign them, but its probably a good idea. A keystore for signing the jar files can be created by using the keytool that comes with the jdk:
$> keytool -genkey -alias mykey -keystore .keystore
Then sign each of the jar files in the directory:
$> jarsigner -keystore .keystore -storepass <password> -keypass <password> <jarfile.jar> mykey

That's all there is to it. Now use your browser to hit your applet.html file. It is also a good idea to turn on the java console for debugging purposes. In linux you should be able to type 'ControlPanel' in the terminal and find the 'Show Console' option. In windows? Idk, google should help.

Hopefully this saves some time for others creating a slick applet.