Sign in to follow this  
eyechild

GLUT game mode

Recommended Posts

eyechild    122
I'm having trouble creating and attaching menus in GLUT, and the problem seems to be that GLUT is in "game mode", in which you are not allowed to attach menus. However, I've tried to use the glutLeaveGameMode() function, and then I'm told you cannot leave game mode as GLUT is not in game mode! This is the code I'm having trouble with. It's a very simple example program which allows the user to clear the screen or exit the program using a menu:
#include <gl/glut.h>

void display()
{
	glClear(GL_COLOR_BUFFER_BIT);
	glBegin(GL_TRIANGLES);
		glVertex2f(0.5, -0.5);
		glVertex2f(0.0, 0.5);
		glVertex2f(-0.5, -0.5);
	glEnd();
	glFlush();
}

void menuFunc(int value)
{
	if (value == 1)
	{
		glClear(GL_COLOR_BUFFER_BIT);
		glFlush();
	}
	else if (value == 2) 
		exit(0);
}

void initMenus()
{
	glutCreateMenu(menuFunc);
	glutAddMenuEntry("Clear Screen", 1);
	glutAddMenuEntry("Exit", 2);
        glutLeaveGameMode(); // Must be done to allow menu to be attached
	glutAttachMenu(GLUT_RIGHT_BUTTON);
}
	
int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	initMenus();
	glutCreateWindow("Menu Example");
	glutDisplayFunc(display);
	glutMainLoop();
	return 0;
}
My only guess as to what is causing this is that a particular GLUT function call causes entry to game mode, but I'm damned if I know which one. Can anyone help me out?

Share this post


Link to post
Share on other sites
NineYearCycle    1538
@eyechild

move the "initMenus()" call to AFTER the glutCreateWindow call :)

works fine for me if you do that. Good luck.

Andy

edit PS: modified example follows to avoid any confusion.

#include <gl/glut.h>

void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_TRIANGLES);
glVertex2f(0.5, -0.5);
glVertex2f(0.0, 0.5);
glVertex2f(-0.5, -0.5);
glEnd();
glFlush();
}

void menuFunc(int value)
{
if (value == 1)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}
else if (value == 2)
exit(0);
}

void initMenus()
{
glutCreateMenu(menuFunc);
glutAddMenuEntry("Clear Screen", 1);
glutAddMenuEntry("Exit", 2);
glutLeaveGameMode(); // Must be done to allow menu to be attached
glutAttachMenu(GLUT_RIGHT_BUTTON);
}

int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutCreateWindow("Menu Example");
// initMenus requires a window for glutAttachMenu to attach too ;)
initMenus();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this