diff --git a/src/java/org/lwjgl/Display.java b/src/java/org/lwjgl/Display.java index f80c6b20..a3cbcdd9 100644 --- a/src/java/org/lwjgl/Display.java +++ b/src/java/org/lwjgl/Display.java @@ -59,20 +59,9 @@ public final class Display { /** Whether or not the display has been requested to shutdown by the user */ private static boolean closeRequested = false; - - /* - * Platforms. This will let you determine which platform you are running - * on, which is handy to know for some GL context calls. - */ - - /** Windows platform */ - public static final int PLATFORM_WGL = 0; - - /** GLX (Linux/Unix) platform */ - public static final int PLATFORM_GLX = 1; - - /** MacOSX platform */ - public static final int PLATFORM_AGL = 2; + + /** Timer for sync() */ + private static long timeNow, timeThen; static { Sys.initialize(); @@ -177,16 +166,6 @@ public final class Display { return mode.freq; } - /** - * Returns the operating system windowing platform. This will be one of the - * constants defined above. There is no "unknown" platform; a native library port - * has to provide a unique platform number for this mechanism to work. If the LWJGL - * is ported to, say, QNX, we will have a PLATFORM_QNX at the ready. - * - * @return the windowing system - */ - public static native int getPlatform(); - /** * Set the display configuration to the specified gamma, brightness and contrast. * The configuration changes will be reset when resetDisplayMode is called. @@ -250,5 +229,21 @@ public final class Display { * @return a String */ public static native String getVersion(); + + /** + * Synchronize the display to a capped frame rate. + * @param frameTime The desired frame time in seconds + */ + public static void sync(float frameRate) { + timeNow = Sys.getTime(); + System.out.println(Sys.getTimerResolution()); + System.out.println(timeNow+" "+timeThen+" "+((float) (timeNow - timeThen) / (float) Sys.getTimerResolution())); + while (timeNow > timeThen && (float) (timeNow - timeThen) / (float) Sys.getTimerResolution() < frameRate) { + // This is a system-friendly way of allowing other stuff to use CPU if it wants to + Thread.yield(); + timeNow = Sys.getTime(); + } + timeThen = timeNow; + } } diff --git a/src/java/org/lwjgl/Sys.java b/src/java/org/lwjgl/Sys.java index 62012936..ebfc2c0b 100644 --- a/src/java/org/lwjgl/Sys.java +++ b/src/java/org/lwjgl/Sys.java @@ -81,7 +81,7 @@ public final class Sys { /** The native library name */ private static String LIBRARY_NAME = "lwjgl"; - /** The platform being executed on */ + /** The platform adapter class name */ private static String PLATFORM; /** @@ -131,13 +131,8 @@ public final class Sys { throw new LinkageError("Version mismatch: jar version is '" + VERSION + "', native libary version is '" + native_version + "'"); setDebug(DEBUG); - setTime(0); - // check platform name, and default to awt - PLATFORM = System.getProperty("org.lwjgl.Sys.platform"); - if(PLATFORM == null) { - PLATFORM = "org.lwjgl.SwingAdapter"; - } + PLATFORM = System.getProperty("org.lwjgl.Sys.platform", "org.lwjgl.SwingAdapter"); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { @@ -173,20 +168,14 @@ public final class Sys { /** * Gets the current value of the hires timer, in ticks. When the Sys class is first loaded * the hires timer is reset to 0. If no hires timer is present then this method will always - * return whatever value the timer was last set to. + * return 0.

NOTEZ BIEN that the hires timer WILL wrap around. * - * @return the current hires time, in ticks. + * @return the current hires time, in ticks (always >= 0) */ - public static native long getTime(); - - /** - * Sets the hires timer to a new time, specified in ticks. - * - * @param time The new time, in ticks - * @see #getTime() - * @see #getTimerResolution() - */ - public static native void setTime(long time); + public static long getTime() { + return ngetTime() & 0x7FFFFFFFFFFFFFFFL; + } + private static native long ngetTime(); /** * Set the process priority in a system independent way. Because of the various diff --git a/src/java/org/lwjgl/examples/Game.java b/src/java/org/lwjgl/examples/Game.java index ae6e9239..dfa7f3aa 100644 --- a/src/java/org/lwjgl/examples/Game.java +++ b/src/java/org/lwjgl/examples/Game.java @@ -109,18 +109,9 @@ public class Game { finished = true; } else if (Window.isActive()) { // The window is in the foreground, so we should play the game - long timeThen = Sys.getTime(); logic(); render(); - // Stabilise the framerate if we haven't got vsync - if (!Window.isVSyncEnabled()) { - long timeNow = Sys.getTime(); - while ((float) (timeNow - timeThen) / (float) Sys.getTimerResolution() < FRAMETIME) { - // This is a system-friendly way of allowing other stuff to use CPU if it wants to - Thread.yield(); - timeNow = Sys.getTime(); - } - } + org.lwjgl.Display.sync(FRAMETIME); } else { // The window is not in the foreground, so we can allow other stuff to run and // infrequently update diff --git a/src/java/org/lwjgl/input/Cursor.java b/src/java/org/lwjgl/input/Cursor.java index 71ec310c..546818a3 100644 --- a/src/java/org/lwjgl/input/Cursor.java +++ b/src/java/org/lwjgl/input/Cursor.java @@ -36,9 +36,8 @@ import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.IntBuffer; -import org.lwjgl.Display; -import org.lwjgl.Sys; import org.lwjgl.LWJGLException; +import org.lwjgl.Sys; /** * $Id$ @@ -108,28 +107,25 @@ public class Cursor { // Win32 or X and do accordingly. This hasn't been implemented on Mac, but we // might want to split it into a X/Win/Mac cursor if it gets too cluttered - switch(Display.getPlatform()) { - case Display.PLATFORM_GLX: - // create our cursor elements - cursors = new CursorElement[1]; - cursors[0] = new CursorElement(); - cursors[0].cursorHandle = nCreateCursor(width, height, xHotspot, yHotspot, numImages, images_copy, images_copy.position(), delays, delays != null ? delays.position() : -1); - break; - case Display.PLATFORM_WGL: - // create our cursor elements - cursors = new CursorElement[numImages]; - for(int i=0; iFindClass("org/lwjgl/DisplayMode"); jobjectArray ret = env->NewObjectArray(num_modes, displayModeClass, NULL); jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "", "(IIII)V"); - + for (i = 0; i < num_modes; i++) { jobject displayMode = env->NewObject(displayModeClass, displayModeConstructor, avail_modes[i]->hdisplay, avail_modes[i]->vdisplay, bpp, 0); env->SetObjectArrayElement(ret, i, displayMode); @@ -238,10 +237,6 @@ JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_nGetAvailableDisplayModes return ret; } -JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getPlatform(JNIEnv * env, jclass clazz) { - return org_lwjgl_Display_PLATFORM_GLX; -} - JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getGammaRampLength(JNIEnv *env, jclass clazz) { return gamma_ramp_length; } diff --git a/src/native/linux/org_lwjgl_Sys.cpp b/src/native/linux/org_lwjgl_Sys.cpp index 1557d545..33249815 100644 --- a/src/native/linux/org_lwjgl_Sys.cpp +++ b/src/native/linux/org_lwjgl_Sys.cpp @@ -45,8 +45,6 @@ #include "org_lwjgl_Sys.h" #include "common_tools.h" -static long int hires_timer_freq; // Hires timer frequency -static long int hires_timer_start; // Hires timer start static long int hires_timer; // Hires timer current time /* @@ -57,7 +55,8 @@ static long int hires_timer; // Hires timer current time JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTimerResolution (JNIEnv * env, jclass clazz) { - return hires_timer_freq; + // Constant on linux + return 1000000; } static long queryTime(void) { @@ -76,29 +75,14 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setDebug(JNIEnv *env, jclass clazz, jb /* * Class: org_lwjgl_Sys - * Method: getTime + * Method: ngetTime * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTime +JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_ngetTime (JNIEnv * env, jclass clazz) { hires_timer = queryTime(); - hires_timer -= hires_timer_start; - return hires_timer; -} - -/* - * Class: org_lwjgl_Sys - * Method: setTime - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setTime - (JNIEnv * env, jclass clazz, jlong startTime) -{ - hires_timer_start = queryTime(); - // We don't have a real resolution so assume highest possible - hires_timer_freq = 1000000; - hires_timer_start -= startTime; + return (jlong) hires_timer; } /* diff --git a/src/native/linux/org_lwjgl_opengl_Window.cpp b/src/native/linux/org_lwjgl_opengl_Window.cpp index 25a32537..c165c362 100644 --- a/src/native/linux/org_lwjgl_opengl_Window.cpp +++ b/src/native/linux/org_lwjgl_opengl_Window.cpp @@ -613,12 +613,6 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsActive return focused ? JNI_TRUE : JNI_FALSE; } -JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsVSyncEnabled - (JNIEnv * env, jclass clazz) -{ - return vsync_enabled; -} - JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nSetVSyncEnabled (JNIEnv * env, jclass clazz, jboolean sync) { diff --git a/src/native/macosx/org_lwjgl_Display.cpp b/src/native/macosx/org_lwjgl_Display.cpp index de8cb21d..296ade6e 100644 --- a/src/native/macosx/org_lwjgl_Display.cpp +++ b/src/native/macosx/org_lwjgl_Display.cpp @@ -189,9 +189,6 @@ JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_nGetAvailableDisplayModes( return ret; } -JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getPlatform(JNIEnv * env, jclass clazz) { - return org_lwjgl_Display_PLATFORM_AGL; -} JNIEXPORT jint JNICALL Java_org_lwjgl_Display_getGammaRampLength(JNIEnv *env, jclass clazz) { return GAMMARAMP_LENGTH; diff --git a/src/native/macosx/org_lwjgl_Sys.cpp b/src/native/macosx/org_lwjgl_Sys.cpp index 8e30e78e..6c02f61e 100644 --- a/src/native/macosx/org_lwjgl_Sys.cpp +++ b/src/native/macosx/org_lwjgl_Sys.cpp @@ -46,9 +46,7 @@ #include "org_lwjgl_Sys.h" #include "common_tools.h" -long int hires_timer_freq; // Hires timer frequency -long int hires_timer_start; // Hires timer start -long int hires_timer; // Hires timer current time +static long int hires_timer; // Hires timer current time /* * Class: org_lwjgl_Sys @@ -58,7 +56,8 @@ long int hires_timer; // Hires timer current time JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTimerResolution (JNIEnv * env, jclass clazz) { - return hires_timer_freq; + // Constant on MacOS + return 1000000; } static long queryTime(void) { @@ -77,34 +76,20 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setDebug(JNIEnv *env, jclass clazz, jb /* * Class: org_lwjgl_Sys - * Method: getTime + * Method: ngetTime * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTime +JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_ngetTime (JNIEnv * env, jclass clazz) { hires_timer = queryTime(); - hires_timer -= hires_timer_start; - return hires_timer; + return (jlong) hires_timer; } JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_getNativeLibraryVersion(JNIEnv *env, jclass clazz) { return getVersionString(env); } -/* - * Class: org_lwjgl_Sys - * Method: setTime - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setTime -(JNIEnv * env, jclass clazz, jlong startTime) -{ - hires_timer_start = queryTime(); - // We don't have a real resolution so assume highest possible - hires_timer_freq = 1000000; - hires_timer_start -= startTime; -} /* * Class: org_lwjgl_Sys diff --git a/src/native/macosx/org_lwjgl_opengl_Window.cpp b/src/native/macosx/org_lwjgl_opengl_Window.cpp index fdf7e87d..fdc3d6c5 100644 --- a/src/native/macosx/org_lwjgl_opengl_Window.cpp +++ b/src/native/macosx/org_lwjgl_opengl_Window.cpp @@ -1,35 +1,35 @@ -/* +/* * Copyright (c) 2002 Light Weight Java Game Library Project * All rights reserved. - * + * * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are + * modification, are permitted provided that the following conditions are * met: - * - * * Redistributions of source code must retain the above copyright + * + * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * - * * Neither the name of 'Light Weight Java Game Library' nor the names of - * its contributors may be used to endorse or promote products derived + * * Neither the name of 'Light Weight Java Game Library' nor the names of + * its contributors may be used to endorse or promote products derived * from this software without specific prior written permission. - * + * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - + /** * $Id$ * @@ -49,7 +49,7 @@ static CGLContextObj context; static bool vsync_enabled; static bool current_fullscreen; - + static void destroyMode(JNIEnv *env, jclass clazz) { if (!current_fullscreen) resetMode(env); @@ -162,7 +162,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nUpdate(JNIEnv *env, jclass JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_swapBuffers(JNIEnv * env, jclass clazz) { CGLFlushDrawable(context); } - + JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_minimize(JNIEnv *env, jclass clazz) { } @@ -185,10 +185,6 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsMinimized(JNIEnv *env return JNI_FALSE; } -JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsVSyncEnabled(JNIEnv *env, jclass clazz) { - return vsync_enabled; -} - JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsVisible(JNIEnv *env, jclass clazz) { return JNI_TRUE; } diff --git a/src/native/win32/org_lwjgl_Sys.cpp b/src/native/win32/org_lwjgl_Sys.cpp index 60901e38..15d5de9f 100644 --- a/src/native/win32/org_lwjgl_Sys.cpp +++ b/src/native/win32/org_lwjgl_Sys.cpp @@ -46,9 +46,8 @@ // Handle to the application's window extern HWND hwnd; -unsigned __int64 hires_timer_freq; // Hires timer frequency -unsigned __int64 hires_timer_start; // Hires timer start -unsigned __int64 hires_timer; // Hires timer current time +unsigned __int64 hires_timer_freq = 0; // Hires timer frequency +unsigned __int64 hires_timer = 0; // Hires timer current time /* * Class: org_lwjgl_Sys @@ -58,7 +57,8 @@ unsigned __int64 hires_timer; // Hires timer current time JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTimerResolution (JNIEnv * env, jclass clazz) { - return hires_timer_freq; + QueryPerformanceFrequency((LARGE_INTEGER*) &hires_timer_freq); + return (jlong) hires_timer_freq; } JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setDebug(JNIEnv *env, jclass clazz, jboolean enabled) { @@ -71,28 +71,14 @@ JNIEXPORT jstring JNICALL Java_org_lwjgl_Sys_getNativeLibraryVersion(JNIEnv *env /* * Class: org_lwjgl_Sys - * Method: getTime + * Method: ngetTime * Signature: ()J */ -JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_getTime +JNIEXPORT jlong JNICALL Java_org_lwjgl_Sys_ngetTime (JNIEnv * env, jclass clazz) { QueryPerformanceCounter((LARGE_INTEGER*) &hires_timer); - hires_timer -= hires_timer_start; - return hires_timer; -} - -/* - * Class: org_lwjgl_Sys - * Method: setTime - * Signature: (J)V - */ -JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setTime - (JNIEnv * env, jclass clazz, jlong startTime) -{ - QueryPerformanceFrequency((LARGE_INTEGER*) &hires_timer_freq); - QueryPerformanceCounter((LARGE_INTEGER*) &hires_timer_start); - hires_timer_start -= startTime; + return (jlong) hires_timer; } /* diff --git a/src/native/win32/org_lwjgl_opengl_Window.cpp b/src/native/win32/org_lwjgl_opengl_Window.cpp index 7d8db6e9..4a3aa961 100755 --- a/src/native/win32/org_lwjgl_opengl_Window.cpp +++ b/src/native/win32/org_lwjgl_opengl_Window.cpp @@ -58,7 +58,6 @@ extern HINSTANCE dll_handle; // Handle to the LWJGL dll RECT clientSize; static bool closerequested; -static jboolean vsync; static jboolean allowSoftwareOpenGL; // Whether to allow software opengl //CAS: commented these out as no longer used @@ -471,7 +470,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nCreate isDirty = true; isFullScreen = fullscreen == JNI_TRUE; isUndecorated = getBooleanProperty(env, "org.lwjgl.opengl.Window.undecorated"); - vsync = JNI_FALSE; // Speacial option for allowing software opengl allowSoftwareOpenGL = getBooleanProperty(env, "org.lwjgl.opengl.Window.allowSoftwareOpenGL"); @@ -586,17 +584,6 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsActive return isFocused; } -/* - * Class: org_lwjgl_opengl_Window - * Method: nIsVSyncEnabled - * Signature: ()Z - */ -JNIEXPORT jboolean JNICALL Java_org_lwjgl_opengl_Window_nIsVSyncEnabled - (JNIEnv * env, jclass clazz) -{ - return vsync; -} - /* * Class: org_lwjgl_opengl_Window * Method: nSetVSyncEnabled @@ -611,7 +598,6 @@ JNIEXPORT void JNICALL Java_org_lwjgl_opengl_Window_nSetVSyncEnabled } else { wglSwapIntervalEXT(0); } - vsync = sync; } }