PDA

View Full Version : best way to enumerate resolutions


Rasterman
2007.07.24, 12:31 PM
I am using GLUT and intend on using glutEnterGameMode to switch to fullscreen, unfortunately GLUT doesn't provide what resolutions are valid.

I was wondering what is the best way to enumerate possible resolutions? Currently I am using a brute force method by calling BeginFullScreen with the fullScreenPreflightSize flag, but this is hacky and doesn't give the refresh rates.

Frank C.
2007.07.24, 01:38 PM
CGDisplayAvailableModes (http://developer.apple.com/documentation/GraphicsImaging/Reference/Quartz_Services_Ref/Reference/reference.html) (and CGGetActiveDisplayList to get a list of displays if yer so inclined)

AnotherJake
2007.07.24, 01:51 PM
Yeah, honestly, this has got to be my *least* favorite thing to do. I personally think Apple could have done a better job and made this task a little easier to accomplish. I'm sorry, but I don't have any Carbon code at hand for this at the moment, but as Frank said, you'll have to use CG to do it. Here's some code that I haphazardly cut snippets from my own crap in Cocoa to maybe at least give you a hint of what direction to take. The NSDictionary and array stuff should be toll-free bridged to the equivalent CF stuff so it should translate directly from Cocoa into Carbon routines without much hassle (except for the NSEnumerator, but that's just a Cocoa shortcut for a proper for loop). Note that if you're only allowing full screen on the main display you won't need to enumerate through all displays as I do. I don't know what I'm leaving out or what will or won't be helpful but you can look at it

(NOTE AGAIN: this is not a proper code snippet, I just cut out stuff you might find helpful):
OSErr err;
CGDirectDisplayID *activeDisplayList;
CGDisplayCount numDisplays;
int i;
NSMutableString *displayName;
NSArray *availableModes, *resolutions;
NSMutableArray *usableModes;
NSDictionary *mode, *currMode;
long currBitsPerPixel, bitsPerPixel;
BOOL safeForHardware;

err = CGGetActiveDisplayList(0, NULL, &numDisplays);
if (err != noErr)
FatalError("Unable to determine the number of active displays.");
activeDisplayList = (CGDirectDisplayID*)malloc(numDisplays * sizeof(CGDirectDisplayID));
err = CGGetActiveDisplayList(numDisplays, activeDisplayList, &numDisplays);
if (err != noErr)
FatalError("Unable to obtain the list of active displays.");
for (i = 0; i < numDisplays; i++)
{
displayName = [[[NSMutableString alloc] init] autorelease];
if (numDisplays > 1)
[displayName appendFormat:@"%d - ", i + 1];
[displayName appendString:[self getDisplayNameUsingID:activeDisplayList[i]]];
currMode = (NSDictionary *)CGDisplayCurrentMode(activeDisplayList[i]);
currBitsPerPixel = [[currMode objectForKey:(NSString *)kCGDisplayBitsPerPixel] longValue];
availableModes = (NSArray *)CGDisplayAvailableModes(activeDisplayList[i]);
usableModes = [[[NSMutableArray alloc] init] autorelease];
enumerator = [availableModes objectEnumerator];
while (mode = [enumerator nextObject])
{
bitsPerPixel = [[mode objectForKey:(NSString *)kCGDisplayBitsPerPixel] longValue];
safeForHardware = [[mode objectForKey:(NSString *)kCGDisplayModeIsSafeForHardware] boolValue];
if ((bitsPerPixel != currBitsPerPixel) || !safeForHardware)
continue;
[usableModes addObject:mode];
}
resolutions = [self constructResolutionArrayFromModes:usableModes];
}

Rasterman
2007.07.24, 02:39 PM
Thanks, a google search of CGDisplayAvailableModes yeilded some copy and paste code from

http://www.carbondev.com/site/?page=CGDisplayAvailableModes