Replaced sync() with a method that works, based on sync2() and Thread.sleep(1)

This commit is contained in:
Caspian Rychlik-Prince 2005-04-09 16:45:14 +00:00
parent 7c8f43e915
commit 24c3017d68
1 changed files with 28 additions and 3 deletions

View File

@ -326,7 +326,7 @@ public final class Display {
* to prevent just missing the frame time if vsync is set. * to prevent just missing the frame time if vsync is set.
* @param fps The desired frame rate, in frames per second * @param fps The desired frame rate, in frames per second
*/ */
public static void sync(int fps) { public static void sync3(int fps) {
float frameTime = 1.0f / (float) (fps > 1 ? fps - 1 : 1); float frameTime = 1.0f / (float) (fps > 1 ? fps - 1 : 1);
timeNow = Sys.getTime(); timeNow = Sys.getTime();
while (timeNow > timeThen && (float) (timeNow - timeThen) / (float) Sys.getTimerResolution() < frameTime) { while (timeNow > timeThen && (float) (timeNow - timeThen) / (float) Sys.getTimerResolution() < frameTime) {
@ -361,6 +361,31 @@ public final class Display {
timeThen = timeNow; timeThen = timeNow;
} }
/**
* Best sync method that works reliably.
*
* @param fps The desired frame rate, in frames per second
*/
public static void sync(int fps) {
long gapTo = Sys.getTimerResolution() / fps + timeThen;
timeNow = Sys.getTime();
while (gapTo > timeNow + timeLate) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
}
timeNow = Sys.getTime();
}
if (gapTo < timeNow)
timeLate = timeNow - gapTo;
else
timeLate = 0;
timeThen = timeNow;
}
/** /**
* @return the X coordinate of the window (always 0 for fullscreen) * @return the X coordinate of the window (always 0 for fullscreen)
*/ */
@ -514,7 +539,7 @@ public final class Display {
if (isVisible() || isDirty()) { if (isVisible() || isDirty()) {
Util.checkGLError(); Util.checkGLError();
try { try {
context.swapBuffers(); Context.swapBuffers();
} catch (LWJGLException e) { } catch (LWJGLException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -707,7 +732,7 @@ public final class Display {
public static void setVSyncEnabled(boolean sync) { public static void setVSyncEnabled(boolean sync) {
vsync = sync; vsync = sync;
if (isCreated()) if (isCreated())
context.setVSync(vsync); Context.setVSync(vsync);
} }
/** /**