Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get screen resolution programmatically in OS X

I'd like to launch a fullscreen 3D C++ application in native resolution on mac. How can I retrieve the native screen resolution ?

like image 250
jrm Avatar asked Feb 07 '11 13:02

jrm


2 Answers

If you are looking for a multiplatform solution for both mac and windows

#include "ScreenSize.h"

#if WIN32
#include <windows.h>
#else
#include <CoreGraphics/CGDisplayConfiguration.h>
#endif

void ScreenSize::getScreenResolution(unsigned int& width, unsigned int& height) {
#if WIN32
    width = (int)GetSystemMetrics(SM_CXSCREEN);
    height = (int)GetSystemMetrics(SM_CYSCREEN);
#else
    auto mainDisplayId = CGMainDisplayID();
    width = CGDisplayPixelsWide(mainDisplayId);
    height = CGDisplayPixelsHigh(mainDisplayId);
#endif
}

Note: You also need to link the CoreGraphics framework to your project. If you are using cmake, link your needed framework like the following:

    target_link_libraries(${PROJECT_NAME}
        "-framework CoreGraphics"
        "-framework Foundation"
    )
like image 59
Mohammad f Avatar answered Nov 20 '22 09:11

Mohammad f


If you don't wish to use Objective C, get the display ID that you wish to display on (using e.g. CGMainDisplayID), then use CGDisplayPixelsWide and CGDisplayPixelsHigh to get the screen width and height, in pixels. See "Getting Information About Displays" for how to get other display information.

If you're willing to use a bit of Objective-C, simply use [[NSScreen mainScreen] frame].

Note that there are other concerns with full screen display, namely ensuring other applications don't do the same. Read "Drawing to the Full Screen" in Apple's OpenGL Programming Guide for more.

like image 24
outis Avatar answered Nov 20 '22 11:11

outis