Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Portable way in C++ to get desktop resolution

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?

like image 316
Vittorio Romeo Avatar asked Nov 16 '12 17:11

Vittorio Romeo


People also ask

How do I get the resolution of my desktop?

Change the screen resolutionStay in, or open, Display settings. Scroll to Scale and layout. Find Display resolution, and then choose an option.

How do I check my screen size in SDL?

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.


1 Answers

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.

like image 95
wardd Avatar answered Oct 12 '22 19:10

wardd