Skip to content

Commit 6606d6c

Browse files
committed
Update README.md
1 parent f3fd498 commit 6606d6c

File tree

2 files changed

+84
-65
lines changed

2 files changed

+84
-65
lines changed

Diff for: game-loop/README.md

+82-63
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,39 @@ tags:
88
- Game programming
99
---
1010

11-
## Intent
12-
A game loop runs continuously during gameplay. Each turn of the loop, it processes user input without blocking, updates
13-
the game state, and renders the game. It tracks the passage of time to control the rate of gameplay.
11+
## Intent
12+
13+
A game loop runs continuously during gameplay. Each turn of the loop, it processes user input
14+
without blocking, updates the game state, and renders the game. It tracks the passage of time to
15+
control the rate of gameplay.
1416

1517
This pattern decouples progression of game time from user input and processor speed.
1618

17-
## Applicability
19+
## Applicability
20+
1821
This pattern is used in every game engine.
1922

2023
## Explanation
24+
2125
Real world example
2226

23-
> 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.
27+
> Game loop is the main process of all the game rendering threads. It's present in all modern games.
28+
> It drives input process, internal status update, rendering, AI and all the other processes.
2429
2530
In plain words
2631

27-
> Game Loop pattern ensures that game time progresses in equal speed in all different hardware setups.
32+
> Game Loop pattern ensures that game time progresses in equal speed in all different hardware
33+
> setups.
2834
2935
Wikipedia says
3036

31-
> 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.
37+
> The central component of any game, from a programming standpoint, is the game loop. The game loop
38+
> allows the game to run smoothly regardless of a user's input, or lack thereof.
3239
3340
**Programmatic Example**
3441

35-
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.
42+
Let's start with something simple. Here's `Bullet` class. Bullets will move in our game. For
43+
demonstration purposes it's enough that it has 1-dimensional position.
3644

3745
```java
3846
public class Bullet {
@@ -53,7 +61,7 @@ public class Bullet {
5361
}
5462
```
5563

56-
GameController is responsible for moving objects in the game. Including the aforementioned bullet.
64+
`GameController` is responsible for moving objects in the game, including the aforementioned bullet.
5765

5866
```java
5967
public class GameController {
@@ -75,7 +83,8 @@ public class GameController {
7583
}
7684
```
7785

78-
Now we introduce the game loop. Or actually in this demo we have 3 different game loops.
86+
Now we introduce the game loop. Or actually in this demo we have 3 different game loops. Let's see
87+
the base class `GameLoop` first.
7988

8089
```java
8190
public enum GameStatus {
@@ -100,7 +109,7 @@ public abstract class GameLoop {
100109

101110
public void run() {
102111
status = GameStatus.RUNNING;
103-
gameThread = new Thread(() -> processGameLoop());
112+
gameThread = new Thread(this::processGameLoop);
104113
gameThread.start();
105114
}
106115

@@ -128,7 +137,11 @@ public abstract class GameLoop {
128137

129138
protected abstract void processGameLoop();
130139
}
140+
```
141+
142+
Here's the first game loop implementation, `FrameBasedGameLoop`:
131143

144+
```java
132145
public class FrameBasedGameLoop extends GameLoop {
133146

134147
@Override
@@ -144,59 +157,9 @@ public class FrameBasedGameLoop extends GameLoop {
144157
controller.moveBullet(0.5f);
145158
}
146159
}
147-
148-
public class VariableStepGameLoop extends GameLoop {
149-
150-
@Override
151-
protected void processGameLoop() {
152-
var lastFrameTime = System.currentTimeMillis();
153-
while (isGameRunning()) {
154-
processInput();
155-
var currentFrameTime = System.currentTimeMillis();
156-
var elapsedTime = currentFrameTime - lastFrameTime;
157-
update(elapsedTime);
158-
lastFrameTime = currentFrameTime;
159-
render();
160-
}
161-
}
162-
163-
protected void update(Long elapsedTime) {
164-
controller.moveBullet(0.5f * elapsedTime / 1000);
165-
}
166-
}
167-
168-
public class FixedStepGameLoop extends GameLoop {
169-
170-
private static final long MS_PER_FRAME = 20;
171-
172-
@Override
173-
protected void processGameLoop() {
174-
var previousTime = System.currentTimeMillis();
175-
var lag = 0L;
176-
while (isGameRunning()) {
177-
var currentTime = System.currentTimeMillis();
178-
var elapsedTime = currentTime - previousTime;
179-
previousTime = currentTime;
180-
lag += elapsedTime;
181-
182-
processInput();
183-
184-
while (lag >= MS_PER_FRAME) {
185-
update();
186-
lag -= MS_PER_FRAME;
187-
}
188-
189-
render();
190-
}
191-
}
192-
193-
protected void update() {
194-
controller.moveBullet(0.5f * MS_PER_FRAME / 1000);
195-
}
196-
}
197160
```
198161

