Don't like ads? PRO users don't see any ads ;-)
Guest

LWJGL: Display.sync

By: a guest on May 3rd, 2012  |  syntax: Java  |  size: 0.71 KB  |  hits: 592  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.         /**
  2.          * Best sync method that works reliably.
  3.          *
  4.          * @param fps The desired frame rate, in frames per second
  5.          */
  6.         public static void sync(int fps) {
  7.                 long timeNow;
  8.                 long gapTo;
  9.                 long savedTimeLate;
  10.                 synchronized ( GlobalLock.lock ) {
  11.                         gapTo = Sys.getTimerResolution() / fps + timeThen;
  12.                         timeNow = Sys.getTime();
  13.                         savedTimeLate = timeLate;
  14.                 }
  15.  
  16.                 try {
  17.                         while ( gapTo > timeNow + savedTimeLate ) {
  18.                                 Thread.sleep(1);
  19.                                 timeNow = Sys.getTime();
  20.                         }
  21.                 } catch (InterruptedException e) {
  22.                         Thread.currentThread().interrupt();
  23.                 }
  24.  
  25.                 synchronized ( GlobalLock.lock ) {
  26.                         if ( gapTo < timeNow )
  27.                                 timeLate = timeNow - gapTo;
  28.                         else
  29.                                 timeLate = 0;
  30.  
  31.                         timeThen = timeNow;
  32.                 }
  33.         }