hi folks,
here is the fix to get a fullscreen context under mac os x!
this is an almost clean solution. the only dirty part is that i don’t know for sure if you really want to be fullscreen. the decision is made on the size of the window that wants the context:
if the window is exactly the size of the current screens resolution, a fullscreen context is generated, otherwise a view-based context is built.
all changes i did begin with a commented ‘atze:’
void* createContext(void* shareContext, void* view,
int redBits,
int greenBits,
int blueBits,
int alphaBits,
int depthBits,
int stencilBits,
int accumRedBits,
int accumGreenBits,
int accumBlueBits,
int accumAlphaBits,
int sampleBuffers,
int numSamples)
{
if (gAutoreleasePool == nil) // atze: create a pool as early as possible
gAutoreleasePool = [[NSAutoreleasePool alloc] init];
int colorSize = redBits + greenBits + blueBits;
int accumSize = accumRedBits + accumGreenBits + accumBlueBits;
NSOpenGLContext *nsChareCtx = (NSOpenGLContext*)shareContext;
NSView *nsView = (NSView*)view;
// atze: if the views window covers the entire screen make the gl-context fullscreen
NSWindow *aWindow = [nsView window];
NSRect aWindowFrame = [aWindow frame];
NSScreen *aScreen = [aWindow screen];
NSDictionary *aDictionary = [aScreen deviceDescription];
NSNumber *aDisplayNumber = [aDictionary objectForKey:@"NSScreenNumber"];
CGDirectDisplayID aDisplay = (CGDirectDisplayID)[aDisplayNumber longValue]; // this is kind of ugly
// atze: we can not use NSScreens size, because it's the hardware resolution
// atze: the GDDisplayPixelsXX returns the soft-resolution
size_t aScreenWidth = CGDisplayPixelsWide(aDisplay);
size_t aScreenHeight = CGDisplayPixelsHigh(aDisplay);
// atze: does the window cover the entire screen?
if(aScreenWidth == (size_t)NSWidth(aWindowFrame) && aScreenHeight == (size_t)NSHeight(aWindowFrame))
{
nsView = nil; // atze: ignore the view, go fullscreen!
}
/* atze: what are these checks for? just create a context, regardless of that views state.
if (nsView != nil)
{
NSRect frame = [nsView frame];
if ((frame.size.width == 0) || (frame.size.height == 0))
{
fprintf(stderr, "Error: view width or height == 0at \"%s:%s:%d\"\n", __FILE__, __FUNCTION__, __LINE__);
// the view is not ready yet
return NULL;
}
else if ([nsView lockFocusIfCanDraw] == NO)
{
fprintf(stderr, "Error: view not ready, cannot lock focus at \"%s:%s:%d\"\n", __FILE__, __FUNCTION__, __LINE__);
// the view is not ready yet
return NULL;
}
[nsView unlockFocus];
}
*/
NSOpenGLPixelFormatAttribute attribs[] =
{
((nsView == nil) ? NSOpenGLPFAFullScreen : NSOpenGLPFAWindow), // atze: allow a fullscreen context
NSOpenGLPFANoRecovery, YES,
NSOpenGLPFAAccelerated, YES,
NSOpenGLPFADoubleBuffer, YES,
NSOpenGLPFAColorSize, colorSize,
NSOpenGLPFAAlphaSize, alphaBits,
NSOpenGLPFADepthSize, depthBits,
NSOpenGLPFAStencilSize, stencilBits,
NSOpenGLPFAAccumSize, accumSize,
NSOpenGLPFASampleBuffers, sampleBuffers,
NSOpenGLPFASamples, numSamples,
NSOpenGLPFAScreenMask, (NSOpenGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(aDisplay), // atze: choosen screen
(NSOpenGLPixelFormatAttribute)0
};
NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attribs];
NSOpenGLContext* nsContext = [[NSOpenGLContext alloc] initWithFormat:fmt shareContext:nsChareCtx];
[fmt release];
[nsContext makeCurrentContext];
if(nsView != nil)
[nsContext setView:nsView];
else // atze: here comes the fullscreen context
[nsContext setFullScreen];
// [nsContext retain]; atze: nsContext is retained already
//fprintf(stderr, " nsContext=%p\n", nsContext);
return nsContext;
}