James Williams
LinkedInMastodonGithub

Using LWJGL with Griffon

Tags: Griffon

LWJGL (Light-weight Java Game Library) is a game library that is the basis for many graphics libraries including jMonkeyEngine. You might be wondering why I'm covering LWJGL in a separate post when it is used in jMonkeyEngine which I covered several weeks ago. Though there is some overlap, each fits a specific need.

I'd describe the relationship like this: OpenGL/JOGL is a bicycle, LWJGL is a Vespa, and jME is a Harley. OpenGL is great if you want to touch the bare metal, LWJGL if you want to tweak and customize, jME if you singular goal is to make a game.

Getting started

  • Download LWJGL and drop the lwjgl.jar in your lib directory.
  • Find the native files for your operating system and extract them into lib/native
  • Download jME 2.0 Distribution. We aren't doing that much advanced stuff so we only need jme.jar, jinput.jar, and jme-awt.jar. Drop these into your lib directory.
  • Modify your Config.groovy file to contain the following:

    griffon {
         app {
           javaOpts = ["-Djava.library.path=${basedir}/lib/native"]
         }
    }
    

Linking our model and view

Our model and view are going to be fairly basic and initiate a model property called canvas that is added to the view. We don't need to do anything in our controller.

SimpleLWJGLModel.groovy

import groovy.beans.Bindable

class SimpleLWJGLModel {
     @Bindable canvas = new SimpleCanvas()
}

SimpleLWJGLView.groovy

application(title:'SimpleLWJGL',
   size:[300,300],
   resizable:false,
   locationByPlatform:true,
   iconImage: imageIcon('/griffon-icon-48x48.png').image,
   iconImages: [imageIcon('/griffon-icon-48x48.png').image,
                             imageIcon('/griffon-icon-32x32.png').image,
                             imageIcon('/griffon-icon-16x16.png').image]
) {
       widget(model.canvas)
}

Drawing on our canvas

Here we draw a rotating quad.

import org.lwjgl.LWJGLException
import org.lwjgl.opengl.AWTGLCanvas
import org.lwjgl.opengl.Display
import org.lwjgl.opengl.GL11

public class SimpleCanvas extends AWTGLCanvas {

    float angle = 0

    public SimpleCanvas() throws LWJGLException {
      // Launch a thread to repaint the canvas 60 fps
      Thread.start{
          while (true) {
            if (isVisible()) {
              repaint()
            }
            Display.sync(60)
          }
      }
    }

    public void paintGL() {
      GL11.with {
          glClear(GL_COLOR_BUFFER_BIT)
          glMatrixMode(GL_PROJECTION_MATRIX)
          glLoadIdentity()
          glOrtho(0, 640, 0, 480, 1, -1)
          glMatrixMode(GL_MODELVIEW_MATRIX)

          glPushMatrix()
          glTranslatef(320, 240, 0.0f)
          glRotatef(angle, 0, 0, 1.0f)
          glBegin(GL_QUADS)
          glVertex2i(-50, -50)
          glVertex2i(50, -50)
          glVertex2i(50, 50)
          glVertex2i(-50, 50)
          glEnd()
          glPopMatrix()
      }
      angle += 1

      try {
        swapBuffers()
      } catch (Exception e) { }
    }
}

One thing you might notice is that LWJGL requires a lot less setup than jME. Download the source here.