Featured Post

Titan Attacks is DONE!

Yup, that’s right – finally in a fit enough state to submit to playtesting over at the XBLIG forum. If I remember correctly I think I started this chunk of work around Nov 2012 but that entailed converting LWJGLApplets first before I could begin on the Titan code. Am I happy with it after all the grief and agro of the past 6 months or so? Pretty much as a project goes I guess. The game itself is near identical to the Java / Desktop original – you’d have to run them side by side to have any chance of spotting the slight differences and even then I reckon you wouldn’t really tell. So game wise, yeah, all is good. The CRT shader is rewritten from scratch – the converted code worked perfectly on Windows but wouldn’t distort correctly on the console – I spent some time trying to fix it, checking texture and render target formats were correct and everything seemed ok, but no, broken. That annoyed me a little Object pools – boo! I broke the game a few times implementing poolable entities but it was a case of do it or be a slave to the GC every 5 seconds or so – *yikes*. I had to trim some of the particle effects on later levels as the Xbox really struggled with > 1000 sprites. Again, you can’t really notice any difference. And a new collision detection system was required. The code-base is hacked more than I would have liked in an ideal world. Public fields sprouted up EVERYWHERE along with the pool changes mentioned above, it just makes things a little nasty. A player is never going to know or care but it’s just a little grating for me the developer Storage had a rewrite at the eleventh hour – I utilised the excellent SharpFileSystem to get me out of a widening hole. And I used a nice thread pool (by KiloWatt who I think is jwatte over in XBLIG land) to alleviate IO stalls. There’s the very occasional stutter caused by the JIT code generator which I can just about live with in my old age. Garbage is tamed mostly – I still generate some but not enough to cause problems during gameplay and then I can collect in the Shop so no glitches in game unless you played a level for about 10 minutes or so (most levels are over within a couple of minutes) So I’m done – unless playtesting throws up anything nasty, I can submit to release in the next couple of weeks. And my reward for finally getting it done? Feet up, chilling with friends? A beer or two? Watch a movie? Nope, I downloaded the latest  Ultratron source changes…..  

Read More

What the dickens is a LWJGL?

Posted by Paul | Posted in Code, Droid Assault, Puppy Games, Ultratron, Uncategorized, XBLIG, XNA | Posted on 30-11-2012


I’ve been Tweeting quite a bit recently about the various libraries and frameworks I’m converting from Java to C# and I’m aware the posts are probably pretty meaningless to all but a few select developer chums.  So, in an attempt to enlighten the rest of the Pumpkin followers, here’s the low-down on the main players involved.

LWJGL – The Lightweight Java Game Library is an open source Java software library for computer game developers. It exposes common libraries like OpenGL, OpenAL, etc in a speedy, lightweight manner to the Java crowd. Cas Prince (@puppygames) was the original author. It powers all the Puppy games, Project Zomboid and Minecraft to name a few.

I’ve converted the graphical primitives like Color, Rectangle, Vector (and whatever interfaces they implement) as is – they are reference types and take precedent over the XNA equivalents. The input classes; Mouse, Controller and Keyboard are done too and they just delegate to their XNA namesakes.

SPGL – The Shaven Puppy Game Library, is a layer that sits on top of LWJGL. It’s got a sprite engine, sound players, resources live here (fonts, paletes, etc) a bunch of animation commands and some OpenGL and OpenAL stuff. There’s some nice ‘rich text’ classes GLStyledText, GLTextArea for use by the GUI.

