Simply put, the following Game loop doestn seem to work, as the print-commadns BOTH only output 0
public static void run() {
int frames = 0;
long timePerFrameInNano = 1000000000/TARGETFPS; //TARGETFPS is set to 60
long lastUpdate = System.nanoTime();
long timer = System.nanoTime();
while(true) {
if(System.nanoTime()-lastUpdate >= timePerFrameInNano) { //If enough time passed
frames++; // count frames to check FPS after each second
lastUpdate = System.nanoTime();
update();
render();
System.out.println(frames);
}
if(timer >= 1000000000) { //After 1s, update the FPS
timer = System.nanoTime(); //Reset 1s timer
fps = frames; //All the frames counted afetr 1s should show me the current fps
System.out.println(fps);
frames = 0;
}
}
}
Well instead of the expected 60FPS it only shows me 0. I’ve been going back and forth in Debug-Mode and couldnt figure out whats wrong, to the point where it drives me crazy. I have done such loops before, a long tiem ago, and I never had problems. I’m pretty sure its a very stupid mistake that I am overlooking here, but I would be glad if you could point it out to me, because I cannot seem to find it.
Slimy Hoffy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.