Jump to content


SDL: Spacebar Resulting in SDL_QUIT?


  • You cannot reply to this topic
7 replies to this topic

#1 onyxthedog

    Senior Member

  • Members
  • PipPipPipPip
  • 467 posts

Posted 16 November 2009 - 02:18 AM

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.
/* Perfect_day.c */
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#2 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 16 November 2009 - 03:45 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#3 onyxthedog

    Senior Member

  • Members
  • PipPipPipPip
  • 467 posts

Posted 16 November 2009 - 04:01 AM

Reedbeta said:

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.
/* Perfect_day.c */
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#4 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 16 November 2009 - 05:16 AM

onyxthedog said:

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.

onyxthedog said:

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 :)
reedbeta.com - developer blog, OpenGL demos, and other projects

#5 onyxthedog

    Senior Member

  • Members
  • PipPipPipPip
  • 467 posts

Posted 16 November 2009 - 05:23 AM

Reedbeta said:

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.
/* Perfect_day.c */
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........

#6 Reedbeta

    DevMaster Staff

  • Administrators
  • 4969 posts
  • LocationBellevue, WA

Posted 16 November 2009 - 06:16 AM

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.
reedbeta.com - developer blog, OpenGL demos, and other projects

#7 JarkkoL

    Senior Member

  • Members
  • PipPipPipPip
  • 467 posts

Posted 16 November 2009 - 07:27 AM

You forgot to add break for SDL_KEYDOWN case.

#8 onyxthedog

    Senior Member

  • Members
  • PipPipPipPip
  • 467 posts

Posted 16 November 2009 - 01:41 PM

@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.
/* Perfect_day.c */
#include <arcade>
#include <computer>
#include <drinks>
#include <hardware/high_end>
#include <snacks>
#pragma <responisiblities>
...........





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users