It also contains the resource loader / converter which takes a bunch of XML and creates fully working (Java/C#) objects from definitions in the mark up:


The following XML snippet would create a GLTexture resource and an ImageBank resource at run time (or build time if you prefer)

<texture name="solid_background.texture" url="solid_background" dst="GL_RGBA" 
target="GL_TEXTURE_2D" min="GL_LINEAR" mag="GL_LINEAR" wrap="false" />

<imagebank name="background.imagebank" texture="background.texture">
  <spriteimage name="spriteimage.bg_bottom_left" x="0" y="0" w="10" h="12" hx="0" hy="0" />
  <spriteimage name="spriteimage.bg_bottom" x="10" y="0" w="42" h="12" hx="0" hy="0" />

Cool eh?! The similarities between Java and C# made this a breeze really which was a godsend as this is probably the core task when converting the Puppy stuff.

The sprite render uses XNA’s DrawIndexedPrimitives with dynamic Vertex and Index buffers. The Java renderer is a big array of floats so I created a corresponding VertexDeclaration and just blat whatever the sprite engine throws at it. Cas removed all (*most?*) of the immediate mode glVertexX OpenGL calls so there’s all sort of goodness going on here, with batching and buffers galore as sprites and arbitrary geometry all live happily together. There were a few glTranslates, glPush/PopMatrix calls left so I just created a GL wrapper object and mimicked the OpenGL matrix stacks (e.g glPushMatrix() becomes GL.PushMatrix()). The AndroidSPGL library I converted last year used OpenTK on the C# side but my new GL wrapper keeps the call parameters the same data types as OpenGL (public const int GL_TEXTURE_2D instead of All.Texture2D type enums)

LWJGLApplets - Next in the chain, this is the GUI and effects layer and is where the particle emitters live and all the widgets you’d need for a user interface. It contains a stack of Screens so you can navigate through the game; Title Screen, Options, High Scores, all the usual basic stuff. Games can subclass these screens to add features and obviously define brand new screens.

Java – I decided to convert bits ‘n’ bobs to aid the conversion process. So when SPGL needs a ByteBuffer I just use my C# implementation of it instead of changing the SPGL source code. Most of java.nio is done and some other misc stuff like Integer.parseInt but I stopped half way through java.io and now I just use .Net collections instead – with added extension methods if required to match the Java behavious (e.g. HashMap.put(xxx) -> Dictionary.put(xxx) extension method). Extension methods help massively in getting to that glorious ‘Build Succeeded’ message in Visual Studio but if after profiling all the wrapping hurts performance then the source code is changed to a native class / call.

The Java Conversion Monkey – this is an automated conversion Windows Forms tool I knocked up to aid the entire process. You point it at a folder containing Java files, give it a destination folder and out pops a corresponding .cs file with beautiful C# code.

It’s a pretty brute force approach; each file has a series of FileProcessors perform conversion work on it in order – I’ve got NamespaceProcessor, FindAndReplaceProcessor, RegExProcessor, plus about 5 or 6 more that I forget now. It helps greatly in the process but there’s still a fair bit of manual fix ups to be done. A special mention here to Resharper too as it flags all the code errors like ‘you need override here’ or ‘this methods hide blah in the base class’.

So what are the main problems converting a bunch of Java libs to C#? Sound normally gets left until last and I usually do a few iterations until I’m happy with it. Stupid Java interfaces with their fields definitions – anonymous classes, blergh. I tend to create a concrete implementation here and add some delegates. But the worst annoyance by far is Binary Serialization. Simple to convert in theory but it’s a major PITA (and is something I will be leaving until the very end). Compact framework doesn’t have a binary serializer,  can’t reflect over private fields on the Phone. Once you add some custom serialization, the built-in mechanism gives up and it’s custom all the way. Java uses an Interface to tag classes as serializable, .Net a [Serializable] Attribute. However, I still need the Java Serializable interface as some of the SPGL functions require a serializable object. But then I need to implement .Net’s ISerializable too. This is what I currently end up with..

public class SomeClass : Whatever, Java.io.Serializable, System.Runtime.Serialization.ISerializable {

hmm, it all gets a little bit messy and confusing :-)

That’s about the bulk of it. The ultimate goal is to convert Puppy Games’ titles to Xbox, iOS, PS Vita, Window 8 Phone, Surface with the minimum amount of fuss and  impact on the actual game code.

Java – done. LWJGL – done. SPGL – done (And the Android version). LWJGLApplets – in testing.

Titan Attacks – COMING SOON!


Write a comment

You must be logged in to post a comment.