docs: improve game loop

This commit is contained in:
Ilkka Seppälä committed 2024-04-22 20:42:57 +03:00
1 parent d6133e3bb0
commit b55e85f8de
2 files changed
+50 -30

No files matched your search

+45 -22
View File
@@ -1,44 +1,40 @@
--- ---
title: Game Loop title: Game Loop
category: Behavioral category: Behavioral
language: en language: en
tag: tag:
- Game programming - Concurrency
- Event-driven
- Game programming
- Performance
--- ---
## Also known as
* Game Cycle
* Main Game Loop
## Intent ## 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 decouples progression of game time from user input and processor speed. The Game Loop design pattern aims to facilitate the continuous execution of a game, where each loop cycle processes input, updates game state, and renders the game state to the screen, maintaining a smooth and interactive gaming experience.
## Applicability
This pattern is used in every game engine.
## Explanation ## Explanation
Real world example Real world example
> Game loop is the main process of all the game rendering threads. It's present in all modern games. > 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.
> It drives input process, internal status update, rendering, AI and all the other processes.
In plain words In plain words
> Game Loop pattern ensures that game time progresses in equal speed in all different hardware > Game Loop pattern ensures that game time progresses in equal speed in all different hardware setups.
> setups.
Wikipedia says Wikipedia says
> The central component of any game, from a programming standpoint, is the game loop. The game loop > 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.
> allows the game to run smoothly regardless of a user's input, or lack thereof.
**Programmatic Example** **Programmatic Example**
Let's start with something simple. Here's `Bullet` class. Bullets will move in our game. For Let's start with something simple. Here's `Bullet` class. Bullets will move in our game. For demonstration purposes it's enough that it has 1-dimensional position.
demonstration purposes it's enough that it has 1-dimensional position.
```java ```java
public class Bullet { public class Bullet {
@@ -81,8 +77,7 @@ public class GameController {
} }
``` ```
Now we introduce the game loop. Or actually in this demo we have 3 different game loops. Let's see Now we introduce the game loop. Or actually in this demo we have 3 different game loops. Let's see the base class `GameLoop` first.
the base class `GameLoop` first.
```java ```java
public enum GameStatus { public enum GameStatus {
@@ -245,8 +240,36 @@ Stop variable-step game loop.
![alt text](./etc/game-loop.urm.png "Game Loop pattern class diagram") ![alt text](./etc/game-loop.urm.png "Game Loop pattern class diagram")
## Applicability
The Game Loop pattern is applicable in real-time simulation and gaming where the state needs to be updated continuously and consistently in response to user inputs and other events.
## Known Uses
* Video games, both 2D and 3D, across various platforms.
* Real-time simulations that require a steady frame rate for updating logic and rendering.
## Consequences
Benefits:
* Ensures the game progresses smoothly and deterministically.
* Facilitates synchronization between the game state, user input, and screen rendering.
* Provides a clear structure for the game developers to manage game dynamics and timing.
Trade-offs:
* Can lead to performance issues if the loop is not well-managed, especially in resource-intensive updates or rendering.
* Difficulty in managing varying frame rates across different hardware.
## Related Patterns
* [State](https://java-design-patterns.com/patterns/state/): Often used within a game loop to manage different states of the game (e.g., menu, playing, paused). The relationship lies in managing the state-specific behavior and transitions smoothly within the game loop.
* [Observer](https://java-design-patterns.com/patterns/observer/): Useful in a game loop for event handling, where game entities can subscribe to and react to events (e.g., collision, scoring).
## Credits ## Credits
* [Game Programming Patterns - Game Loop](http://gameprogrammingpatterns.com/game-loop.html) * [Game Programming Patterns - Game Loop](http://gameprogrammingpatterns.com/game-loop.html)
* [Game Programming Patterns](https://www.amazon.com/gp/product/0990582906/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0990582906&linkId=1289749a703b3fe0e24cd8d604d7c40b) * [Game Programming Patterns](https://www.amazon.com/gp/product/0990582906/ref=as_li_qf_asin_il_tl?ie=UTF8&tag=javadesignpat-20&creative=9325&linkCode=as2&creativeASIN=0990582906&linkId=1289749a703b3fe0e24cd8d604d7c40b)
* [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) * [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)
* [Real-Time Collision Detection](https://amzn.to/3W9Jj8T)
@@ -24,22 +24,19 @@
*/ */
package com.iluwatar.gameloop; package com.iluwatar.gameloop;
import lombok.Getter;
import lombok.Setter;
/** /**
* Bullet object class. * Bullet object class.
*/ */
public class Bullet { public class Bullet {
@Getter
@Setter
private float position; private float position;
public Bullet() { public Bullet() {
position = 0.0f; position = 0.0f;
} }
public float getPosition() {
return position;
}
public void setPosition(float position) {
this.position = position;
}
} }