You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: game-loop/README.md
+82-63
Original file line number
Diff line number
Diff line change
@@ -8,31 +8,39 @@ tags:
8
8
- Game programming
9
9
---
10
10
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.
14
16
15
17
This pattern decouples progression of game time from user input and processor speed.
16
18
17
-
## Applicability
19
+
## Applicability
20
+
18
21
This pattern is used in every game engine.
19
22
20
23
## Explanation
24
+
21
25
Real world example
22
26
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.
24
29
25
30
In plain words
26
31
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.
28
34
29
35
Wikipedia says
30
36
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.
32
39
33
40
**Programmatic Example**
34
41
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.
36
44
37
45
```java
38
46
publicclassBullet {
@@ -53,7 +61,7 @@ public class Bullet {
53
61
}
54
62
```
55
63
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.
57
65
58
66
```java
59
67
publicclassGameController {
@@ -75,7 +83,8 @@ public class GameController {
75
83
}
76
84
```
77
85
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.
79
88
80
89
```java
81
90
publicenumGameStatus {
@@ -100,7 +109,7 @@ public abstract class GameLoop {
100
109
101
110
publicvoidrun() {
102
111
status =GameStatus.RUNNING;
103
-
gameThread =newThread(() ->processGameLoop());
112
+
gameThread =newThread(this::processGameLoop);
104
113
gameThread.start();
105
114
}
106
115
@@ -128,7 +137,11 @@ public abstract class GameLoop {
128
137
129
138
protectedabstractvoidprocessGameLoop();
130
139
}
140
+
```
141
+
142
+
Here's the first game loop implementation, `FrameBasedGameLoop`:
131
143
144
+
```java
132
145
publicclassFrameBasedGameLoopextendsGameLoop {
133
146
134
147
@Override
@@ -144,59 +157,9 @@ public class FrameBasedGameLoop extends GameLoop {
144
157
controller.moveBullet(0.5f);
145
158
}
146
159
}
147
-
148
-
publicclassVariableStepGameLoopextendsGameLoop {
149
-
150
-
@Override
151
-
protectedvoidprocessGameLoop() {
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
-
protectedvoidupdate(LongelapsedTime) {
164
-
controller.moveBullet(0.5f* elapsedTime /1000);
165
-
}
166
-
}
167
-
168
-
publicclassFixedStepGameLoopextendsGameLoop {
169
-
170
-
privatestaticfinallongMS_PER_FRAME=20;
171
-
172
-
@Override
173
-
protectedvoidprocessGameLoop() {
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
-
protectedvoidupdate() {
194
-
controller.moveBullet(0.5f*MS_PER_FRAME/1000);
195
-
}
196
-
}
197
160
```
198
161
199
-
Finally we can show all these game loops in action.
162
+
Finally, we show all the game loops in action.
200
163
201
164
```java
202
165
try {
@@ -226,10 +189,66 @@ Finally we can show all these game loops in action.
226
189
}
227
190
```
228
191
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
+
229
246
## Class diagram
247
+
230
248

231
249
232
-
## Credits
250
+
## Credits
251
+
233
252
*[Game Programming Patterns - Game Loop](http://gameprogrammingpatterns.com/game-loop.html)
*[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)
0 commit comments