I'd like to launch a fullscreen 3D C++ application in native resolution on mac. How can I retrieve the native screen resolution ?
-
Ideally, there should be an API function (e.g. `fullscreenize()`), which would do all the handling for you. Then you'd just use your window's size. – ulidtko Feb 07 '11 at 13:32
-
possible duplicate of [Objective C - how to get current screen resolution?](http://stackoverflow.com/questions/1868677/objective-c-how-to-get-current-screen-resolution) – John Parker Feb 07 '11 at 13:35
-
2does the solution need to be just C++, or is Obj-C acceptable? – outis Feb 07 '11 at 13:48
-
4@middaparka: That is not a duplicate. The OP clearly specifies C++- the other question clearly specifies Objective C. – Puppy Feb 07 '11 at 13:54
-
@DeadMG Good point - my bad. :-) – John Parker Feb 07 '11 at 13:58
2 Answers
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.

- 75,655
- 22
- 151
- 221
-
1NSScreen mainScreen will not necessarily return the "mainScreen" - the size seems to depend on other things... possibly the last selected window? At any rate it doesn't provide consistent results in practice – jheriko Feb 27 '12 at 11:25
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"
)

- 961
- 10
- 11