Phantom Events?

Member
Posts: 30
Joined: 2006.05
Post: #1
When I build and run my application using SDL event management in XCode, it works perfectly, but when I build it using the command line, I get a strange problem - it seems to think all the keys are pressed down, thus making it not accept my input and instead just send the player sprite off in one direction.

Does anyone know why these phantom events could occur? My event management code is below:

Code:
while(SDL_PollEvent(&event) != 0) {
            switch(event.type) {
                /*
                case SDL_USEREVENT:
                    break;
                */
                    
                case SDL_KEYDOWN:
                    keys[event.key.keysym.sym] = true;
                    break;

                case SDL_KEYUP:
                    keys[event.key.keysym.sym] = false;
                    break;
                    
                case SDL_MOUSEMOTION:
                    currmousex = event.motion.x;
                    currmousey = event.motion.y;
                    break;
                /*    
                case SDL_QUIT:
                    break;
                */
                    
                default:
                    break;
            }
        }
        
        //: GAME LOGIC :///////////////////////////////////////////
        
        if(keys[SDLK_RIGHT] || keys[SDLK_d]) {
            xvel = 1;
        } else if (keys[SDLK_LEFT]|| keys[SDLK_a]) {
            xvel = -1;
        } else {
            xvel = 0;
        }
        
        if (keys[SDLK_UP] || keys[SDLK_w]) {
            yvel = -1;
        } else if (keys[SDLK_DOWN] || keys[SDLK_s]) {
            yvel = 1;
        } else {
            yvel = 0;
        }
        
        if (keys[SDLK_ESCAPE]) {
            done = 1;
        }
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
Do you initialize all of the elements of your keys array to 0 at startup? If not, they'll contain random memory garbage, which probably just happens to be all zeros when you run the program from Xcode.
Quote this message in a reply
Member
Posts: 30
Joined: 2006.05
Post: #3
I hadn't initialized them correctly, but that wasn't the problem. It still does exactly the same thing. Another odd thing is that it starts with the image being very large, even though I initialize the scale variable to 1. (I use OpenGL scaling, rotation, and blitting for the image, which is made to look like it's bouncing with scaling).
Quote this message in a reply
Moderator
Posts: 771
Joined: 2003.04
Post: #4
I did the exact same thing as you did:
Code:
case SDL_KEYDOWN:
                    keys[event.key.keysym.sym] = true;
                    break;

                case SDL_KEYUP:
                    keys[event.key.keysym.sym] = false;
                    break;
on my game, until I found out that I didn't need to: SDL does this already. Smile

Simply declare your keys array as:
Code:
Uint8 *keys;

Then somewhere after you initialize SDL, call (just once, not in your even loop but before):
Code:
keys = SDL_GetKeyState(NULL);

SDL keeps its own array of key states, and SDL_GetKeyState(NULL) gives you a pointer to it.
Quote this message in a reply
Member
Posts: 30
Joined: 2006.05
Post: #5
Strangely enough, that fixed the problem... how odd. I guess SDL was sending incorrect key events as it started or something.
Quote this message in a reply
Post Reply