LWJGL Basics 1 (The Display)

Introduction
The LWJGL library uses its own lightweight native window (known as the Display) and has its own input system. This provides a fast, small context to draw on, enables low latency input and is a solid base for building modern games.

The LWJGL Display
The Display class is LWJGL's most important class. It creates and controls the native window on which all graphics content is rendered.

The Display class has 3 methods that one should be aware of, namely:

  • create()
  • update()
  • destroy()

Display.create()
This method will create the native Display window. In order to set the size of the Display its DisplayMode must be set. For simplicity we will not cover fullscreen mode in this tutorial. The DisplayMode is set before calling Display.create() and done by specifying the width and height of the Display as follows:

Display.setDisplayMode(new DisplayMode(width,height));
Display.create();

This will create the native Display of the desired size. OpenGL methods can now be called to start drawing on the Display.

In order to keep the application running a basic loop will be needed to update the contents of the Display.

Display.update()
LWJGL uses double buffering and everything will be drawn to an offscreen buffer. When Display.update() is called it will swap the buffers and make what has been drawn visible. Normally this method is called once every frame inside the rendering loop. This method will also poll the Keyboard and Mouse.

Display.destroy()
This method will destroy the native Display (i.e. close it) and clean up any resources used by it.

The Display.isCloseRequested() method will return true when the close button on the native window is clicked. This will be used in the example below to exit our rendering loop.

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
 
public class DisplayExample {
 
    public void start() {
        try {
	    Display.setDisplayMode(new DisplayMode(800,600));
	    Display.create();
	} catch (LWJGLException e) {
	    e.printStackTrace();
	    System.exit(0);
	}
 
	// init OpenGL here
 
	while (!Display.isCloseRequested()) {
 
	    // render OpenGL here
 
	    Display.update();
	}
 
	Display.destroy();
    }
 
    public static void main(String[] argv) {
        DisplayExample displayExample = new DisplayExample();
	displayExample.start();
    }
}

The full LWJGL Display API can be found on its Javadoc page here.

Comments

When I put this code into Eclipse and run it, I get the error. I copied and pasted even and still got the error. I am running LWJGL 2.9.1.
Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1860)
at java.lang.Runtime.loadLibrary0(Runtime.java:845)
at java.lang.System.loadLibrary(System.java:1084)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:95)
at org.lwjgl.Sys.(Sys.java:112)
at org.lwjgl.opengl.Display.(Display.java:135)
at part01.DisplayExample.start(DisplayExample.java:13)
at part01.DisplayExample.main(DisplayExample.java:34)

Anonymous Ninja
Wed, 01/08/2014 - 21:52

Did you add natives?
If you didnt its just to right-click your prodject > properties > Java Build Path > Libraries and expand lwjgl, select Native library location and click edit on the right.
Select the path to lwjgl natives (C:/LWJGL/native/windows)
if you use lwjgl_util too its the same thing.

Anonymous Ninja
Fri, 03/28/2014 - 15:53

here is a picture of what I have right now. The error reads expected class or object definition.

http://imgur.com/gTYMx5f

Anonymous Ninja
Thu, 12/19/2013 - 05:31

I forgot, now I'm searching to find the Mac equivalent of Task Manager.

Anonymous Ninja
Mon, 11/26/2012 - 02:54

"Could not find the main class: DisplayExample. Program will now exit."

I keep getting that error every time I try to run it... I'll copy and paste my code but it's not any much different than yours. Do you have any idea what the problem is? Thanks.

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
 
public class DisplayExample {
 
	public static void main(String[] args) {
			DisplayExample displayExample = new DisplayExample();
		displayExample.start();
		}
	public void start() {
 
		try {
		Display.setDisplayMode(new DisplayMode(800,600));
		Display.create();
 
		} catch (LWJGLException e) {
			e.printStackTrace();
			System.exit(0);
 
		}
 
 
		while (!Display.isCloseRequested()) {
 
			Display.update();
		}
 
		Display.destroy();
 
	}
}

Anonymous Ninja
Tue, 01/10/2012 - 04:21

I found the answer; one of the folders in the path had a semicolon, so it was separating the thing into two different paths. <___< Renaming it and replacing the lwjgl.jar fixed it.

Anonymous Ninja
Mon, 01/16/2012 - 22:00

I copied and pasted this "Display Example" example into my Eclipse project after configuring it as you said in your earlier tutorial. When I run the program as a java application, I get what looks like tv static, and the LWJGL icon in the top left corner is orange and obscured. How do I get rid of the static, or is it supposed to be there?

Anonymous Ninja
Sat, 09/10/2011 - 22:41

odd indeed, its not something that is normal behaviour and sounds like a driver problem. Do update your graphics drivers and see if that makes any difference.

admin
Sat, 09/10/2011 - 23:30

I don't know if that makes any difference, but tutorials 3 and 4 work perfectly. I guess on my computer, I need to state what gets put on the canvas or else it'll print anything(hence, the tv static). Since tuts 3 and 4 have an object on the screen, it works correctly(does that make sense?)

Oh well, asides from the first 2 tuts, the window works perfectly, so, I'm not complaining anymore.

Anonymous Ninja
Sun, 09/11/2011 - 12:10

I'm betting the difference you're seeing is due to the glClear() call. It depends on the driver, but if you don't clear the buffer first (ie, glClear(GL_COLOR_BUFFER_BIT) ), the buffer will be full of random data. I think OSX GL drivers usually clear the buffer (or the memory) while Windows drivers don't (it's technically unnecessary assuming you render over the whole buffer). If you've ever seen a "hall of mirrors effect" in a game, that's actually related to this.

Anonymous Ninja
Tue, 11/15/2011 - 19:06

Exception in thread "main" java.lang.UnsatisfiedLinkError: no lwjgl in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1045)
at org.lwjgl.Sys$1.run(Sys.java:73)
at java.security.AccessController.doPrivileged(Native Method)
at org.lwjgl.Sys.doLoadLibrary(Sys.java:66)
at org.lwjgl.Sys.loadLibrary(Sys.java:95)
at org.lwjgl.Sys.(Sys.java:112)
at org.lwjgl.opengl.Display.(Display.java:135)
at lwjgl.start(lwjgl.java:14)
at lwjgl.main(lwjgl.java:9)

BTW I used ALL of the jars as "extenal jars"

Anonymous Ninja
Wed, 08/21/2013 - 03:17

You need to set up a natives folder, and link the build path to your OS.

Anonymous Ninja
Sat, 10/19/2013 - 07:56