Skip to content

Commit 26f4aa8

Browse files
committed
2 parents 51b444d + 1fdc650 commit 26f4aa8

File tree

99 files changed

+1599
-453
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+1599
-453
lines changed

game-loop/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
2+
---
3+
layout: pattern
4+
title: Game Loop
5+
folder: game-loop
6+
permalink: /patterns/game-loop/
7+
categories: Other
8+
tags:
9+
- Java
10+
- Difficulty-Beginner
11+
---
12+
13+
## Intent
14+
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.
15+
16+
This pattern decouple the progression of game time from user input and processor speed.
17+
18+
## Applicability
19+
This pattern is used in every game engine.
20+
21+
## Explanation
22+
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.
23+
24+
There are a lot of implementations of game loop:
25+
26+
- Frame-based game loop
27+
28+
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.
29+
30+
- Variable-step game loop
31+
32+
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.
33+
34+
- Fixed-step game loop
35+
36+
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.
37+
38+
39+
## Credits
40+
41+
* [Game Programming Patterns - Game Loop](http://gameprogrammingpatterns.com/game-loop.html)

game-loop/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
The MIT License
5+
Copyright © 2014-2019 Ilkka Seppälä
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in
15+
all copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
THE SOFTWARE.
24+
25+
-->
26+
<project xmlns="http://maven.apache.org/POM/4.0.0"
27+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
28+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
29+
<parent>
30+
<artifactId>java-design-patterns</artifactId>
31+
<groupId>com.iluwatar</groupId>
32+
<version>1.22.0-SNAPSHOT</version>
33+
</parent>
34+
<modelVersion>4.0.0</modelVersion>
35+
36+
<artifactId>game-loop</artifactId>
37+
<dependencies>
38+
<dependency>
39+
<groupId>junit</groupId>
40+
<artifactId>junit</artifactId>
41+
</dependency>
42+
</dependencies>
43+
44+
45+
</project>
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.gameloop;
25+
26+
import org.slf4j.Logger;
27+
import org.slf4j.LoggerFactory;
28+
29+
/**
30+
* A game loop runs continuously during gameplay. Each turn of the loop, it processes
31+
* user input without blocking, updates the game state, and renders the game. It tracks
32+
* the passage of time to control the rate of gameplay.
33+
*/
34+
public class App {
35+
36+
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
37+
38+
/**
39+
* Each type of game loop will run for 2 seconds.
40+
*/
41+
private static final int GAME_LOOP_DURATION_TIME = 2000;
42+
43+
/**
44+
* Program entry point.
45+
* @param args runtime arguments
46+
*/
47+
public static void main(String[] args) {
48+
49+
try {
50+
LOGGER.info("Start frame-based game loop:");
51+
var frameBasedGameLoop = new FrameBasedGameLoop();
52+
frameBasedGameLoop.run();
53+
Thread.sleep(GAME_LOOP_DURATION_TIME);
54+
frameBasedGameLoop.stop();
55+
LOGGER.info("Stop frame-based game loop.");
56+
57+
LOGGER.info("Start variable-step game loop:");
58+
var variableStepGameLoop = new VariableStepGameLoop();
59+
variableStepGameLoop.run();
60+
Thread.sleep(GAME_LOOP_DURATION_TIME);
61+
variableStepGameLoop.stop();
62+
LOGGER.info("Stop variable-step game loop.");
63+
64+
LOGGER.info("Start fixed-step game loop:");
65+
var fixedStepGameLoop = new FixedStepGameLoop();
66+
fixedStepGameLoop.run();
67+
Thread.sleep(GAME_LOOP_DURATION_TIME);
68+
fixedStepGameLoop.stop();
69+
LOGGER.info("Stop variable-step game loop.");
70+
71+
} catch (InterruptedException e) {
72+
LOGGER.error(e.getMessage());
73+
}
74+
}
75+
76+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.gameloop;
25+
26+
/**
27+
* Bullet object class.
28+
*/
29+
public class Bullet {
30+
31+
private float position;
32+
33+
public Bullet() {
34+
position = 0.0f;
35+
}
36+
37+
public float getPosition() {
38+
return position;
39+
}
40+
41+
public void setPosition(float position) {
42+
this.position = position;
43+
}
44+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.gameloop;
25+
26+
/**
27+
* For fixed-step game loop, a certain amount of real time has elapsed since the
28+
* last turn of the game loop. This is how much game time need to be simulated for
29+
* the game’s “now” to catch up with the player’s.
30+
*/
31+
public class FixedStepGameLoop extends GameLoop {
32+
33+
/**
34+
* 20 ms per frame = 50 FPS.
35+
*/
36+
private static final long MS_PER_FRAME = 20;
37+
38+
@Override
39+
protected void processGameLoop() {
40+
var previousTime = System.currentTimeMillis();
41+
var lag = 0L;
42+
while (isGameRunning()) {
43+
var currentTime = System.currentTimeMillis();
44+
var elapsedTime = currentTime - previousTime;
45+
previousTime = currentTime;
46+
lag += elapsedTime;
47+
48+
processInput();
49+
50+
while (lag >= MS_PER_FRAME) {
51+
update();
52+
lag -= MS_PER_FRAME;
53+
}
54+
55+
render();
56+
}
57+
}
58+
59+
protected void update() {
60+
controller.moveBullet(0.5f * MS_PER_FRAME / 1000);
61+
}
62+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.gameloop;
25+
26+
/**
27+
* Frame-based game loop is the easiest implementation. The loop always keeps spinning
28+
* for the following three processes: processInput, update and render. The problem with
29+
* it is you have no control over how fast the game runs. On a fast machine, that loop
30+
* will spin so fast users won’t be able to see what’s going on. On a slow machine, the
31+
* game will crawl. If you have a part of the game that’s content-heavy or does more AI
32+
* or physics, the game will actually play slower there.
33+
*/
34+
public class FrameBasedGameLoop extends GameLoop {
35+
36+
@Override
37+
protected void processGameLoop() {
38+
while (isGameRunning()) {
39+
processInput();
40+
update();
41+
render();
42+
}
43+
}
44+
45+
/**
46+
* Each time when update() is invoked, a new frame is created, and the bullet will be
47+
* moved 0.5f away from the current position.
48+
*/
49+
protected void update() {
50+
controller.moveBullet(0.5f);
51+
}
52+
53+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* The MIT License
3+
* Copyright © 2014-2019 Ilkka Seppälä
4+
*
5+
* Permission is hereby granted, free of charge, to any person obtaining a copy
6+
* of this software and associated documentation files (the "Software"), to deal
7+
* in the Software without restriction, including without limitation the rights
8+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
* copies of the Software, and to permit persons to whom the Software is
10+
* furnished to do so, subject to the following conditions:
11+
*
12+
* The above copyright notice and this permission notice shall be included in
13+
* all copies or substantial portions of the Software.
14+
*
15+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
* THE SOFTWARE.
22+
*/
23+
24+
package com.iluwatar.gameloop;
25+
26+
/**
27+
* Update and render objects in the game. Here we add a Bullet object to the
28+
* game system to show how the game loop works.
29+
*/
30+
public class GameController {
31+
32+
protected final Bullet bullet;
33+
34+
/**
35+
* Initialize Bullet instance.
36+
*/
37+
public GameController() {
38+
bullet = new Bullet();
39+
}
40+
41+
/**
42+
* Move bullet position by the provided offset.
43+
*
44+
* @param offset moving offset
45+
*/
46+
public void moveBullet(float offset) {
47+
var currentPosition = bullet.getPosition();
48+
bullet.setPosition(currentPosition + offset);
49+
}
50+
51+
/**
52+
* Get current position of the bullet.
53+
*
54+
* @return position of bullet
55+
*/
56+
public float getBulletPosition() {
57+
return bullet.getPosition();
58+
}
59+
60+
}
61+

0 commit comments

Comments
 (0)