Advertisement
Wolfrost

Java Game Loop

Jul 14th, 2015
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.53 KB | None | 0 0
  1. ///Public Java Game Loop
  2.  
  3. long lastTime = System.nanoTime();
  4. double amountOfTicks = 60.0;
  5. double ns = 1000000000 / amountOfTicks;
  6. double delta = 0;
  7. long timer = System.currentTimeMillis();
  8. int frames = 0;
  9. while (running) {
  10.     long now = System.nanoTime();
  11.     delta += (now - lastTime) / ns;
  12.     lastTime = now;
  13.     while (delta >= 1) {
  14.         tick();
  15.         delta--;
  16.     }
  17.     if (running) render();
  18.     frames++;
  19.            
  20.     if (System.currentTimeMillis() - timer > 1000) {
  21.         timer += 1000;
  22.         System.out.println("FPS: "+frames);
  23.         frames = 0;
  24.     }
  25. }
  26. stop();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement