Here's my simplified game loop:
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
| public void gameLoop() { nextCheck = System.nanoTime() + 1000000000; frameCounter = 0;
while(gameRunning) { calculateFPS(); game.tick(); game.getFrame().render();
while(System.nanoTime() < beforeRender + (1000000000 / game.FPS)) { Thread.yield(); } } }
private void calculateFPS() { beforeRender = System.nanoTime();
if(beforeRender >= nextCheck) { nextCheck = beforeRender + 1000000000; fps = frameCounter; frameCounter = 0; } frameCounter++; } |
game.tick() , this is just how I'm delegating things.

game.getFrame().render(); = me showing the bufferstrategy and disposing the graphics object.
Yes, I know I'm a freak for putting the calculate FPS code in it's own method.

Edit: eh, no idea why the indentation doesn't work right. Sorry.
Edit 2: This code locks the fps at 60 fps (the value of my constant).