Hello there! I have started to make a game in Java, and I would appreciate some comments on my Game Loop.
It basically limits the amount of update calls that are made to 60 per second, allowing the render calls to be unlimited. I am not sure if this is the ideal approach as I don't have a background in Game Programming. Would it make more sense to limit the render method too?
public void run() {
final float updates = 1000000000/60; // 60 ups (updates por segundo)
float prevUpdate = System.nanoTime();
float diff, timeNow;
float delta = 0, delta2 = 0;
int ups=0,fps=0;
while(isRunning){
timeNow = System.nanoTime();
diff = timeNow-prevUpdate;
prevUpdate = timeNow;
delta2+=diff;
delta+=diff; // delta holds the time since the last update
if(delta2>1000000000){ // OPTIONAL: I use it to calculate fps and ups
updateFrames(fps,ups);
delta2 = 0;
fps=0;
ups=0;
}
if(delta>=updates){ // delta resets 60 times per second( and update is called)
delta = 0;
update();
ups++;
}
render(); // the render method isnt limited
fps++;
}
}
private void updateFrames(int fps, int ups) {
System.out.println("FPS: "+fps+" UPS: "+ups);
}