Hello :). I have a quick question about frames per second.
I am working on a game and am trying to implement a constant frames per second. Everything is fine if I set it to about 50 fps, but anything faster and it will have a lower fps for the first 10 seconds or so, then it jumps up to what I want. Now, 50 fps is fine for this game, but I have a pretty fast computer and don’t know what is going to happen on a slower machine. Any reason this could happen?
Here is my code if it helps, but I think this is pretty standard.
long lastTime = System.nanoTime();
long thisTime;
long sleepTime = 1000/TARGET_FPS;
while (going ) {
thisTime = System.nanoTime();
updateStuff();
render();
long passedTime = thisTime - lastTime;
fps = (int) (1.0/((1.0)*passedTime)*1000000000L);
sleepTime = 1000/TARGET_FPS;
if(sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
System.exit(-1);
}
}
secs += 1.0/fps;
lastTime = thisTime;
}