Revamped architecture replacing AGL nastiness with cleaner CGL API

This commit is contained in:
Gregory Pierce 2003-05-08 00:53:10 +00:00
parent 06cf180b71
commit 647e1b2b29
4 changed files with 44 additions and 210 deletions

View File

@ -41,189 +41,76 @@
#include "RenderingContext.h" #include "RenderingContext.h"
#define kMaxDisplays 16
CGDirectDisplayID display[kMaxDisplays];
RenderingContext::RenderingContext() RenderingContext::RenderingContext()
{ {
} }
bool RenderingContext::createDisplay( int width, int height, int bpp, int freq ) bool RenderingContext::createDisplay( int width, int height, int bpp, int freq )
{ {
InitCursor(); printf("Creating display");
SetRect( &rect, 0, 0, width, height ); CGDisplayCapture( kCGDirectMainDisplay ) ;
windowPtr = NewCWindow( NULL, &rect, "LWJGL", true, kWindowShadowDialogProc, (WindowPtr) -1L, true, 0L ); CGDisplaySwitchToMode( kCGDirectMainDisplay,
CGDisplayBestModeForParameters( kCGDirectMainDisplay,
bpp, width, height, freq ) ) ;
SetPortWindowPort( windowPtr ); CGOpenGLDisplayMask displayMask = CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay ) ;
CGLPixelFormatAttribute attribs[] =
if ( windowPtr == NULL )
{ {
printf("Failed to create a window\n"); kCGLPFAFullScreen,
return false; kCGLPFADisplayMask,
} displayMask,
NULL
} ;
CGLPixelFormatObj pixelFormatObj ;
long numPixelFormats ;
CGLChoosePixelFormat( attribs, &pixelFormatObj, &numPixelFormats );
long swapInterval ;
CGLCreateContext( pixelFormatObj, NULL, &contextObj ) ;
CGLDestroyPixelFormat( pixelFormatObj ) ;
swapInterval = 1 ;
CGLSetParameter( contextObj, kCGLCPSwapInterval, &swapInterval ) ;
CGLSetCurrentContext( contextObj ) ;
CGLSetFullScreen( contextObj ) ;
ShowWindow( windowPtr );
return true; return true;
} }
void RenderingContext::destroyDisplay() void RenderingContext::destroyDisplay()
{ {
// cleanup the AGL context CGLClearDrawable( contextObj ) ;
// CGLDestroyContext( contextObj ) ;
aglSetCurrentContext(NULL); CGReleaseAllDisplays();
aglSetDrawable(aglContext, NULL);
aglDestroyContext(aglContext);
// cleanup the window
//
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 )
{
AGLPixelFormat fmt;
GLboolean ok;
GLint attrib[] = { AGL_RGBA, AGL_NONE };
if ( extgl_Open() != 0 )
{
printf("extgl_Open failed");
return false;
}
// Choose an rgb pixel format
//
fmt = aglChoosePixelFormat(NULL, 0, attrib);
if(fmt == NULL)
{
return false;
}
// Create an AGL context
//
aglContext = aglCreateContext(fmt, NULL);
if( aglContext == NULL)
{
return false;
}
// Attach the window to the context
//
ok = aglSetDrawable(aglContext, GetWindowPort(windowPtr) );
if(!ok)
{
return false;
}
// Make the context the current context
//
ok = aglSetCurrentContext(aglContext);
if(!ok)
{
return false;
}
if ( extgl_Initialize() != 0 )
{
printf("Failed to initialize GL [extgl_Initialize()]\n");
return false;
}
// Pixel format is no longer needed
//
aglDestroyPixelFormat(fmt);
#ifdef _DEBUG
char * p = (char * ) glGetString( GL_EXTENSIONS );
if ( NULL == p )
{
printf("NO extensions available");
}
else
{
printf("Available extensions:\n%s\n", p);
}
#endif
return true;
}
void RenderingContext::destroyGL()
{
// clear out the current rendering context
//
aglSetCurrentContext( NULL );
// destroy the context
//
aglDestroyContext( aglContext );
// close the gl extension context
//
extgl_Close();
} }
void RenderingContext::swap() void RenderingContext::swap()
{ {
// swap the rendering buffer // swap the rendering buffer
// //
aglSwapBuffers( aglContext ); CGLFlushDrawable( contextObj )
} }
void RenderingContext::makeContextCurrent() void RenderingContext::makeContextCurrent()
{ {
// make the current context the one we have stored // make the current context the one we have stored
// //
aglSetCurrentContext( aglContext ); CGLSetCurrentContext( contextObj ) ;
} }
void RenderingContext::releaseContext() void RenderingContext::releaseContext()
{ {
// release the context // release the context
// //
aglSetCurrentContext( NULL ); CGLSetCurrentContext( NULL ) ;
} }
RenderingContext::~RenderingContext() RenderingContext::~RenderingContext()

