Tag Archives: framebuffer

LWJGL Window

By the end of this tutorial you will have learned how to create, update, and destroy a display compatible with the Lightweight Java Game Library.

Everything is static

Unlike in other Java windowing libraries, the LWJGL handles the window statically. Instead of creating an instance of the hypothetical Display class, static methods are invoked.

Importing classes

Note that Display refers to org.lwjgl.opengl.Display and not the other Display class. Also DisplayMode refers to org.lwjgl.opengl.DisplayMode. The methods that are used in this tutorials are listed below.

Display.setDisplayMode(DisplayMode);
// Sets the display mode (width,height) of the window.
Display.setTitle(String);
// Sets the window title to the specified String.
Display.create();
// Creates a display with the specified display mode and title.
Display.isCloseRequested();
// Returns if the user attempted to close the display.
Display.update();
// Updates the contents of the display and checks for input.
Display.sync(int);
// Caps the frame-rate of the display to the specified amount of frames-per-second.
Display.destroy();
// Frees all the resources allocated for the display and destroys it.

Creating the LWJGL window

Since the method “Display.create()” throws an “LWJGLException”, the try-catch clause will resemble the following.

try {
	Display.setDisplayMode(new DisplayMode(640, 480));
	Display.setTitle("Episode 2 - A fresh display!");
	Display.create();
} catch (LWJGLException e) {
	e.printStackTrace();
	Display.destroy();
	System.exit(1);
}

Updating the window in the game loop

The game loop is a continuous loop in which input handling, game logic, and resource management is done. The loop will be broken if the user requests the display to be closed.

while (!Display.isCloseRequested()) {
	// While no attempt to close the display is made..
	// Put render code here.
	// Put input handling code here.
	Display.update();
	// Refresh the display and poll input.
	Display.sync(60);
	// Wait until 16.67 milliseconds have passed. (Maintain 60 frames-per-second)
}

Destroying the LWJGL window

After the game loop has been broken, the resources have to be cleaned up. In this case of this program, the only thing that needs to be cleaned up in the display itself.

Display.destroy();

For some inexplicable reason the window does not immediately close when the user clicks on the close button – it lingers for several seconds. Fixing this consists of placing the following line after “Display.destroy();”.

System.exit(0);

This quits the Java Virtual Machine. Keep in mind that any code written after this will not be executed.

Entire source code

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.LWJGLException;
 
public class YourFirstDisplay {
	public static void main(String[] args) {
		try {
			Display.setDisplayMode(new DisplayMode(640, 480));
			Display.setTitle("A fresh display!");
			Display.create();
		} catch (LWJGLException e) {
			e.printStackTrace();
			Display.destroy();
			System.exit(1);
		}
		while(!Display.isCloseRequested()) {
			Display.update();
			Display.sync(60);
		}
		Display.destroy();
		System.exit(0);
	}
}

Conclusion

That concludes this tutorial. If you have any questions please leave a comment below.