Archived

This topic is now archived and is closed to further replies.

Kyger

Question about my game loop

Recommended Posts

My problem is that if I take out the while-loop that checks for SDL_QUIT before p.action, the escape key doesn''t exit out of the program, but if I keep the while loop in there, the escape key works. Does pressing the escape key toggle SDL_QUIT flag? (whatever it''s called, Im new to SDL)
CPlayer p;

void CEngine::loop() {
	while(p.iState != GAME_QUIT ) {
		switch(p.iState) {
			case GAME_MENU:
				p.iState = titlemenu();
				break;
			case GAME_LOOP:
				while(SDL_PollEvent(&event)){
					switch(event.type){
						case SDL_QUIT:
							p.iState = GAME_QUIT;
							break;
						default:
							break;
					}
				}
				p.action();
				SDL_Delay(1);
				break;
			case GAME_OVER:
				break;
			case GAME_REDO:
				break;
			case GAME_QUIT:
				exit(1);
				break;
			default:
				break;
		}
	}
}

void CPlayer::action() {
	iVelX = 0;
	keys = SDL_GetKeyState(NULL);

	if(keys[SDLK_RIGHT]) {
		iVelX = iSpeed;
		bDirection = true;
	}
	if(keys[SDLK_LEFT]) {
		iVelX = -(iSpeed);
		bDirection = false;
	}
	if(keys[SDLK_UP] && !bJumped) {
		iVelY = -(iUpspeed);
		bJumped = true;
	}
	if(keys[SDLK_ESCAPE]) {
		iState = GAME_QUIT;
	}
}
If there''s any information I left out, I''ll post it if you need.

Share this post


Link to post
Share on other sites
No, pressing the Esc key doesn''t toggle SDL_QUIT. SDL_QUIT is generated when you try to close the window. If you don''t have a handler for SDL_QUIT or set an exit function with atexit() then your window won''t even close.

But anyways to the problem at hand. My only question is why do you have to poll for you keys when you can just use the message pump?

while(SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT: {
p.iState = GAME_QUIT;
} break;
case SDL_KEYDOWN: {
if (event.key.keysym.sym == SLDK_ESCAPE)
p.iState = GAME_QUIT;
} break;
default:
break;
}
}


_________________________________________________________________

Drew Sikora
President, Lead Programmer - Blade Edge Software
Staff Writer, Newsletter Editor - GameDev.net
Community Relations - Game Institute

Share this post


Link to post
Share on other sites