3

I'm making a game and I'd like to get a list of valid fullscreen resolutions for the launcher. I can't find any way of doing this for Mac OS X;

Like in the system preferences Displays pane.

enter image description here

Is it possible?

markhunte
  • 6,805
  • 2
  • 25
  • 44
Hinchy
  • 87
  • 8

2 Answers2

5

If you mean get the Display screen resolutions.

This may be what you are after.

NSScreen* thescreen;
id theScreens = [NSScreen screens];

for (thescreen in theScreens) {
  NSLog(@"%@x%@",  [NSNumber numberWithFloat:[thescreen frame].size.width],   [NSNumber numberWithFloat:[thescreen frame].size.height]);
}

This example will give you the set resolutions of all displays

Have a look at apples NSScreen

If this is not quite what you are after can you expand your question.

Cheers


  • UPDATE. in regard to comment from OP on wanting all possible display resolutions.

This maybe what you are after and you will have to play with it to see if it is indeed returning the correct info. I was getting multiple results hence the filter. But if you play with it you should be able to thin it down.

The test project was using ARC and it forced the __bridges.. But again I am sure you will have time to code it all better.

My reference was Quartz Display Services Reference

NSArray* theref  =  (__bridge NSArray *)(CGDisplayCopyAllDisplayModes ( CGMainDisplayID(), nil ));

NSMutableArray * rezes = [[NSMutableArray  alloc]init];

for (id aMode  in theref) {
  CGDisplayModeRef  thisMode = (__bridge CGDisplayModeRef)(aMode);
  size_t theWidth = CGDisplayModeGetWidth( thisMode );
  size_t theHeight = CGDisplayModeGetHeight( thisMode );
  NSString *theRez = [NSString stringWithFormat:@"%zux%zu",theWidth,theHeight];

  if (![rezes containsObject:theRez]) {
    [rezes addObject:theRez];
  }
}

NSLog(@" display deatails = %@", rezes);

--> display deatails = ( 2560x1440, 1280x720, 640x480, 800x600, 1024x768, 1280x1024, 1344x756, 1600x900, 1680x1050, 1920x1080, 1600x1200, 1920x1200 )

Mark
  • 7,167
  • 4
  • 44
  • 68
markhunte
  • 6,805
  • 2
  • 25
  • 44
  • I meant a list of any valid screen resolutions supported at the OS level - like, when I go into the system preferences, I can see that my display supports several fullscreen resolutions. I want to get those – Hinchy Sep 24 '13 at 11:34
  • yes, this updated version is exactly what I needed. thank you so much!! :) – Hinchy Sep 25 '13 at 04:42
  • @Mark how to sort the screen resolutions ? – jarvis12 Apr 12 '19 at 07:22
  • @jarvis12 what do you mean?. if you mean by list order then there will likely be a sort command in Objective -C or what ever language you are using.. – markhunte Apr 15 '19 at 18:32
  • @markhunte ok and have tried to pass `kCGDisplayShowDuplicateLowResolutionModes` instead of nil in `CGDisplayCopyAllDisplayModes ` ? It said to get all possible resolutions but i am getting same as if i pass `nil`. – jarvis12 Apr 16 '19 at 04:18
  • @jarvis12 looks like you are already talking to the person who put an up an example using this 'reserved for future use' option, so best you keep with them on that.. https://stackoverflow.com/a/53161200/261305 – markhunte Apr 21 '19 at 09:07
1

In C++ http://specialmeaning.blogspot.com/2016/07/yes-apple-i-did-it.html

#include <iostream>
#include <CoreGraphics/CoreGraphics.h>

int main(int argc, const char * argv[]) {
    // insert code here...
    auto mainDisplayId = CGMainDisplayID();

    std::cout << "Current resolution was "
    << CGDisplayPixelsWide(mainDisplayId) << 'x'
    << CGDisplayPixelsHigh(mainDisplayId) << std::endl
    << "Supported resolution modes:";

    auto modes = CGDisplayCopyAllDisplayModes(mainDisplayId, nullptr);
    auto count = CFArrayGetCount(modes);
    CGDisplayModeRef mode;
    for(auto c=count;c--;){
        mode = (CGDisplayModeRef)CFArrayGetValueAtIndex(modes, c);
        auto w = CGDisplayModeGetWidth(mode);
        auto h = CGDisplayModeGetHeight(mode);
        std::cout << std::endl << w << 'x' << h;
    }
    CGDisplaySetDisplayMode(mainDisplayId, mode, nullptr);
    std::cout << " is the selected top one." << std::endl;
    std::cin.get();
    return 0;

}
Sergei Krivonos
  • 4,217
  • 3
  • 39
  • 54