
LWJGL: Display.sync
By: a guest on
May 3rd, 2012 | syntax:
Java | size: 0.71 KB | hits: 592 | expires: Never
/**
* Best sync method that works reliably.
*
* @param fps The desired frame rate, in frames per second
*/
public static void sync(int fps) {
long timeNow;
long gapTo;
long savedTimeLate;
synchronized ( GlobalLock.lock ) {
gapTo = Sys.getTimerResolution() / fps + timeThen;
timeNow = Sys.getTime();
savedTimeLate = timeLate;
}
try {
while ( gapTo > timeNow + savedTimeLate ) {
Thread.sleep(1);
timeNow = Sys.getTime();
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
synchronized ( GlobalLock.lock ) {
if ( gapTo < timeNow )
timeLate = timeNow - gapTo;
else
timeLate = 0;
timeThen = timeNow;
}
}