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.
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.
This pattern decouples 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.
Real world example
There are a lot of implementations of game loop:
> Game loop is the main process of all the game rendering threads. It's present in all modern games. It drives input process, internal status update, rendering, AI and all the other processes.
- Frame-based game loop
In plain words
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 won’t be able to see what’s going on. On a slow machine, the game will crawl. If you have a part of the game that’s content-heavy or does more AI or physics, the game will actually play slower there.
> Game Loop pattern ensures that game time progresses in equal speed in all different hardware setups.
- Variable-step game loop
Wikipedia says
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.
> The central component of any game, from a programming standpoint, is the game loop. The game loop allows the game to run smoothly regardless of a user's input or lack thereof.
- Fixed-step game loop
**Programmatic Example**
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 game’s “now” to catch up with the player’s.
Let's start with something simple. Here's a bullet that will move in our game. For demonstration it's enough that it has 1-dimensional position.
```java
publicclassBullet{
privatefloatposition;
publicBullet(){
position=0.0f;
}
publicfloatgetPosition(){
returnposition;
}
publicvoidsetPosition(floatposition){
this.position=position;
}
}
```
GameController is responsible for moving objects in the game. Including the aforementioned bullet.
```java
publicclassGameController{
protectedfinalBulletbullet;
publicGameController(){
bullet=newBullet();
}
publicvoidmoveBullet(floatoffset){
varcurrentPosition=bullet.getPosition();
bullet.setPosition(currentPosition+offset);
}
publicfloatgetBulletPosition(){
returnbullet.getPosition();
}
}
```
Now we introduce the game loop. Or actually in this demo we have 3 different game loops.
*[Game Engine Architecture, Third Edition](https://www.amazon.com/gp/product/1138035459/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=1138035459&linkId=94502746617211bc40e0ef49d29333ac)