Guest User

Untitled

a guest
Jun 10th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.29 KB | None | 0 0
  1.     public void test() {
  2.         final double GAME_HERTZ = 144;
  3.         final double TIME_BETWEEN_UPDATES = 1000000000 / GAME_HERTZ;
  4.         final int MAX_UPDATES_BEFORE_RENDER = 5;
  5.         double lastUpdateTime = System.nanoTime();
  6.         double lastRenderTime = System.nanoTime();
  7.  
  8.         final double TARGET_FPS = 144;
  9.         final double TARGET_TIME_BETWEEN_RENDERS = 1000000000 / TARGET_FPS;
  10.        
  11.         int lastSecondTime = (int) (lastUpdateTime / 1000000000);
  12.  
  13.         while (isRunning) {
  14.             double now = System.nanoTime();
  15.             int updateCount = 0;
  16.  
  17.             if (!paused) {
  18.                 while (now - lastUpdateTime > TIME_BETWEEN_UPDATES && updateCount < MAX_UPDATES_BEFORE_RENDER) {
  19.                     tick();
  20.                     lastUpdateTime += TIME_BETWEEN_UPDATES;
  21.                     updateCount++;
  22.                 }
  23.  
  24.                 if (now - lastUpdateTime > TIME_BETWEEN_UPDATES) {
  25.                     lastUpdateTime = now - TIME_BETWEEN_UPDATES;
  26.                 }
  27.  
  28.                 render();
  29.                 lastRenderTime = now;
  30.  
  31.                 int thisSecond = (int) (lastUpdateTime / 1000000000);
  32.                 if (thisSecond > lastSecondTime) {
  33.                     System.out.println("FPS " + frameCount);
  34.                     fps = frameCount;
  35.                     frameCount = 0;
  36.                     lastSecondTime = thisSecond;
  37.                 }
  38.  
  39.                 while (now - lastRenderTime < TARGET_TIME_BETWEEN_RENDERS
  40.                         && now - lastUpdateTime < TIME_BETWEEN_UPDATES) {
  41.                     Thread.yield();
  42.                     now = System.nanoTime();
  43.                 }
  44.             }
  45.         }
  46.     }
Add Comment
Please, Sign In to add comment