Blogs & NewsJust our thoughts

Oct 10 2011 comments 6

PlayN, Cross-platform Game Abstraction Library

At Tahrir2 we have been investigating new game programming technologies and methodologies.

One of the most recent technologies is PlayN, formerly named as forplay, which was presented in Google I/O 2011′s Kick-ass Game Programming with Google Web Toolkit session.

The framework allows the development of one abstracted java game code using its Layers, Sounds APIs and the jbox2d as the physics engine, while keeping the Game Loop concepts.

Here we go into the details of how to get your game up and running using PlayN:

Note: previous maven experience would be of great help. PlayN requires Maven 3.x

Follow the Getting Started wiki page. NOTE:  you need to add android properties of the version and sdk path to the main maven pom file file playn/pom.xml in the <properties> tag

<android.version>2.3.3</android.version>
<android.sdk.path>$android-sdk-path</android.sdk.path>

I recommend developing on eclipse using the m2eclipse (maven to eclipse) plugin,  Google Plugin for Eclipse and Android Plugin for Eclipse and importing the playn folder as a maven project. (This will import many playn-* projects)

For new project creation with jbox2d physics engine enabled, I have been unsuccessful to generate a new project with jbox2d dependency from maven or eclipse using the playn architecture type.

So, I copied the showcase sample to mygame folder and changed the <artifactId> and <name> in the mygame/pom.xml to:

<artifactId>mygame</artifactId>
<name>My Game Metaproject</name>

and in the nested projects (core, html, flash, android) changing the <parent>, <artifactId> and <name> in their pom.xml files.

<parent>
    <groupId>com.googlecode.playn</groupId>
    <artifactId>angryarabs</artifactId>
    <version>1.0-SNAPSHOT</version>
</parent>
<artifactId>mygame-core</artifactId>
<name>My Game Core</name>

After finishing the development of the game on the JVM, you would want to test it against HTML, Flash and Android.

I tried and succeeded testing against JVM, HTML and Android.

Some commands I frequently use (on the directory of the project):

  1. ant run-java (Runs the game on the JVM)
  2. ant run-devmode (Runs the game in the GWT development mode, so we can see the logs and exceptions, if any. However the frame-rate is extremely low for real testing).
  3. ant run-html (Compiles the code using GWT for production).
  4. ant clean && mvn clean (clean the project files).
NOTE: ant commands are executed ant-maven bridge.
After creating new project and importing into eclipse, the android project seems not to be working.

Steps followed to get your Android version of the game to work:

First, I followed the Android Ports wiki page of playn. Again, didn’t seem to be working, as  dependencies weren’t found. I followed these steps to create an Android project

  1. Create new Android project with 2.3.3 as the target sdk. (e.g. mygame-android).
  2. In your AndroidManifest.xml, put in the <manifest> tag:
    <uses-sdk android:minSdkVersion="6" android:targetSdkVersion="10" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    and in the <activity> tag:

    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:configChanges="keyboardHidden|orientation">

    And in the activity

    package com.tahrir2.mygame;
    
    import playn.android.GameActivity;
    import playn.core.PlayN;
    import playn.showcase.core.Showcase;
    
    public class AngryarabsActivity extends GameActivity {
      @Override
      public void main() {
        platform().assetManager().setPathPrefix("playn/showcase/resources");
        PlayN.run(new Showcase());
      }
    }

    Leave the syntax errors for now, we will fix them later.

  3. $ cd mygame-android
  4. $ mkdir lib
  5. $ cp playn/android/target/playn-android-1.0-SNAPSHOT.jar lib/
  6. $ cp playn/java/target/playn-java-1.0-SNAPSHOT.jar lib/
  7. $ cp playn/android/lib/pythagoras-1.1-SNAPSHOT.jar lib/
  8. $ cp -rf playn/android/libs .
  9. Open mygame-android project’s Properties. If not, click on the Java Build Path selection. Under the Sources tab, you should already have two sources: mygame-src and mygame-res. In addition to this, you will need to add a Linked Source to the playn-android source. Hit the Linked Source… button and browse to the .playn/android/src folder for the Linked Folder Location. Call the Folder Name playn-android-src or something simliar.
  10. Do step 8 again: link playn/core/src to playn-core-src and link mygame/src to mygame-src.
  11. In your mygame folder create new folder called “assets” to hold the resources your game needs. (OpenGL needs 3 asset files we will copy soon).
  12. $ mkdir -p mygame-android/assets/playn/android/
  13. $ mkdir -p mygame-android/assets/playn/showcase
  14. $ cp playn/android/src/playn/android/tex-fragment-shader.txt mygame-android/assets/playn/android/
  15. $ cp playn/android/src/playn/android/vertex-shader.txt mygame-android/assets/playn/android/
  16. $ cp playn/android/src/playn/android/color-fragment-shader.txt mygame-android/assets/playn/android/
  17. Link to your resources in the path stated above in your activity playn/showcase/resources:
    $ ln -s mygame/core/src/playn/showcase/resources mygame-android/assets/playn/showcase/resources

Tips and tricks:

  • Avoid using either java.util.Timer and java.util.TimerTask or GWT’s single threaded timer into your game, If you really need Timer, you need to do your own Timer and updating it in your update method to trigger events.
  • Avoid references to device-specific (or GWT-only) classes in your game code.
  • Make sure you plan your controls around different input schemes. (e.g. most Android games shouldn’t be working with keyboard input).

Authored by: Mostafa Eweda @MostafaEweda

6 thoughts on “PlayN, Cross-platform Game Abstraction Library
  1. Hey, you stated in a much more direct way what I was trying to communicate, thanks, I will recommend your site to my friends.

    My site:
    rachat credit sans hypotheque rachatdecredit.net

  2. Superb advice! I have already been seeking something similar to this for some time now. Appreciate it!

  3. Could be hard to find skilled people on that topic, you be understood as you are aware of what you are posting on! Bless you

  4. hi,
    18/10/11 7:11:06 PM IST: Missing artifact com.sun:tools:jar:1.6:system

    This is the issue i meeting please help me solve this

  5. Nice article. Its realy nice. More info help me.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>