Making libraries compatible with GWT

Cross-platform deployment is surely one of the best features of Libgdx. However, supporting a platform is hard, even when Libgdx does the dirty work through Google Web Toolkit (GWT), which translates your core Java code into obfuscated, optimized JavaScript code. This can be interpreted by any WebGL browsers without the need for extra plugins.

This magical translation comes with a price. GWT does not support every single feature from the Java specification. In order to know whether a feature can be emulated or not, take a look at the official documentation at http://www.gwtproject.org/doc/latest/RefJreEmulation.html.

Another challenge starts once your code is able to be deployed into WebGL browsers—adapt it to the GWT build process.

Getting ready

Unlike the other recipes, we will not write any Java code. XML files will be the main characters here.

Within the /src/com/cookbook/samples folder of the GWT sample project, you will find the following key files:

  • GdxDefinition.gwt.xml
  • GdxDefinitionSuperdev.gwt.xml

The main difference between both of them is that the second allows developers to use a debugger and recompile the code quickly so that tests are performed in a more efficient environment. Consequently, this recipe will focus on the first among them.

The /src folder from core-samples contains the Samples.gwt.xml file.

How to do it…

Making your application/library compatible with GWT can be a tedious task. Even using third-party libraries would need some manual work. Let's dive into it.

Whenever you want to make use of a Java project whose code can be translated into JavaScript, it should include its corresponding module information. The resulting Samples.gwt.xml file for the samples projects is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd">
<module>
   <source path="aurelienribon/bodyeditor" />
   <source path="com/cookbook/animation" />
   <source path="com/cookbook/assetmanager" />
   <source path="com/cookbook/audio" />
   <source path="com/cookbook/box2d" />
   <source path="com/cookbook/inputmapping" />
   <source path="com/cookbook/scene2d" />
   <source path="com/cookbook/samples" />	
   <source path="com/cookbook/platforms" />
   <source path="com/cookbook/localization" />	
   <source path="com/cookbook/ai" />	
   <source path="com/cookbook/tween" />	
</module>

It includes each and every source package in order to inform GWT about the whole content of the module whose name is marked by the file's module without any extension. In this case, Samples.

Once your code is refactored into a module, you must remember to add your project dependencies (and include it!). This is explicitly written within the GdxDefinition.gwt.xml file, which looks like the following code:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE module PUBLIC "-//Google Inc.//DTD Google Web Toolkit trunk//EN" "http://google-web-toolkit.googlecode.com/svn/trunk/distro-source/core/src/gwt-module.dtd">
<module rename-to="html">
<inherits name='Samples' />	
<inherits name='com.badlogic.gdx.backends.gdx_backends_gwt' />
   <inherits name='aurelienribon.tweenengine'/>
   <inherits name='Box2DLights' />
   <inherits name='com.badlogic.gdx.ai' />
   <inherits name='com.esotericsoftware.spine' />
   <inherits name='com.badlogic.gdx.physics.box2d.box2d-gwt' />
   <inherits name='com.badlogic.gdx.controllers.controllers-gwt' />
   <entry-point class='com.cookbook.samples.client.GwtLauncher' />
   
   <set-configuration-property name="gdx.assetpath" value="../android/assets" />
</module>

It is important to highlight the inherits tag as this is the way to add dependencies to your GWT projects. Notice that the first one includes the previously created module while the rest refer to Libgdx itself and some other extensions or third-party libraries that are used within this book.

Note

Remember to look for the .gwt.xml file in order to know the name of the module.

As you can see, you must also specify, within the entry-point tag, the fully qualified class name that will launch your application.

Finally, as happens with the rest of the iOS and the desktop projects, they need to know that, by default, assets are within the Android project.

There's more…

It is hard to guarantee that your code will work because it depends on a lot of variables. However, adding the next option to your GWT compiler is a good practice to avoid going against all odds, so it stops as soon as an error is found:

-strict

You can also optimize the compilation by adding this:

-optimize 9

See also

The forthcoming recipes will make direct use of what you have just learned, so make sure you refer to the following recipes:

  • The Smooth animations with Universal Tween Engine recipe
  • The Dynamic 2D lighting with Box2DLights recipe
  • The Finite state machines and messaging recipe
  • The Component-based entity systems with Ashley recipe
  • The Skeletal animations with Spine recipe
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.174.253