Featured Post

Converting Java is Fun!

The same way I find running fun! It hurts a lot while I’m doing it and I’m glad when it’s over but there’s a certain satisfaction followed by a warm glow after. I first dabbled with the whole Java/C#/Puppy/Pumpkin conversion thing about 5 years ago now I think, just after the release of XNA Game Studio 1.0. Being a jobbing C# developer it sounded great to be able to use the same language to write cool games. I’d spent a short time with the fledgling game library and already converted my existing C++ engine to it and developed my first XNA game, Doppelganger. Around this time I’d been playing a fair bit of Droid Assault and thought how cool it would be to get it on the Xbox and after a bit of back ‘n’ forth with Cas, it was decided I’d convert the games as is – no new art and minimal design changes – cheap as chips basically. Even at this early stage we didn’t think XBLIG would be a massive success financially so we went for quick and dirty. So, there I was, eagerly hitting refresh on the Send/Receive email button in Outlook when the email from Cas arrived. Yay, I though, then “Ultratron source code” hit me. Now I liked Ultratron, but it was no Droid Assault, bah, I though! But Cas was right. It was a MUCH simpler game and would be MUCH easier to convert. It would be the perfect test case and hopefully we’d hit the launch of XBLIG too. Reading through the code it was obvious there were a few support libraries that the game used – namely, LWJGL, SPGL, LWJGLApplets. Hmm, that’s a fair bit of code there, I thought. And as I began trying to get a handle on the way it all fit together I got a bit despondent. And, converting the cool little game suddenly became a much larger project. But then I had an epiphany! I’d already got a game engine, written in C#, using XNA Game Studio, targeting the XBox. Let’s face it, Ultratron is a simple game – it’s Robotron with knobs on – what would we need? Entity system? Got one. Menus? Those too. Particles and effects? Yup, all in the Doppelganger engine. XML parsing – close enough. So I had a plan – I would extract all the data from the XML scripts, the entity behaviour from the Ultratron code, bolt my menu on, tweak the particles and add the XNA specific bits like Controllers and Profiles the the Xbox audience would expect. Conversion was rapid. My engine was pretty basic but it had all the guts required and the only thing I had to change was the collision detection as the Xbox CPU choked a bit on the exiting approach. Around this time I was preparing to emigrate to New Zealand so things were mad but I completed the conversion a few days after XBLIG went live. The game received some fairly good reviews (2nd I think in Retro Remakes early round up of the service) as well as some (valid) criticism – folks missed the Puppy menu system and some would have liked a super duper HD widescreen version. It sold shockingly however – I think we split a cheque for a couple hundred dollars early on and that was about it for it. But I’d got the bug and after tidying up a few things in a couple of updates (including new Menu) I was eager to get my hands on the Droid Assault code. Eventually, the email came through! “Titan Attacks source code”, bah * 2. BUT, it was mostly more of the same and again much simpler than Droid. I followed the same process as for Ultratron but this time things didn’t go as smooth. I struggled a bit with some of the code features of Java, I couldn’t get the sprites to draw in the correct locations (stupid hotspots), Titan was my least favourite of the Puppy games so enthusiasm dipped a bit and then my world turned upside down MASSIVELY and development stalled. Titan was pretty much dead in the water. It was maybe 85% done but I’d got zero time to dedicate to it and even less desire. But the life changing event didn’t last forever and eventually (maybe a year?) I had some time to kick things off again. And what do you know Microsoft go and announce a Dream Build Play competition and Cas is up for Droid Assault as our entry. The stars align and I’ve got the code mojo back so I begin the conversion… …it’s a disaster. The code just doesn’t fit my engine. I introduce too many bugs during the conversion and I’m gutted. I’d been waiting to convert this game for ages and then I go and bugger it up?  I wasn’t totally beaten yet though – I loved the game too much to give up and I wanted to give it a real go. So I went at SPGL and LWJGL with an XNA sized lump hammer. The code was rough and hacky and I had to change a lot of the syntax (but not as before, the behaviour) and blimey Charlie I’d got them into some sort of compilable assembly. This was probably about 2 years ago so details are a bit fuzzy and I’ve no idea how long it took but, there it was, a little droid, doing his little animation dance on the screen – he was upside down and in the wrong place but I didn’t care.  A sprite, on screen, using SPGL, LWJGLApplets and Droid Assault code. I finally fixed the sprite positioning bug and even wrote some Java utils that wrote the map data into a format I could use. And by some miracle, I got maybe 95% of it converted. There was some work to do with the controls but it ran and you could play it and transfer and shoot stuff and…..it sucked on the Xbox. Like 1 or 2 FPS sucked. “Garbage, that is”, I confidently predicted but it wasn’t. Method call overhead in the sprite engine maybe? – I made public fields EVERYWHERE. Better, but still not good enough. The first ten levels were playable but not much more – I posted some images on Twitter and even upload a YouTube vid but something was rotten in the core of it. Eventually I tracked it down to the enemy droid AI and the map renderer!! I came up with various changes to both systems but nothing worked well enough to get it performing acceptably. I submitted the 10 level demo to the comp but didn’t hear anything back from Microsoft – I showed it at GameCityNights in Nottingham and folks liked it but I wasn’t happy with. It needed MAJOR work, most probably a rewrite of the offending systems and I was totally burnt out. I spoke to Cas about a few ideas but Revenge of the Titans was nearing release and things were about to go mental there so Droid kind of got put on the back burner. At this point, the scoreline read success 1 – 2 failure. Boooo! After a period of recuperation of about 3 – 4 months I’d got the bug again – I found the  Titan Attacks source code snapshot, ran the new Java Conversion Monkey tool at it and had the lot converted in 3 days! 3 days, part time, the entire game. And it worked. The work that went into the Droid converison had finally paid off – I posted a tweet – and then BAM! Cas replies, “We’ve just given Titan a massive widescreen overhaul – with HD graphics” – none of it compatible with the engine stuff I’d done. “Never mind”, he says, “check out this Android version. What about doing that?”. So, I started again. With AndroidSPGL – And I did it all. Properly this time – no XNA hacks in there. I did bits of Java, LWJGL and all the resource loading tools. It was hard work, late nights, head scratching but I reckon about 2 months or so and I’d got it done. I did the test harness too and got it running in the iPad emulator – I did a Windows Phone 7 version for kicks – it all just worked now. Score most definitely 2-2 I thnk I just needed an AndroidLWJGLApplets now but it never came. Revenge of the Titans took off on Steam, rather well, in fact and Cas was swamped with that. And then life gets in the way for me again and I have to cope with that for about 4 months again. Things are a bit messy so I reassess: - Ultratron - done - Titan Attacks (pre-widescreen) – done – uses hacked XNA version of SPGL / LWGL - Titan Attacks (post-widescreen) – some done - uses hacked XNA version of SPGL / LWGL - Droid Assault – 95% done with crippling performance problems - uses hacked XNA version of SPGL / LWGL - AndroidSPGL - done (for iOS / Windows Phone 7) - AndroidTools – done (for iOS / Windows Phone 7) What was missing? – there’s desktop SPGL and desktop LWJGLApplets - hmm, I’d already done a bunch of SPGL work for Android and a bunch of ‘hacked’ LWJGLApplets work for DA and TA. So I set about doing the full desktop versions of the Puppy libs using MonoGame - no XNA style hacks – properly this time, like the Android version. And they are pretty much done… SPGL 100%, LWJGLApplets is being tested right now. Just need to do the glue layer that sticks a Puppy game to a MonoGame game and add sound back in. Then, I can get the latest Titan Attacks code (it’s had some updates recently) and run the conversion monkey over it! Phew, it’s been quite a ride!  

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.