View File

@ -44,13 +44,12 @@
#include "extgl.h" #include "extgl.h"
#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h> #include <ApplicationServices/ApplicationServices.h>
class RenderingContext class RenderingContext
{ {
public: public:
AGLContext aglContext; CGLContextObj contextObj;
WindowPtr windowPtr; WindowPtr windowPtr;
Rect rect; Rect rect;
@ -60,8 +59,6 @@ public:
bool createDisplay( int width, int height, int bpp, int freq ); bool createDisplay( int width, int height, int bpp, int freq );
void destroyDisplay(); void destroyDisplay();
CGDirectDisplayID * enumerateDisplays();
CFArrayRef enumerateDisplayModes( CGDirectDisplayID display );
bool createGL( int colorBits, int alphaBits, int depthBits, int stencilBits ); bool createGL( int colorBits, int alphaBits, int depthBits, int stencilBits );
void destroyGL(); void destroyGL();

View File

@ -36,20 +36,6 @@
RenderingContext * renderingContext; 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 * Class: org_lwjgl_Display
* Method: getAvailableDisplayModes * Method: getAvailableDisplayModes
@ -58,54 +44,23 @@ static int numberForKey( CFDictionaryRef desc, CFStringRef key )
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes
(JNIEnv * env, jclass clazz) (JNIEnv * env, jclass clazz)
{ {
CGDirectDisplayID * displays = renderingContext->enumerateDisplays();
CFArrayRef modeList = renderingContext->enumerateDisplayModes( displays[0] );
// count the display modes printf("Getting default display mode - 1024x768x32");
//
int cnt = CFArrayGetCount( modeList );
// Allocate an array of DisplayModes big enough // Allocate an array of DisplayModes big enough
jclass displayModeClass = env->FindClass("org/lwjgl/DisplayMode"); 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(1, displayModeClass, NULL);
jobjectArray ret = env->NewObjectArray(cnt * 16, displayModeClass, NULL);
jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "<init>", "(IIIIIII)V"); jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "<init>", "(IIIIIII)V");
CFDictionaryRef mode; jobject displayMode = env->NewObject(displayModeClass, displayModeConstructor,
for ( int i=0; i< cnt; i++ ) 1024, 768, 32, 0,
{ 0,0,0 );
mode = CFArrayGetValueAtIndex( modeList, i );
int width = numberForKey( mode, kCGDisplayWidth ); env->SetObjectArrayElement( ret, 0, displayMode );
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 ) return ret;
{
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 );
}
}
}
}
}
} }
/* /*
@ -116,10 +71,6 @@ JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate
(JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jboolean debug) (JNIEnv * env, jclass clazz, jint width, jint height, jint bpp, jint freq, jboolean debug)
{ {
#ifdef _DEBUG
printf("Creating display: size %dx%d %dhz %dbpp...\n", width, height, freq, bpp);
#endif
renderingContext = new RenderingContext(); renderingContext = new RenderingContext();
renderingContext->createDisplay( width, height, bpp, freq ); renderingContext->createDisplay( width, height, bpp, freq );
@ -128,9 +79,7 @@ JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate
jfieldID fid_handle = env->GetStaticFieldID(clazz, "handle", "I"); jfieldID fid_handle = env->GetStaticFieldID(clazz, "handle", "I");
env->SetStaticIntField(clazz, fid_handle, (jint) renderingContext->windowPtr ); env->SetStaticIntField(clazz, fid_handle, (jint) renderingContext->windowPtr );
#ifdef _DEBUG
printf("Display created\n"); printf("Display created\n");
#endif
return JNI_TRUE; return JNI_TRUE;
} }

View File

@ -130,6 +130,7 @@ JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setTime
JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setProcessPriority JNIEXPORT void JNICALL Java_org_lwjgl_Sys_setProcessPriority
(JNIEnv * env, jclass clazz, jint priority) (JNIEnv * env, jclass clazz, jint priority)
{ {
printf("Not supported on OSX \n");
} }
/* /*