Azureyjt 3ccc9baa1a Game Loop Pattern (#1083)
* Add game loop module

* Add game loop module

* Fix merge issue

* Implement game loop module

* Implement game loop module

* Implement time based game loop

* implement VariableStepGameLoop

* Implement FixedStepGameLoop

* Add UT

* Add Unit tests

* Fix checkstyle issues

* Add README.md

* Fix code review issues

* Fix code review issues

* update README.md
2019-11-16 14:40:23 +02:00
..
2019-11-16 14:40:23 +02:00
2019-11-16 14:40:23 +02:00
2019-11-16 14:40:23 +02:00


layout: pattern
title: Game Loop folder: game-loop
permalink: /patterns/game-loop/
categories: Other
tags:

  • Java
  • Difficulty-Beginner

Intent

A game loop runs continuously during gameplay. Each turn of the loop, it processes user input without blocking, updates the game state, and renders the game. It tracks the passage of time to control the rate of gameplay.

This pattern decouple the progression of game time from user input and processor speed.

Applicability

This pattern is used in every game engine.

Explanation

Game loop is the main process of all the game rendering threads. It drives input process, internal status update, rendering, AI and all the other processes.

There are a lot of implementations of game loop:

  • Frame-based game loop

Frame-based game loop is the easiest implementation. The loop always keeps spinning for the following three processes: processInput, update and render. The problem with it is you have no control over how fast the game runs. On a fast machine, that loop will spin so fast users wont be able to see whats going on. On a slow machine, the game will crawl. If you have a part of the game thats content-heavy or does more AI or physics, the game will actually play slower there.

  • Variable-step game loop

The variable-step game loop chooses a time step to advance based on how much real time passed since the last frame. The longer the frame takes, the bigger steps the game takes. It always keeps up with real time because it will take bigger and bigger steps to get there.

  • Fixed-step game loop

For fixed-step game loop, a certain amount of real time has elapsed since the last turn of the game loop. This is how much game time need to be simulated for the games “now” to catch up with the players.

Credits