Finding screen width and height dynamically in OpenGL GLUT program

In my OpenGL program, in order to run it in fullscreen mode, I’m using the GLUT function glutGameModeString(const char *string).
(where ‘string’ specifies the screen width,height,pixelDepth and refresh rate)

To make it working in any system,i need to dynamically determine the screen width and screen height of the host system, prior to making the call to ‘glutGameModeString’ function.
How do I do that?

(I just need to know how to retrieve the screen height and width in windows)

You stop using GLUT and use a system that’s designed to be used for this sort of thing. GLUT has no function for this sort of thing, and OpenGL doesn’t know of anything outside of the window.

GLFW is one possibility.

What about just using the default:

Per the API docs "glutGameModeString: Specify the display mode that should be entered when GameMode is entered. Default is the current display mode of the monitor on which the GameMode screen will be opened. "

Also, have you looked at: glutFullScreen, glutLeaveFullScreen, glutFullScreenToggle?


Unfortunately GLUT is very limited for this kind of thing - them’s the tradeoffs for using a simplified framework.

If, like you say, you’re only interested in finding this on Windows, you could break into some Windows API calls. For getting the size of the entire screen, something like this can suffice:

HDC hdc = GetDC (NULL);
width = GetDeviceCaps (hdc, HORZRES);
height = GetDeviceCaps (hdr, VERTRES);
ReleaseDC (NULL, hdc);

That’s sometimes not what you want; you may want the size of the working area of the screen (i.e. excluding the taskbar and other similar widgets) in which case something like this will do the job:

RECT workarea;
SystemParametersInfo (SPI_GETWORKAREA, 0, &workarea, 0);

Both of these have uses other than just for helping with going to and from fullscreen mode; the working area size is particularly useful for setting an upper bound on the largest windowed mode you might wish to support, and can also be used for centering your window properly on the screen.