Skip to content

Commit

Permalink
docs: added_documentation_for_game_loop_implementations (#2957)
Browse files Browse the repository at this point in the history
  • Loading branch information
gatlanagaprasanna committed May 15, 2024
1 parent e51522e commit ffd3440
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions game-loop/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,65 @@ public class FrameBasedGameLoop extends GameLoop {
}
}
```
Here's the next game loop implementation, `FixedStepGameLoop`:

```java
public class FixedStepGameLoop extends GameLoop {
/**
* 20 ms per frame = 50 FPS.
*/
private static final long MS_PER_FRAME = 20;
@Override
protected void processGameLoop() {
var previousTime = System.currentTimeMillis();
var lag = 0L;
while (isGameRunning()) {
var currentTime = System.currentTimeMillis();
var elapsedTime = currentTime - previousTime;
previousTime = currentTime;
lag += elapsedTime;
processInput();
while (lag >= MS_PER_FRAME) {
update();
lag -= MS_PER_FRAME;
}
render();
}
}
protected void update() {
controller.moveBullet(0.5f * MS_PER_FRAME / 1000);
}
}
```
And the last game loop implementation, `VariableStepGameLoop`:
```java
public class VariableStepGameLoop extends GameLoop {
@Override
protected void processGameLoop() {
var lastFrameTime = System.currentTimeMillis();
while (isGameRunning()) {
processInput();
var currentFrameTime = System.currentTimeMillis();
var elapsedTime = currentFrameTime - lastFrameTime;
update(elapsedTime);
lastFrameTime = currentFrameTime;
render();
}
}
protected void update(Long elapsedTime) {
controller.moveBullet(0.5f * elapsedTime / 1000);
}
}
```

Finally, we show all the game loops in action.

Expand Down

0 comments on commit ffd3440

Please sign in to comment.