Created:

Last Changed:

The Game Loop

Preamble

The foundation of every game and game engine is the game loop. It is responsible for scheduling the game logic updates usually referred to as „ticks“ and the rendering of frames. If the game loop is not carefully implemented it will impact performance and stability of the game engine and later the games.

Basic Lifecycle Phases

Before designing a game loop it is important to know the different lifecycle phases, their time of execution and their duration.

Some phases can be divived into subphases usually referred to as „Early Phase“ or „Pre Phase“, „Phase“ and „Late Phast“ or „Post Phase“. In this article I will use the therms „Pre“ and „Post“.

The order of the subphases is always first „Pre Phase“, then „Phase“ and at last „Post Phase“. This seperates preparation for a phase, the main actions of the phase and actions that need to be executed at the end of the phase.

Engine Setup

The „Engine Setup“ phase is the first ever phase and directly starts with the entry point. In this phase the most fundamental engine objects like the game engine instance with the game loop and the game instance are created.

Initialization (Init)

The „Init“ phase is the first „active“ phase of a game engine and executes only once directly after the engine setup.

The phase can and should be devided into three sub phases.

Pre-Init

During the „Init“ phase the core of the game engine and the game gets initialized. For example in this phase the resource manager and the renderer-instances are created. It also loads the settings for the game, registers user input actions like mouse or keyboard buttons. It also loads the resources for the loading screen and the main menu.

Init

Post-Init

Update (Tick)

Pre-Tick

Tick

Post-Tick

Render

Pre-Render

Render

Post-Render

Cleanup (Destroy)

Framerate Messurement

„Frames per Second“ (FPS)

Most commonly the messurement for framerates is „Frames per Second“ (FPS). This is the average amount of displayed frames in one Second. However this is a rather meaningless messurement as this does not take the ticks into account, that take place in the same second. It is not distinguishable if a low FPS value means that the rendering takes too long and therefore only a small amount of frame can be rendered and displayed within one second or if the ticks are taking to long.

„Frame Duration“ (FD)

Another approach is to messure the „Frame Duration“. That is the average time a frame takes to be prepared and/or to be displayed. This gives a good hint about bottlenecks in the rendering code.

„Frames per Tick“ (FPT)

The last approach is similar to „Frames per Second“ I would like to call it „Frames per Tick“. As the name suggests, it messures not the amount of displayed frames per second but the average amount of displayed frames per tick.

Common Loops

Simple Loop

The most basic game loop is the 1:1 Loop. This refers to the fact that for each tick one frame will be rendered and displayed. This loop is mostly useless except for quick and dirty tests that don’t require any reasonable scheduling. It can be used for example to test out new rendering code.

  • Java
public void run()
{
	init();

	while(this.isRunning)
	{
		update();

		render();
	}

	destroy();
}

Fixed Timestep

  • Java
public void run()
{
	long lastTime = System.nanoTime();
	long currentTime = 0;
	long passedTime = 0;
	double unprocessedTime = 0;
	
	int tps = 20;
	
	double SECOND = 1000000000.0;
	double tickTime = 1.0 / tps;
	
	while(isRunning)
	{
		currentTime = System.nanoTime();
		passedTime = currentTime - lastTime;
		lastTime = currentTime;
		
		unprocessedTime += passedTime / SECOND;
		
		while(unprocessedTime >= tickTime)
		{
			tick(tickTime);
			
			unprocessedTime -= tickTime;	
		}
		
		double partialTickTime = unprocessedTime / tickTime;

		render(partialTickTime);
	}
}

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Content