Hey,
For the past few months I've been messing around on a little project of mine and now I'm just getting to the point where I can start using the game loop again. I haven't even looked at this loop in a few months so I'm not sure how to fix it up the way I want just yet. Could someone explain how I could go about limiting the FPS to stop it from running at a few thousand FPS?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| public void run() { long lastTime = System.nanoTime(); long timer = System.currentTimeMillis(); final double NANO_SECONDS = 1000000000.0 / 60.0; double delta = 0.0; int frames = 0; int updates = 0; new TESTING();
while (running) { long now = System.nanoTime(); delta += (now - lastTime) / NANO_SECONDS; lastTime = now; while(delta >= 1) { updateLogic(); updates++; delta--; } renderScreen(); frames++; if((System.currentTimeMillis() - timer) > 1000) { timer += 1000; FRAME.setTitle(updates + "UPS, " + frames + "FPS"); updates = 0; frames = 0; } } stop(); } |
I'm fairly sure that I based this game loop off of TheCherno's youtube tutorials but it was quite a while back so I'm not sure.
Thanks in advance for any replies.