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"
#define kMaxDisplays 16
CGDirectDisplayID display[kMaxDisplays];
RenderingContext::RenderingContext()
{
}
bool RenderingContext::createDisplay( int width, int height, int bpp, int freq )
{
InitCursor();
printf("Creating display");
SetRect( &rect, 0, 0, width, height );
windowPtr = NewCWindow( NULL, &rect, "LWJGL", true, kWindowShadowDialogProc, (WindowPtr) -1L, true, 0L );
CGDisplayCapture( kCGDirectMainDisplay ) ;
CGDisplaySwitchToMode( kCGDirectMainDisplay,
CGDisplayBestModeForParameters( kCGDirectMainDisplay,
bpp, width, height, freq ) ) ;
SetPortWindowPort( windowPtr );
if ( windowPtr == NULL )
CGOpenGLDisplayMask displayMask = CGDisplayIDToOpenGLDisplayMask( kCGDirectMainDisplay ) ;
CGLPixelFormatAttribute attribs[] =
{
printf("Failed to create a window\n");
return false;
}
kCGLPFAFullScreen,
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;
}
void RenderingContext::destroyDisplay()
{
// cleanup the AGL context
//
CGLClearDrawable( contextObj ) ;
CGLDestroyContext( contextObj ) ;
aglSetCurrentContext(NULL);
aglSetDrawable(aglContext, NULL);
aglDestroyContext(aglContext);
CGReleaseAllDisplays();
// 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()
{
// swap the rendering buffer
//
aglSwapBuffers( aglContext );
CGLFlushDrawable( contextObj )
}
void RenderingContext::makeContextCurrent()
{
// make the current context the one we have stored
//
aglSetCurrentContext( aglContext );
CGLSetCurrentContext( contextObj ) ;
}
void RenderingContext::releaseContext()
{
// release the context
//
aglSetCurrentContext( NULL );
CGLSetCurrentContext( NULL ) ;
}
RenderingContext::~RenderingContext()

View File

@ -44,13 +44,12 @@
#include "extgl.h"
#include <Carbon/Carbon.h>
#include <ApplicationServices/ApplicationServices.h>
class RenderingContext
{
public:
AGLContext aglContext;
CGLContextObj contextObj;
WindowPtr windowPtr;
Rect rect;
@ -60,8 +59,6 @@ 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();

View File

@ -36,20 +36,6 @@
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
@ -58,54 +44,23 @@ static int numberForKey( CFDictionaryRef desc, CFStringRef key )
JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes
(JNIEnv * env, jclass clazz)
{
CGDirectDisplayID * displays = renderingContext->enumerateDisplays();
CFArrayRef modeList = renderingContext->enumerateDisplayModes( displays[0] );
// count the display modes
//
int cnt = CFArrayGetCount( modeList );
printf("Getting default display mode - 1024x768x32");
// 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);
jobjectArray ret = env->NewObjectArray(1, displayModeClass, NULL);
jmethodID displayModeConstructor = env->GetMethodID(displayModeClass, "<init>", "(IIIIIII)V");
CFDictionaryRef mode;
for ( int i=0; i< cnt; i++ )
{
mode = CFArrayGetValueAtIndex( modeList, i );
jobject displayMode = env->NewObject(displayModeClass, displayModeConstructor,
1024, 768, 32, 0,
0,0,0 );
int width = numberForKey( mode, kCGDisplayWidth );
int height = numberForKey( mode, kCGDisplayHeight );
int bpp = numberForKey( mode, kCGDisplayBitsPerPixel );
int refreshRate = numberForKey( mode, kCGDisplayRefreshRate );
env->SetObjectArrayElement( ret, 0, displayMode );
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 );
}
}
}
}
}
return ret;
}
/*
@ -116,10 +71,6 @@ JNIEXPORT jobjectArray JNICALL Java_org_lwjgl_Display_getAvailableDisplayModes
JNIEXPORT jboolean JNICALL Java_org_lwjgl_Display_nCreate
(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->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");
env->SetStaticIntField(clazz, fid_handle, (jint) renderingContext->windowPtr );
#ifdef _DEBUG
printf("Display created\n");
#endif
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
(JNIEnv * env, jclass clazz, jint priority)
{
printf("Not supported on OSX \n");
}
/*