So I'm working on this OpenGL 3D Graph program, and like, I ran into some trouble, since I wanted it to support fullscreen, so I made the following;

int main(int argc, char *argv[])
    {
    ...  // Some stuff
    glutSpecialFunc(SpecKey);
    ... // More stuff
    }

Which is calling the following function;

GLvoid ResetWindowSize(GLvoid)  // Reset WindowSize
    {
    glutReshapeWindow(WindowWidth, WindowHeight);
    glutPositionWindow(0,0);
    }

GLvoid SpecKey(GLint key, GLint x, GLint y)
	{
	if (key == GLUT_KEY_F1)     // Toggle FullScreen
            {
	    fullscreen = !fullscreen; // Toggle FullScreenBool
	    if (fullscreen)
	    	{
		glutFullScreen();     // Enable Fullscreen
            	}
	    else
            	{
            	ResetWindowSize();    // Make Window, and resize
            	}
            }
	}

WindowWidth and WindowHeight are both global variables, which are updated everytime I'm resizing the window.

fullscreen is a global bool.

The problem is, that going to fullscreen is working fine, however returning to windowed mode, is giving me some problem since it returns with a window resolution equal to my desktop resolution, and not the resolution which is givin' in the variables WindowWidth and WindowHeight.

Anyone able to figure out the problem?

EDIT; I'm able to get it into a fixed resolution using fixes values instead of the variables like this;

GLvoid ResetWindowSize(GLvoid)  // Reset WindowSize
    {
    glutReshapeWindow(800, 600);
    glutPositionWindow(0,0);
    }

However to make this work I'm forced to call the "ResetWindowSize" function twice?

Dani AI

Generated

A likely cause of the behavior described by is that the window enters fullscreen asynchronously and the reshape callback (or GLUT itself) updates the globals to the desktop resolution when the fullscreen switch happens. Restoring immediately then uses those overwritten values, and some GLUT builds ignore the first restoration call while the window manager is still changing modes — which explains the “call twice” symptom.

A robust pattern: save the window geometry and a flag before going fullscreen, suppress reshape-driven updates while fullscreen, and restore the saved geometry after leaving fullscreen using a short timer so the WM transition has completed. Example (concept only — adapt types/names to the program):

/* Save before fullscreen */
savedW = WindowWidth; savedH = WindowHeight;
savedX = WindowX;     savedY = WindowY;
ignoreReshape = true;
glutFullScreen();

/* When toggling back, schedule restore */
glutTimerFunc(10, restoreWindowCB, 0);

void restoreWindowCB(int)
{
    glutReshapeWindow(savedW, savedH);
    glutPositionWindow(savedX, savedY);
    ignoreReshape = false;
}

/* In the reshape callback, ignore updates while ignoreReshape == true */

Notes: if using freeglut, glutLeaveFullScreen() can be used before the timer. Avoid hardcoding (0,0) so multi-monitor setups restore correctly. The small delay (10–50 ms) can be tuned per platform; when toggling still misbehaves, consider using a more modern windowing library (GLFW/SDL) that exposes explicit enter/leave-fullscreen controls.

Anyone?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.