Skip to content

DisplayManager

ivanorsolic edited this page Jan 21, 2018 · 7 revisions

The LWJGL 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. 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 can also be used to exit the rendering loop.

Sleeping and Display.sync()

Sleeping is very important in games and is needed otherwise the game loop will run as fast as possible, taking up all the system resources and in most cases heating CPU's, GPU's and draining batteries. Sleeping will allow the application to sleep and thus not use all the resources on the computer. There are various loops that can be created using options like Thread.sleep() and Thread.yield(). However LWJGL provides the method Display.sync(int fps) to make this easier. This method allows the game loop to run at the specified frame rate (e.g 60fps) and sleep for any extra time. This method must be called every frame (i.e. put it in the game loop).

You can browse through the full LWJGL Display API here.

My code

Instance variables

I've used a couple of static variables to set the width and height of the display:

private static final int WIDTH = 1280;
private static final int HEIGHT = 720;

I also used a variable to set the FPS cap:

private static final int FPS_CAP = 120;

Methods

There are three methods used here.

public static void createDisplay()
public static void updateDisplay()
public static void closeDisplay()

The first method, createDisplay(), as mentioned above, opens up the display. Inside it we can find:

ContextAttribs attribs

which tell the program which version of OpenGL we are using, and set the Forward Compatibility and ProfileCore to true, and a try-catch block which

try {
	Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
	Display.create(new PixelFormat(), attribs);
    Display.setTitle("Nas prvi display! :)");

} catch (LWJGLException e) {
		e.printStackTrace();
}

Where setDisplayMode determines the size of the display, and Display.create actually creates it. Display.setTitle() does pretty much what it says, sets the title of the display.

After the try-catch block we can see:

GL11.glViewport(0, 0, WIDTH, HEIGHT);

which tells OpenGL where on the display it can render the game. The first two parameters x, y are set to 0 (to the bottom left of the display), and the second two parameters, WIDTH and HEIGHT set it at 1280x720 (the top right of the display).

The second method used, updateDisplay has only two statements:

Display.sync(FPS_CAP);
Display.update();

The Display.sync() method allows our game loop to run at the specified frame rate (which is 120 fps in our case, FPS_CAP=120).

THe Display.update() method updates the display with the new frame.

And the last method closeDisplay() contains only one statement:

Display.destroy();

which closes the display.

##For quick reference, here is the full code with some comments:

package renderEngine;

import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.ContextAttribs;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.PixelFormat;

//A Class for managing the display
public class DisplayManager {

	//Width and the height of the display
	private static final int WIDTH = 1280;
	private static final int HEIGHT = 720;
	//FPS cap at which we want the game to run at
	private static final int FPS_CAP = 120;
	
	//Method to open up the display at the beginning when we start the game
	public static void createDisplay(){
		
		//Version of the OpenGL we're using, enabling forward compatibility and ProfileCore
		//Just a couple of settings, not too important
		ContextAttribs attribs = new ContextAttribs(3,2).withForwardCompatible(true).withProfileCore(true);
		
		try {
			//Determining the size of the display
			Display.setDisplayMode(new DisplayMode(WIDTH,HEIGHT));
			//Actually creating the display
			Display.create(new PixelFormat(), attribs);
                    //Setting the display title to a custom string
                    Display.setTitle("Nas prvi display! :)");
		} catch (LWJGLException e) {
			e.printStackTrace();
		}
		//Telling OpenGL where on the display it can render the game
		//x,y=0 <-- choosing the whole display, bottom left and top right -->Width, Height
		GL11.glViewport(0, 0, WIDTH, HEIGHT);
	}
	
	//Method to update the display every frame
	public static void updateDisplay(){
		Display.sync(FPS_CAP);
		Display.update();
	}
	
	//Method to exit and close the display
	public static void closeDisplay(){
		Display.destroy();
		}
}

Clone this wiki locally