Sign in to follow this  
kikkler

glut and game states

Recommended Posts

I want to combine the game states managment technique presented here with that of glut. i have the class CGameEngine which basically encapsulates a stack of CGameState*, and in the usual glut functions (renderScene, processNormalKeys etc') i just call the eqvivalent function of the top state. that way each state can implement his own drawing/input handling functionality here's a test code :
void IntroState::Draw(CGameEngine* game) {
	glutSolidSphere(0.1, 16, 16);
}

void IntroState::processNormalKeys(unsigned char key, CGameEngine* game) {
	if (key == 27) {
		cout << "exiting IntroState";
		exit(0);
	}
	if (key == 'a')
		game->ChangeState(GameState::Instance());
}

void GameState::Draw(CGameEngine* game) {
	glutSolidCube(1.0);
}

void GameState::processNormalKeys(unsigned char key, CGameEngine* game) {
	if (key == 27) {
		cout << "exiting GameState";
		exit(0);
	}
	if (key == 'b')
		game->ChangeState(IntroState::Instance());
}


i can switch states using 'a' or 'b', and each state should draw a different primitive. in main.cpp i have:
void renderScene() {
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	game.Draw();
	glutSwapBuffers();
}

CGameEngine game; // the global game

int main(int argc, char **argv) {
	
	game.ChangeState(IntroState::Instance());
        ...
	glutDisplayFunc(renderScene);
        ...
}


the problem is that while i can see that the states do change correctly (because each one prints something else when exiting), the sphere (of IntroState) remains constant and the cube never get drawn, even when changing to GameState any ideas anyone? tell me if you need some more code thanks

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