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?
Change the screen resolutionStay in, or open, Display settings. Scroll to Scale and layout. Find Display resolution, and then choose an option.
In SDL2, use SDL_GetCurrentDisplayMode or SDL_GetDesktopDisplayMode depending on your needs. Usage example: SDL_DisplayMode DM; SDL_GetCurrentDisplayMode(0, &DM); auto Width = DM. w; auto Height = DM.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With