void256/nifty-gui — pull request #146 by 3xp0n3nt (2013-09-25 15:20:33+00:00)
Title: Create LibGDX renderer and examples.

The main changes are the addition of two new Maven modules to Nifty:
nifty-renderer-libgdx and nifty-examples-libgdx. Changes to existing
modules are listed at the very bottom. All Nifty tests are passing, and
the two new modules are working perfectly after extensive integration
testing.

Important Note: Android / Nifty integration is a work in progress. The
LibGDX renderer seems to work on Android, but it has not been
extensively tested, especially because of the fact that the size of the
resources used in the Nifty Examples module are designed for desktops,
not mobile devices. The Android submodule of the nifty-examples-libgdx
module is broken at this time.

Important Note: iOS is completely unsupported at this time.

Detailed changes:

New Module 1: nifty-renderer-libgdx:

The new Nifty LibGDX renderer handles mouse & keyboard input, sound, music,
custom mouse cursors, graphics, pretty much anything Nifty can throw at
it ;-). It runs the main Nifty demo, the default controls demo, and the
Nifty 1.2 tutorial demo.

It uses the new BatchRenderBackend interface for rendering, but not yet
core profile. The renderer only uses OpenGL 1.0 functionality for
maximum compatibility. I have a very old computer, so even if I write a
core profile renderer, I would not be able to test it or run it :'(.

The LwjglBatchRenderBackend served as a reference; in fact, the renderer
doesn't really make use of any high-level features of LibGDX; the
original RenderDevice implementation of the LibGDX renderer written by
Martin Karing tried to take that route, but there
were too many issues, and I eventually removed it completely and focused
on porting the LwjglBatchRenderBackend instead. LibGDX does use LWJGL
for it's desktop implementation after all.

The real "magic" that was required to port LwjglBatchRenderBackend to
LibGDX involved using GL_TRIANGLES in the call to glDrawArrays. The
reason for this is that LibGDX only provides OpenGL ES functionality,
since it must work on Android and iOS, and GL_QUADS is not available in
OpenGL ES. Using GL_TRIANGLES (triangle list) requires the duplication
of two vertices per quad, since two joined triangles must be formed to
create a quad, and they share two vertices. This results in a total of 6
vertices per quad, but the performance should be made up for by the fact
that OpenGL LOVES triangles, and gobbles them up at a faster rate than
large, unwieldy quads, which it has to chew into triangles eventually
anyway, lol.

New Module 2: nifty-examples-libgdx:

This is a complete LibGDX project, set up Maven-style. It has a parent
module with pom packaging, and three sub-modules, core, desktop, and
android. The core submodule contains a LibGDX example application. The
desktop and android submodules merely instantiate a NiftyExample and
pass it to the LibGDX example application in the core submodule.

Three main NiftyExample demos are provided, and work just fine:
Nifty Main Examples Demo
Nifty Default Controls Demo
Nifty 1.2 Tutorial Demo

To run the above demos, go to the nifty-examples-libgdx directory and run:
mvn integration-test -Pdesktop

Or you can just do 'mvn clean install' and then double click the executable jar file in the nifty-examples-libgdx/desktop/target directory (nifty-examples-libgdx-desktop-1.4.0-SNAPSHOT-jar-with-dependencies.jar).

To run the second and third demos listed above, you'll need to change the mainClass property in nifty-examples-libgdx/desktop/pom.xml to either de.lessvoid.nifty.examples.libgdx.defaultcontrols.ControlsDemoMain for the Nifty Default Controls Demo, or de.lessvoid.nifty.examples.libgdx.tutorial.TutorialDemoMain for the Nifty 1.2 Tutorial Demo. After changing the mainClass property, go to the nifty-examples-libgdx directory and run:
mvn clean install -Pdesktop

The example application was set up in IntelliJ with Maven using the
LibGDX Maven archetype project, located at:
https://github.com/libgdx/libgdx-maven-archetype (There are a lot of great details in the README!)

Changes to existing modules / files in Nifty:

pom.xml:
Add nifty-renderer-libgdx and nifty-examples-libgdx as modules.

nifty-examples/src/main/java/de/lessvoid/nifty/examples/controls/ControlsDemoStartScreen.java:
Add "LibGDX" as a dropdown choice in the controls demo.

nifty-examples/src/main/resources/all/intro.xml:
nifty-examples/src/main/resources/all/libgdx.png:
Add LibGDX mention & logo in main example credits.

nifty-core/src/main/java/de/lessvoid/nifty/input/keyboard/KeyboardInputEvent.java:
Create an empty default constructor because GdxKeyboardInputEvent, which
subclasses KeyboardInputEvent, makes use of a default constructor, and needs
to call super(). Also add a public setData method, to initialize the data for
the object, so it doesn't have to be done in the constructor. This is
necessary because GdxKeyboardInputEvent relies on a pool to obtain new
instances, and the data required by Nifty to initialize the KeyboardInputEvent
with the parameterized constructor is not yet available. These changes are
made by Martin Karing , who wrote most of the original
input system for the LibGDX renderer.