🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Screen not updating in game mode

Started by
-1 comments, last by imyourfoot 18 years, 2 months ago
I'm trying to get my program to switch between windowed mode and game mode when I hit the f key, and for the most part it's working. The only small problem is that the screen isn't updating after I go into game mode, even though I've re-registered the callback functions. It will display the first frame after the switch, and when I hit f again to switch back to windowed mode it will display the last frame, but nothing in between gets drawn. Windowed mode works like it's supposed to. Game mode does work correctly if I don't set up the window, and go straight into game mode, but then I don't have the windowed mode. My code:

int main(int argc, char** argv){
	startTime = clock();
	glutInit(&argc, argv);
	cam = new Camera(start, center, up);
	setupWindow();
//	switchGameMode(); (when I comment out setupWindow and uncomment this, it launches into game mode and works correctly, but then there's no window mode
	glutMainLoop();
}

void myKeyboard(unsigned char key, int x, int y){
	int a = x; a = y; // do nothing, get rid of compiler warnings
	switch(key){
		case 'f': switchGameMode(); 
			break;
		// other cases to handle camera movement controls
	}
}

void myDisplay(){
	float matrix[16] = {1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0};
	GLfloat mat_ambient[] = {1.0f, 0.0f, 0.0f, 1.0f}; // red
	GLfloat mat_diffuse[] = {1.0, 0.0f, 0.0f, 1.0f};
	GLfloat mat_specular[] = {1.0f, 0.0f, 0.0f, 1.0f};
	GLfloat mat_shininess[] = {50.0f};
	glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
	glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
	glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
	glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
	GLfloat lightIntensity[] = {.7f, .7f, .7f, 1.0f};
	GLfloat light_position[] = {2.0f, 6.0f, 3.0f, 0.0f};
	glLightfv(GL_LIGHT0, GL_POSITION, light_position);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightIntensity);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	cam->setModelViewMatrix();
	glPushMatrix();
	clock_t endTime = clock();
	double elapsedTime = endTime - startTime;
	glTranslated(-10 + 3*sin((elapsedTime*1.0)/CLOCKS_PER_SEC), 2*cos((elapsedTime*1.0)/CLOCKS_PER_SEC), 4*sin((elapsedTime*1.0)/CLOCKS_PER_SEC));
	glRotated(elapsedTime*50.0/CLOCKS_PER_SEC, 3*sin(3*elapsedTime/CLOCKS_PER_SEC), 4*cos(2*elapsedTime/CLOCKS_PER_SEC), 1*sin(5*elapsedTime/CLOCKS_PER_SEC));
	glutSolidCone(3, 3, 50, 4); // magic numbers!
	glPopMatrix();
	glFlush();
	glutSwapBuffers();
}

void idle(){
	glutPostRedisplay();
}

void switchGameMode(){
	static bool gameMode = false;
	gameMode = !gameMode;
	if(gameMode){
		glutGameModeString("1024x768:32");
		if(glutGameModeGet(GLUT_GAME_MODE_POSSIBLE)){
			cout << "game mode possible" << endl;
			glutSetWindow(glutEnterGameMode());
			setupGameWindow();
		}
		else{
			cout << "game mode not possible!" << endl;
			exit(1);
		}
		if(glutGameModeGet(GLUT_GAME_MODE_ACTIVE))
			cout << "game mode active!\n";
		else
			cout << "game mode not active!\n";
	}
	else{
		cout << "leaving" << endl;
		glutLeaveGameMode();
	}
}
void setupWindow(){
	cout << "setting up main window\n";
	glutInitWindowSize(640, 480);
	glutInitWindowPosition(50, 50);
	glutCreateWindow("GLUT playground");
	glutKeyboardFunc(myKeyboard);
	glutDisplayFunc(myDisplay);
	glutIdleFunc(idle);
	glutInitFunction();
	cam->setShape(30.0f, 64.0f/48.0f, 1.0f, 200.0f);
}

void setupGameWindow(){
	cout << "setting up game window\n";
	glutKeyboardFunc(myKeyboard);
	glutDisplayFunc(myDisplay);
	glutIdleFunc(idle);	
	glutInitFunction();
	cam->setShape(30.0f, 1024.0f/768.0f, 1.0f, 200.0f);
}

void glutInitFunction(){
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	glShadeModel(GL_SMOOTH);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_NORMALIZE);
	glEnable(GL_TEXTURE_2D);
	glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
	glColor3f(1.0f, 0.0f, 0.0f);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	cam->setModelViewMatrix();
}

My computer fan goes quiet when I'm in game mode, so I would say that the callback functions aren't getting called, except that I can move the camera (even though the display doesn't update), and if I put a counter into the idle/display functions it goes up whether I'm in game mode or not. I get the feeling I'm missing something really simple here, but I have no idea what.

This topic is closed to new replies.

Advertisement