|
From: Caspian Rychlik-P. <ci...@us...> - 2003-03-29 21:52:27
|
Update of /cvsroot/java-game-lib/LWJGL/src/native/win32 In directory sc8-pr-cvs1:/tmp/cvs-serv10824/src/native/win32 Modified Files: org_lwjgl_Display.cpp Window.h org_lwjgl_Window.cpp Log Message: Stuff for 0.6 Index: org_lwjgl_Display.cpp CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/native/win32/org_lwjgl_Display.cpp =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/native/win32/org_lwjgl_Display.cpp,v retrieving revision 1.36 retrieving revision 1.37 diff -u -d -r1.36 -r1.37 --- org_lwjgl_Display.cpp 28 Mar 2003 23:16:14 -0000 1.36 +++ org_lwjgl_Display.cpp 29 Mar 2003 21:52:13 -0000 1.37 @@ -47,6 +47,7 @@ jobjectArray GetAvailableDisplayModesNT(JNIEnv * env); jobjectArray GetAvailableDisplayModes9x(JNIEnv * env); bool modeSet = false; // Whether we've done a display mode change +WORD* originalGamma = new WORD[256 * 3]; // Original gamma settings /* @@ -238,7 +239,7 @@ // class's mode instance variable. // Get the screen - HDC screenDC = CreateCompatibleDC(NULL); + HDC screenDC = GetDC(NULL); // Get the device caps width = GetDeviceCaps(screenDC, HORZRES); height = GetDeviceCaps(screenDC, VERTRES); @@ -246,7 +247,7 @@ freq = GetDeviceCaps(screenDC, VREFRESH); if (freq <= 1) freq = 0; // Unknown - DeleteDC(screenDC); + ReleaseDC(NULL, screenDC); jmethodID ctor = env->GetMethodID(cls_displayMode, "<init>", "(IIII)V"); jobject newMode = env->NewObject(cls_displayMode, ctor, width, height, bpp, freq); @@ -265,6 +266,12 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Display_resetDisplayMode (JNIEnv * env, jclass clazz) { + + // Return device gamma to normal + HDC screenDC = GetDC(NULL); + SetDeviceGammaRamp(screenDC, originalGamma); + ReleaseDC(NULL, screenDC); + if (modeSet) { modeSet = false; // Under Win32, all we have to do is: @@ -280,20 +287,127 @@ * Method: getGammaRamp * Signature: ()[I */ -JNIEXPORT jintArray JNICALL Java_org_lwjgl_Display_getGammaRamp - (JNIEnv * env, jclass clazz) +JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_getGammaRamp + (JNIEnv * env, jclass clazz, jintArray red, jintArray green, jintArray blue) { - return NULL; +#ifdef _DEBUG + if (red == NULL) { + throwRuntimeException(env, "Null red array."); + return JNI_FALSE; + } + if (green == NULL) { + throwRuntimeException(env, "Null green array."); + return JNI_FALSE; + } + if (blue == NULL) { + throwRuntimeException(env, "Null blue array."); + return JNI_FALSE; + } + if (env->GetArrayLength(red) != 256) { + throwRuntimeException(env, "Red array is not 256 long."); + return JNI_FALSE; + } + if (env->GetArrayLength(green) != 256) { + throwRuntimeException(env, "Green array is not 256 long."); + return JNI_FALSE; + } + if (env->GetArrayLength(blue) != 256) { + throwRuntimeException(env, "Blue array is not 256 long."); + return JNI_FALSE; + } +#endif + + jint * redPtr = env->GetIntArrayElements(red, NULL); + jint * greenPtr = env->GetIntArrayElements(green, NULL); + jint * bluePtr = env->GetIntArrayElements(blue, NULL); + + WORD currentGamma[768]; + HDC screenDC = GetDC(NULL); + if (GetDeviceGammaRamp(screenDC, currentGamma) == FALSE) { +#ifdef _DEBUG + printf("Failed to get device gamma\n"); +#endif + env->ReleaseIntArrayElements(red, redPtr, JNI_ABORT); + env->ReleaseIntArrayElements(green, greenPtr, JNI_ABORT); + env->ReleaseIntArrayElements(blue, bluePtr, JNI_ABORT); + ReleaseDC(NULL, screenDC); + return JNI_FALSE; + } + ReleaseDC(NULL, screenDC); + for (int i = 0; i < 256; i ++) { + redPtr[i] = (jint) currentGamma[i]; + greenPtr[i] = (jint) currentGamma[i + 256]; + bluePtr[i] = (jint) currentGamma[i + 512]; + } + env->ReleaseIntArrayElements(red, redPtr, 0); + env->ReleaseIntArrayElements(green, greenPtr, 0); + env->ReleaseIntArrayElements(blue, bluePtr, 0); + return JNI_TRUE; } /* * Class: org_lwjgl_Display * Method: setGammaRamp - * Signature: ([I)V + * Signature: ([I[I[I)V */ -JNIEXPORT void JNICALL Java_org_lwjgl_Display_setGammaRamp - (JNIEnv * env, jclass clazz, jintArray gamma) +JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_setGammaRamp + (JNIEnv * env, jclass clazz, jintArray red, jintArray green, jintArray blue) { +#ifdef _DEBUG + if (red == NULL) { + throwRuntimeException(env, "Null red array."); + return JNI_FALSE; + } + if (green == NULL) { + throwRuntimeException(env, "Null green array."); + return JNI_FALSE; + } + if (blue == NULL) { + throwRuntimeException(env, "Null blue array."); + return JNI_FALSE; + } + if (env->GetArrayLength(red) != 256) { + throwRuntimeException(env, "Red array is not 256 long."); + return JNI_FALSE; + } + if (env->GetArrayLength(green) != 256) { + throwRuntimeException(env, "Green array is not 256 long."); + return JNI_FALSE; + } + if (env->GetArrayLength(blue) != 256) { + throwRuntimeException(env, "Blue array is not 256 long."); + return JNI_FALSE; + } +#endif + + jint * redPtr = env->GetIntArrayElements(red, NULL); + jint * greenPtr = env->GetIntArrayElements(green, NULL); + jint * bluePtr = env->GetIntArrayElements(blue, NULL); + + // Turn array of ints into array of RGB WORDs + WORD newGamma[768]; + for (int i = 0; i < 256; i ++) { + newGamma[i] = (WORD)(min(0x00010000, redPtr[i])); + newGamma[i + 256] = (WORD)(min(0x00010000, greenPtr[i])); + newGamma[i + 512] = (WORD)(min(0x00010000, bluePtr[i])); + } + jboolean ret; + HDC screenDC = GetDC(NULL); + if (SetDeviceGammaRamp(screenDC, newGamma) == FALSE) { +#ifdef _DEBUG + printf("Failed to set device gamma\n"); +#endif + ret = JNI_FALSE; + } else { + ret = JNI_TRUE; + } + ReleaseDC(NULL, screenDC); + + env->ReleaseIntArrayElements(red, redPtr, JNI_ABORT); + env->ReleaseIntArrayElements(green, greenPtr, JNI_ABORT); + env->ReleaseIntArrayElements(blue, bluePtr, JNI_ABORT); + + return ret; } @@ -307,7 +421,7 @@ { // Determine the current screen resolution // Get the screen - HDC screenDC = CreateCompatibleDC(NULL); + HDC screenDC = GetDC(NULL); if (!screenDC) { printf("Couldn't get screen DC!\n"); return; @@ -319,7 +433,6 @@ int freq = GetDeviceCaps(screenDC, VREFRESH); if (freq <= 1) freq = 0; // Unknown - DeleteDC(screenDC); jclass jclass_DisplayMode = env->FindClass("org/lwjgl/DisplayMode"); jmethodID ctor = env->GetMethodID(jclass_DisplayMode, "<init>", "(IIII)V"); @@ -327,6 +440,14 @@ jfieldID fid_initialMode = env->GetStaticFieldID(clazz, "mode", "Lorg/lwjgl/DisplayMode;"); env->SetStaticObjectField(clazz, fid_initialMode, newMode); env->DeleteLocalRef(newMode); + + // Get the default gamma ramp + if (GetDeviceGammaRamp(screenDC, originalGamma) == FALSE) { +#ifdef _DEBUG + printf("Failed to get initial device gamma\n"); +#endif + } + ReleaseDC(NULL, screenDC); } Index: Window.h CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/native/win32/Window.h =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/native/win32/Window.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Window.h 28 Mar 2003 19:01:55 -0000 1.2 +++ Window.h 29 Mar 2003 21:52:14 -0000 1.3 @@ -88,4 +88,9 @@ */ WINDOW_H_API void throwException(JNIEnv * env, const char * err); + /* + * Utility function to throw a RuntimeException + */ + WINDOW_H_API void throwRuntimeException(JNIEnv * env, const char * err); + #endif /* _LWJGL_WINDOW_H_INCLUDED_ */ Index: org_lwjgl_Window.cpp CVS Browser: http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/java-game-lib/LWJGL/src/native/win32/org_lwjgl_Window.cpp =================================================================== RCS file: /cvsroot/java-game-lib/LWJGL/src/native/win32/org_lwjgl_Window.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- org_lwjgl_Window.cpp 28 Mar 2003 23:16:15 -0000 1.4 +++ org_lwjgl_Window.cpp 29 Mar 2003 21:52:14 -0000 1.5 @@ -66,6 +66,16 @@ } /* + * Utility function to throw a RuntimeException + */ +void throwRuntimeException(JNIEnv * env, const char * err) +{ + jclass cls = env->FindClass("java/lang/RuntimeException"); + env->ThrowNew(cls, err); + env->DeleteLocalRef(cls); +} + +/* * Create DirectInput. * Returns true for success, or false for failure */ |