I'm making a C++ game, and I want it to automatically get the user's desktop resolution.
I've found windows-only solutions so far - is there a way/library to find the resolution on Windows/Mac/Linux?
I'm making a C++ game, and I want it to automatically get the user's desktop resolution.
I've found windows-only solutions so far - is there a way/library to find the resolution on Windows/Mac/Linux?
There are several libraries helping you at getting the low-level stuff to work out. You'll probably need more stuff on those platforms anyways, so I'll list some:
GLFW provides a crossplatform (for Windows, Mac, and Linux) way to get the desktop video mode. It is a C api, but it will work in C++. The relevant function (and documentation) is here:
void glfwGetDesktopMode( GLFWvidmode *mode )
Parameters
mode
Pointer to a GLFWvidmode
structure, which will be filled out by the function.
Return values
The GLFWvidmode
structure pointed to by mode is filled out with the desktop video mode.
Description
This function returns the desktop video mode in a GLFWvidmode
structure. See glfwGetVideoModes
for a definition of the GLFWvidmode
structure.
Notes
The color depth of the desktop display is always reported as the number of bits for each individual color component (red, green and blue), even if the desktop is not using an RGB or RGBA color format. For instance, an indexed 256 color display may report RedBits = 3, GreenBits = 3 and BlueBits = 2, which adds up to 8 bits in total.
The desktop video mode is the video mode used by the desktop at the time the GLFW window was opened, not the current video mode (which may differ from the desktop video mode if the GLFW window is a fullscreen window).
typedef struct {
int Width, Height; // Video resolution
int RedBits; // Number of red bits
int GreenBits; // Number of green bits
int BlueBits; // Number of blue bits
} GLFWvidmode;
See Jonas Wielicki's answer for alternatives.
I did a cross-platform c++ function:
#if WIN32
#include <windows.h>
#else
#include <X11/Xlib.h>
#endif
//...
void getScreenResolution(int &width, int &height) {
#if WIN32
width = (int) GetSystemMetrics(SM_CXSCREEN);
height = (int) GetSystemMetrics(SM_CYSCREEN);
#else
Display* disp = XOpenDisplay(NULL);
Screen* scrn = DefaultScreenOfDisplay(disp);
width = scrn->width;
height = scrn->height;
#endif
}
int main() {
int width, height;
getScreenResolution(width, height);
printf("Screen resolution: %dx%d\n", width, height);
}