简体   繁体   English

在Objective-C或C ++中获取OS X上有效全屏分辨率的方法?

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

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; 我找不到任何方法为Mac OS X做这个;

Like in the system preferences Displays pane. 就像在系统首选项 Displays窗格中一样。

在此输入图像描述

Is it possible? 可能吗?

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 看看苹果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. 关于OP对于想要所有可能的显示分辨率的评论。

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. 测试项目使用ARC并强制__bridgeges ..但我再次确信你将有时间更好地编写代码。

My reference was Quartz Display Services Reference 我的参考是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 ) - > display deatails = ( 2560x1440, 1280x720, 640x480, 800x600, 1024x768, 1280x1024, 1344x756, 1600x900, 1680x1050, 1920x1080, 1600x1200, 1920x1200 )

In C++ http://specialmeaning.blogspot.com/2016/07/yes-apple-i-did-it.html 在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;

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM