Tag Archives: main menu

LWJGL Game States

By the end of this episode you will have learned how to implement LWJGL game states.

Why and when are the LWJGL game states used?

To accomplish things like the Skyrim launcher, the main menu in Counter-Strike, or the cinematic when launching Diablo III, the developers use different game states.

Initializing the LWJGL game states

To represent the current game state, an enumerator is used. There are three states: ‘INTRO’, ‘MAIN_MENU’, and ‘GAME’. The code for the enumerator looks like this.

public enum State {

The program has one static instance of the type State, set by default to the ‘INTRO’ state.

private static State state = State.INTRO;

Rendering the LWJGL game states

To switch over all the possible states, a switch statement is used. Symbolically a colour is assigned to every state. A new method called ‘glRectf(…)’ is also used. This method draws a rectangle with the first and last two parameters being the corners.

switch (state) {
case INTRO:
	glColor3f(1.0f, 0.0f, 0.0f);
	glRectf(0, 0, 640, 480);
case GAME:
	glColor3f(0.0f, 1.0f, 0.0f);
	glRectf(0, 0, 640, 480);
	glColor3f(0.0f, 0.0f, 1.0f);
	glRectf(0, 0, 640, 480);

The coloured rectangles (quadrilaterals) are just placeholders for things like a Game Menu, an Intro, and a Game Over screen. Post creative and original ideas in the comments.

Input handling

To switch between the different game states, the input is retrieved from the event queue. Insert this code in the game loop.

while (Keyboard.next()) {
	if(Keyboard.isKeyDown(Keyboard.KEY_SPACE)) {
		if(state == State.INTRO)
			state = State.MAIN_MENU;
		else if(state == State.MAIN_MENU)
			state = State.GAME;
		else if(state == State.GAME)
			state = State.INTRO;


This concludes the tutorial for game states. As always, should you have any questions or remarks, put them in the comments section below.


This article was written with the help of Jakob Stein Sturlunson.