Hello,
To celebrate the start of the new wiki we will try to create a gameloop together as a community. Since this is a bit of an easy task and there are many gameloop around I will ask to not copy paste any existing gameloop. Instead, to get a wiki feel to that experiment, each person contributing can only add, remove or modify a few lines.
Good luck community!
public class GameLoopExperiment implements Runnable {
private boolean running = true;
private final int framerate = 60;
private final long frameIntervalNanos = 1_000_000_000L / framerate;
public void logic(int deltaTime) {
for(int i=0; i<entities.size(); i++) {
entities[i].processTick(deltaTime);
}
}
public void render() {
for(int i=0; i<entities.size(); i++) {
entities[i].draw();
}
}
public boolean isRunning() {
return running;
}
public void exit() {
running = false;
}
public void run() {
long lastFrameNanos = System.nanoTime(); //Initialize it for the first time
while ( this.isRunning() ) {
long now = System.nanoTime(); // TODO wrap in a method that fixes negative nanoTimes
long deltaTime = now - lastFrameNanos;
lastFrameNanos = now;
logic((int)deltaTime);
render();
}
}
}