LWJGL Basics 1 (The Display)

From LWJGL
Jump to: navigation, search

Contents

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.


Example

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.

Credit

Tutorial Credit - Ninja Cave

Personal tools
Namespaces

Variants
Actions
Navigation
Tools