Replaced deprecated CoreGraphics calls in video mode enumeration.

This commit is contained in:
Camilla Berglund 2012-03-05 16:27:53 +01:00
parent c0db61bd6e
commit 7af82fdade
4 changed files with 69 additions and 35 deletions

View File

@ -312,6 +312,7 @@ version of GLFW.</p>
<li>[Cocoa] Added support for joysticks</li>
<li>[Cocoa] Postponed menu creation to first window creation</li>
<li>[Cocoa] Replaced <code>NSDate</code> time source with <code>mach_absolute_time</code></li>
<li>[Cocoa] Replaced deprecated CoreGraphics calls in video mode enumeration</li>
<li>[Cocoa] Bugfix: The loop condition for saving video modes used the wrong index variable</li>
<li>[Cocoa] Bugfix: The OpenGL framework was not retrieved, making glfwGetProcAddress crash</li>
<li>[Cocoa] Bugfix: <code>glfwInit</code> changed the current directory for unbundled executables</li>

View File

@ -34,38 +34,59 @@
// Check whether the display mode should be included in enumeration
//========================================================================
static BOOL modeIsGood(NSDictionary* mode)
static GLboolean modeIsGood(CGDisplayModeRef mode)
{
// This is a bit controversial, if you've got something other than an
// LCD computer monitor as an output device you might not want these
// checks. You might also want to reject modes which are interlaced,
// or TV out. There is no one-size-fits-all policy that can work here.
// This seems like a decent compromise, but certain applications may
// wish to patch this...
return [[mode objectForKey:(id)kCGDisplayBitsPerPixel] intValue] >= 15 &&
[mode objectForKey:(id)kCGDisplayModeIsSafeForHardware] != nil &&
[mode objectForKey:(id)kCGDisplayModeIsStretched] == nil;
uint32_t flags = CGDisplayModeGetIOFlags(mode);
if (!(flags & kDisplayModeValidFlag) || !(flags & kDisplayModeSafeFlag))
return GL_FALSE;
if (flags & kDisplayModeInterlacedFlag)
return GL_FALSE;
if (flags & kDisplayModeTelevisionFlag)
return GL_FALSE;
if (flags & kDisplayModeStretchedFlag)
return GL_FALSE;
CFStringRef format = CGDisplayModeCopyPixelEncoding(mode);
if (CFStringCompare(format, CFSTR(IO16BitDirectPixels), 0) &&
CFStringCompare(format, CFSTR(IO32BitDirectPixels), 0))
{
CFRelease(format);
return GL_FALSE;
}
CFRelease(format);
return GL_TRUE;
}
//========================================================================
// Convert Core Graphics display mode to GLFW video mode
//========================================================================
static GLFWvidmode vidmodeFromCGDisplayMode(NSDictionary* mode)
static GLFWvidmode vidmodeFromCGDisplayMode(CGDisplayModeRef mode)
{
unsigned int width =
[[mode objectForKey:(id)kCGDisplayWidth] unsignedIntValue];
unsigned int height =
[[mode objectForKey:(id)kCGDisplayHeight] unsignedIntValue];
unsigned int bps =
[[mode objectForKey:(id)kCGDisplayBitsPerSample] unsignedIntValue];
GLFWvidmode result;
result.width = width;
result.height = height;
result.redBits = bps;
result.greenBits = bps;
result.blueBits = bps;
result.width = CGDisplayModeGetWidth(mode);
result.height = CGDisplayModeGetHeight(mode);
CFStringRef format = CGDisplayModeCopyPixelEncoding(mode);
if (CFStringCompare(format, CFSTR(IO16BitDirectPixels), 0) == 0)
{
result.redBits = 5;
result.greenBits = 5;
result.blueBits = 5;
}
else
{
result.redBits = 8;
result.greenBits = 8;
result.blueBits = 8;
}
CFRelease(format);
return result;
}
@ -80,17 +101,23 @@ static GLFWvidmode vidmodeFromCGDisplayMode(NSDictionary* mode)
int _glfwPlatformGetVideoModes(GLFWvidmode* list, int maxcount)
{
NSArray* modes = (NSArray*) CGDisplayAvailableModes(CGMainDisplayID());
unsigned int i, j = 0, n = [modes count];
CGDisplayModeRef mode;
CFArrayRef modes;
CFIndex count, i;
int stored = 0;
for (i = 0; i < n && j < (unsigned)maxcount; i++)
modes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), NULL);
count = CFArrayGetCount(modes);
for (i = 0; i < count && stored < maxcount; i++)
{
NSDictionary *mode = [modes objectAtIndex:i];
mode = (CGDisplayModeRef) CFArrayGetValueAtIndex(modes, i);
if (modeIsGood(mode))
list[j++] = vidmodeFromCGDisplayMode(mode);
list[stored++] = vidmodeFromCGDisplayMode(mode);
}
return j;
CFRelease(modes);
return stored;
}
//========================================================================

View File

@ -91,8 +91,7 @@ int _glfwPlatformInit(void)
changeToResourcesDirectory();
_glfwLibrary.NS.desktopMode =
(NSDictionary*) CGDisplayCurrentMode(CGMainDisplayID());
_glfwLibrary.NS.desktopMode = CGDisplayCopyDisplayMode(CGMainDisplayID());
// Save the original gamma ramp
_glfwLibrary.originalRampSize = CGDisplayGammaTableCapacity(CGMainDisplayID());
@ -117,6 +116,12 @@ int _glfwPlatformTerminate(void)
// Restore the original gamma ramp
_glfwPlatformSetGammaRamp(&_glfwLibrary.originalRamp);
if (_glfwLibrary.NS.desktopMode)
{
CFRelease(_glfwLibrary.NS.desktopMode);
_glfwLibrary.NS.desktopMode = NULL;
}
[NSApp setDelegate:nil];
[_glfwLibrary.NS.delegate release];
_glfwLibrary.NS.delegate = nil;

View File

@ -37,6 +37,7 @@
#if defined(__OBJC__)
#import <Cocoa/Cocoa.h>
#else
#include <ApplicationServices/ApplicationServices.h>
typedef void* id;
#endif
@ -91,7 +92,7 @@ typedef struct _GLFWlibraryNS
// dlopen handle for dynamically loading OpenGL extension entry points
void* OpenGLFramework;
id desktopMode;
CGDisplayModeRef desktopMode;
id delegate;
id autoreleasePool;
} _GLFWlibraryNS;