Java2D -> LWJGL conversion

Hey guys, ive seen alot of talk about how using something like LWJGL with its OpenGL abilities is blisteringly (word?) faster than using Java2D. So my question is how easy is it to switch from Java2D to LWJGL, all of my gfx are just gif files that are being drawn. Like are we looking at a few commands to display it or a whole lot of extra fluff ill need to learn?

Hi gamepro65,

If you haven’t done any OpenGL before then there’ll be a bit of a learning curve.

You probably wouldn’t be needed to use a large proportion of the API for drawing images, mostly lots of glDrawPixels calls a bit like

GL11.glRasterPos2i(xPosition, yPosition);
GL11.glDrawPixels(imageWidth, imageHeight, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, imageData)

where imageData is a ByteBuffer containing the image pixel data. You can use LWJGL’s devIL library to load image files into ByteBuffers. You can set things up to display in 2D a bit like this:

int width = Display.getDisplayMode().getWidth();
int height = Display.getDisplayMode().getHeight();

GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, width, 0, height, -1, 1);

You would also have to change your input code to use the LWJGL Keyboard and Mouse classes - you would no longer be dealing with AWT events.

If you want to swap to OpenGL purely for speed, it might not be worth the trouble unless your game really isn’t fast enough. Otherwise, I expect you would enjoy the process of learning OpenGL and LWJGL, and there are people on these boards who would be willing to advise if you have any trouble (like Riven, who has been helping me with a couple of things). Cas is probably the local expert in using LWJGL for 2D games - have you seen Titan Attacks?

Firstly, don’t use glDrawPixels, as it’s a horribly inefficient and slow way of rendering. It’ll also cause all sorts of problems with clipping and culling later on.

The ‘proper’ way to do sprites in GL is by loading your image data into textures, then using a textured quad for each sprite. Kev’s space invaders tutorial starts with a Java2d version and converts it to LWJGL, that should show you everything you need to get started.

OT, I didn’t know that about glDrawPixels, that’s a handy tip. I use glDrawPixels to render text; if I find it too slow, I’ll do a straight-forward optimization by swapping to textured quads.

(I’m hoping that glScissor will do clipping of glDrawPixels rendering.)