From 529748c7b761cfdd2275ded1dcd65263bca045f4 Mon Sep 17 00:00:00 2001 From: Gregory Pierce Date: Thu, 30 Jan 2003 18:56:56 +0000 Subject: [PATCH] Added changes to support the selection of display modes on OSX in multiple monitor configurations. Currently defaults to only getting the display modes of display[0] - the primary display since lwjgl doesn't yet support clean multi monitor configuration programatically. This code based off the Apple provided sample code at http://developer.apple.com/samplecode/Sample_Code/Graphics_2D/Mode.htm --- src/native/macosx/RenderingContext.cpp | 39 +++++++++++ src/native/macosx/RenderingContext.h | 3 + src/native/macosx/org_lwjgl_Display.cpp | 67 ++++++++++++++++++- src/native/macosx/org_lwjgl_opengl_BaseGL.cpp | 3 - 4 files changed, 108 insertions(+), 4 deletions(-) diff --git a/src/native/macosx/RenderingContext.cpp b/src/native/macosx/RenderingContext.cpp index 0496af35..0c7cb5cd 100644 --- a/src/native/macosx/RenderingContext.cpp +++ b/src/native/macosx/RenderingContext.cpp @@ -41,6 +41,10 @@ #include "RenderingContext.h" +#define kMaxDisplays 16 + +CGDirectDisplayID display[kMaxDisplays]; + RenderingContext::RenderingContext() { } @@ -79,6 +83,41 @@ void RenderingContext::destroyDisplay() DisposeWindow( windowPtr ); } +CGDirectDisplayID * RenderingContext::enumerateDisplays() +{ + + CGDisplayCount numDisplays; + CGDisplayErr err; + + err = CGGetActiveDisplayList( kMaxDisplays, + display, + &numDisplays ); + + if ( err != CGDisplayNoErr ) + { + printf("Cannot get displays (%d). \n", err ); + } + + return display; +} + +CFArrayRef RenderingContext::enumerateDisplayModes( CGDirectDisplayID display ) +{ + CFArrayRef modeList; + + modeList = CGDisplayAvailableModes( display ); + if ( modeList = NULL ) + { + printf("Error. Display requested is invalid.\n"); + return NULL; + } + + return modeList; +} + + + + bool RenderingContext::createGL( int colorBits, int alphaBits, int depthBits, int stencilBits ) { diff --git a/src/native/macosx/RenderingContext.h b/src/native/macosx/RenderingContext.h index c7d67e97..78715567 100644 --- a/src/native/macosx/RenderingContext.h +++ b/src/native/macosx/RenderingContext.h @@ -45,6 +45,7 @@ #include "extgl.h" #include +#include class RenderingContext { @@ -59,6 +60,8 @@ public: bool createDisplay( int width, int height, int bpp, int freq ); void destroyDisplay(); + CGDirectDisplayID * enumerateDisplays(); + CFArrayRef enumerateDisplayModes( CGDirectDisplayID display ); bool createGL( int colorBits, int alphaBits, int depthBits, int stencilBits ); void destroyGL(); diff --git a/src/native/macosx/org_lwjgl_Display.cpp b/src/native/macosx/org_lwjgl_Display.cpp index eae38d04..a475bcf8 100644 --- a/src/native/macosx/org_lwjgl_Display.cpp +++ b/src/native/macosx/org_lwjgl_Display.cpp @@ -36,6 +36,20 @@ RenderingContext * renderingContext; +static int numberForKey( CFDictionaryRef desc, CFStringRef key ) +{ + CFNumberRef value; + int num = 0; + + if ( (value = CFDictionaryGetValue(desc, key)) == NULL ) + { + return 0; + } + + CFNumberGetValue( value, kCFNumberIntType, &num ); + return num; +} + /* * Class: org_lwjgl_Display * Method: getAvailableDisplayModes @@ -44,7 +58,54 @@ RenderingContext * renderingContext; JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes (JNIEnv * env, jclass clazz) { - return NULL; + CGDirectDisplayID * displays = renderingContext->enumerateDisplays(); + CFArrayRef modeList = renderingContext->enumerateDisplayModes( displays[0] ); + + // count the display modes + // + int cnt = CFArrayGetCount( modeList ); + + // Allocate an array of DisplayModes big enough + jclass displayModeClass = env->FindClass("org/lwjgl/DisplayMode"); + + // Note the * 16 - this is because we are manufacturing available alpha/depth/stencil combos. + jobjectArray ret = env->NewObjectArray(cnt * 16, displayModeClass, NULL); + jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "", "(IIIIIII)V"); + + CFDictionaryRef mode; + for ( int i=0; i< cnt; i++ ) + { + mode = CFArrayGetValueAtIndex( modeList, i ); + + int width = numberForKey( mode, kCGDisplayWidth ); + int height = numberForKey( mode, kCGDisplayHeight ); + int bpp = numberForKey( mode, kCGDisplayBitsPerPixel ); + int refreshRate = numberForKey( mode, kCGDisplayRefreshRate ); + + if ( bpp <= 8 ) + { + continue; + } + else + { + jobject displayMode; + + for ( int depthBits = 0; depthBits <= 24; depthBits += 8 ) + { + for ( int stencilBits = 0; stencilBits <= 8; stencilBits += 8 ) + { + for ( int alphaBits = 0; alphaBits <= 8; alphaBits += 8 ) + { + displayMode = env->NewObject(displayModeClass, displayModeConstructor, + width, height, bpp, refreshRate, + alphaBits, depthBits, stencilBits ); + + env->SetObjectArrayElement( ret, i, displayMode ); + } + } + } + } + } } /* @@ -67,6 +128,10 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate jfieldID fid_handle = env->GetStaticFieldID(clazz, "handle", "I"); env->SetStaticIntField(clazz, fid_handle, (jint) renderingContext->windowPtr ); +#ifdef _DEBUG + printf("Display created\n"); +#endif + return JNI_TRUE; } diff --git a/src/native/macosx/org_lwjgl_opengl_BaseGL.cpp b/src/native/macosx/org_lwjgl_opengl_BaseGL.cpp index 5c68f150..ab830568 100644 --- a/src/native/macosx/org_lwjgl_opengl_BaseGL.cpp +++ b/src/native/macosx/org_lwjgl_opengl_BaseGL.cpp @@ -42,9 +42,6 @@ #include "RenderingContext.h" #include "org_lwjgl_opengl_BaseGL.h" - - - /* * Class: org_lwjgl_opengl_BaseGL * Method: nCreate