199-
Finally we can show all these game loops in action.
162+
Finally, we show all the game loops in action.
200163

201164
```java
202165
try {
@@ -226,10 +189,66 @@ Finally we can show all these game loops in action.
226189
}
227190
```
228191

192+
Program output:
193+
194+
```java
195+
Start frame-based game loop:
196+
Current bullet position: 0.5
197+
Current bullet position: 1.0
198+
Current bullet position: 1.5
199+
Current bullet position: 2.0
200+
Current bullet position: 2.5
201+
Current bullet position: 3.0
202+
Current bullet position: 3.5
203+
Current bullet position: 4.0
204+
Current bullet position: 4.5
205+
Current bullet position: 5.0
206+
Current bullet position: 5.5
207+
Current bullet position: 6.0
208+
Stop frame-based game loop.
209+
Start variable-step game loop:
210+
Current bullet position: 6.5
211+
Current bullet position: 0.038
212+
Current bullet position: 0.084
213+
Current bullet position: 0.145
214+
Current bullet position: 0.1805
215+
Current bullet position: 0.28
216+
Current bullet position: 0.32
217+
Current bullet position: 0.42549998
218+
Current bullet position: 0.52849996
219+
Current bullet position: 0.57799995
220+
Current bullet position: 0.63199997
221+
Current bullet position: 0.672
222+
Current bullet position: 0.778
223+
Current bullet position: 0.848
224+
Current bullet position: 0.8955
225+
Current bullet position: 0.9635
226+
Stop variable-step game loop.
227+
Start fixed-step game loop:
228+
Current bullet position: 0.0
229+
Current bullet position: 1.086
230+
Current bullet position: 0.059999995
231+
Current bullet position: 0.12999998
232+
Current bullet position: 0.24000004
233+
Current bullet position: 0.33999994
234+
Current bullet position: 0.36999992
235+
Current bullet position: 0.43999985
236+
Current bullet position: 0.5399998
237+
Current bullet position: 0.65999967
238+
Current bullet position: 0.68999964
239+
Current bullet position: 0.7299996
240+
Current bullet position: 0.79999954
241+
Current bullet position: 0.89999944
242+
Current bullet position: 0.98999935
243+
Stop variable-step game loop.
244+
```
245+
229246
## Class diagram
247+
230248
![alt text](./etc/game-loop.urm.png "Game Loop pattern class diagram")
231249

232-
## Credits
250+
## Credits
251+
233252
* [Game Programming Patterns - Game Loop](http://gameprogrammingpatterns.com/game-loop.html)
234253
* [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)
235254
* [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)

Diff for: game-loop/src/main/java/com/iluwatar/gameloop/GameLoop.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public abstract class GameLoop {
3636

3737
protected volatile GameStatus status;
3838

39-
protected GameController controller;
39+
protected final GameController controller;
4040

4141
private Thread gameThread;
4242

@@ -53,7 +53,7 @@ public GameLoop() {
5353
*/
5454
public void run() {
5555
status = GameStatus.RUNNING;
56-
gameThread = new Thread(() -> processGameLoop());
56+
gameThread = new Thread(this::processGameLoop);
5757
gameThread.start();
5858
}
5959

0 commit comments

Comments
 (0)