method of getting valid fullscreen resolutions on OS X in Objective-C or C++?

时光怂恿深爱的人放手 提交于 2019-12-11 13:36:50

问题


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.

Is it possible?


回答1:


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 )




回答2:


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;

}


来源:https://stackoverflow.com/questions/18930230/method-of-getting-valid-fullscreen-resolutions-on-os-x-in-objective-c-or-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!