JEGL (Java Efficient Game Loop) is a lightweight Java library for creating smooth and efficient game loops. It helps manage game updates and timing accurately while using minimal CPU resources. It’s perfect for game development and real-time applications.
Here’s how to get started with JEGL and use different loop types for various needs.
Gradle:
repositories {
maven { url 'https://jitpack.io' }
}
dependencies {
implementation 'com.github.teixayo:JEGL:v1.0'
}Maven:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.teixayo</groupId>
<artifactId>JEGL</artifactId>
<version>v1.0</version>
</dependency>
</dependencies>Minecraft Server (20 TPS):
Loop loop = LoopBuilder.builder()
.loopType(LoopType.BUSY_WAIT_LOCK)
.updatePerSecond(20)
.useThread()
.loopApp(new MinecraftServer())
.build();
loop.start();Game Application (120 FPS):
Loop loop = LoopBuilder.builder()
.loopType(LoopType.LOCK)
.updatePerSecond(120)
.useThread()
.loopApp(new Game())
.build();
loop.start();To fix issues with Windows sleep accuracy, make sure to call DaemonThread.active() before starting the loop.
JEGL provides several types of loops to fit different needs:
BusyWaitLockLoop: Combines busy-waiting with locking to provide very accurate timing for updates while managing CPU usage effectively. It’s best for situations where accuracy is crucial.LockLoop: UsesLockSupport.parkNanos()to efficiently sleep between updates. This type offers the best CPU usage while still maintaining good update accuracy.BusyWaitLoop: Continuously checks the update time, which uses a lot of CPU and can cause high CPU usage. It’s not recommended for most cases due to its inefficient CPU usage.BusyWaitYield: Similar toBusyWaitLoop, but usesThread.yield()to hint to the system to reduce CPU usage. It’s a bit more efficient but still not ideal for CPU usage.
Avoid using BusyWaitLoop and BusyWaitYield due to their high CPU usage. Instead, consider LockLoop for the best CPU usage or BusyWaitLockLoop if timing accuracy is more critical.
JEGL includes a LoopStats class to give you real-time information about your game loop’s performance. You can access these stats through loop.getLoopStats().
currentMillisPerUpdate: Elapsed time in milliseconds for each update.currentUpdatePerSecond: Current updates per second.updates: Number of updates that have occurred.