PDA

View Full Version : SDL: Spacebar Resulting in SDL_QUIT?


onyxthedog
11-15-2009, 06:18 PM
The SDL_Event type SDL_QUIT seems to set when I hit the spacebar. Any ideas as to why?

Thank you,
Onyx

void HandleEvents()
{

switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_q:
printf("Bye Bye!\n");
SDL_FreeSurface(sprite);
exit(0);

case SDLK_SPACE:
break;

default:
break;
}

case SDL_QUIT:
printf("Bye Bye!\n");
SDL_FreeSurface(sprite);
exit(0);

default:
break;
}
} Oh and event is a global variable as is sprite, so it is there.

P.S. I added the SDLK_SPACE because it kept exiting, as for now it seems if I use return; instead of break it works.

UPDATE: After further testing, it seems that all nonexplicitly defined keystrokes cause this to occur.

Reedbeta
11-15-2009, 07:45 PM
The break after SDLK_Space breaks out of the inner switch, but not the outer one; execution then falls through into the SDL_QUIT case. You need another break after the inner switch.

onyxthedog
11-15-2009, 08:01 PM
The break after SDLK_Space breaks out of the inner switch, but not the outer one; execution then falls through into the SDL_QUIT case. You need another break after the inner switch.
But why is SDL_QUIT even meeting the criteria? As for the way to break out of the inner switch I found return; works pretty well, but your way is better now that I see that.

Congrats on the release of Infamous, just the other day I saw a kid playing the demo awing over it and making out loud comments about how cool it is.

Reedbeta
11-15-2009, 09:16 PM
But why is SDL_QUIT even meeting the criteria?

It's not. If you don't put a break, return, or something at the end of a case it falls through into the next case. That's how switch statements work. Trace through it in a debugger and you'll see.

Congrats on the release of Infamous, just the other day I saw a kid playing the demo awing over it and making out loud comments about how cool it is.

Thanks :)

onyxthedog
11-15-2009, 09:23 PM
It's not. If you don't put a break, return, or something at the end of a case it falls through into the next case. That's how switch statements work. Trace through it in a debugger and you'll see.



Thanks :) I understand about the case statements I just thought it was weird that a keyboard event was also meeting the requirements of SDL_QUIT, as far as SDL_Event.type is concerned.

Reedbeta
11-15-2009, 10:16 PM
Maybe we're misunderstanding each other. What do you mean by "meeting the requirements of SDL_QUIT"? It sounds like you're expecting the SDL_QUIT case label to check again that the event type is SDL_QUIT. This isn't what happens; when you fall through from one case to another, you just fall through. The only check is right at the top of the switch.

JarkkoL
11-15-2009, 11:27 PM
You forgot to add break for SDL_KEYDOWN case.

onyxthedog
11-16-2009, 05:41 AM
@Reedbeta: Nevermind the pre-edit ideas of this post, I understand what you are saying, thanks.
@JarkkoL: Thanks, I noticed that after I posted it. Before I figured it out, I was using "return;" where there are breaks in that inner cases.