Managing game loops properly can be significantly more complicated than you might expect. First of all, should you put your update and render functions in the same loop?. Furthermore, there are many ways to time your loops, and each approach has its own drawbacks. This mini library provides a convenient class to handle the timing of game loops.
The usage is best shown using example code:
class SampleGame {
public static void main(String[] args) {
long updatePeriod = 50_000_000L; // 50 milliseconds = 50M nanoseconds
new Thread(new UpdateLoop(updatePeriod, updateLoop -> {
synchronized (gameState) {
gameState.update();
}
if (!shouldContinue) updateLoop.stop();
})).start();
while (shouldContinue) {
synchronized (gameState) {
gameState.render();
}
}
}
}
This example would update approximately 20 times per second and render with maximum FPS. Note that this is not necessarily the smartest game loop: the parallelization could be improved significantly with smarter synchronization.
For convenience, this library also provides an UpdateCounter
class that can count updates or frames:
class Sample {
void test() {
UpdateCounter counter = new UpdateCounter();
while (shouldContinue) {
counter.increment();
render();
System.out.println("FPS is " + counter.getValue());
}
}
}
For demonstration purposes, this project also has a testbench
module
.
Since this library only has 3 small classes, you could just copy & paste them into your own project. If you prefer proper dependency management, you can use Jitpack:
...
repositories {
...
maven { url 'https://jitpack.io' }
}
...
dependencies {
...
implementation 'com.github.knokko.update-loop:implementation:v2.0.0'
}
...
<repositories>
...
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
...
<dependency>
<groupId>com.github.knokko.update-loop</groupId>
<artifactId>implementation</artifactId>
<version>v2.0.0</version>
</dependency>