Jump to content
  • Advertisement
Sign in to follow this  
MARS_999

Help with SDL events, movement

This topic is 5144 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am jumping back into SDL. I can't remember how to allow me to hold down the key and continously update the variable for movement? I have to hit the key 10 times if I want to move 10 units... How can I allow the user to hold the key and the event poll keep taking input? Thanks

Share this post


Link to post
Share on other sites
Advertisement
Set a flag when the key goes down, clear it when it goes up. Don't check for the current state of the key; just watch event type.

Share this post


Link to post
Share on other sites
SDL already does this for you

Uint8 *keys = SDL_GetKeyState(NULL);

if(keys[SDLK_b])
{
THE B KEY IS BEING PRESSED DOWN!!!
}

you should probably call SDL_GetKeyState() AFTER you Poll for input!

Share this post


Link to post
Share on other sites
I like the flag setting idea better personally, though graveyard filla's suggestion is also good. Note that if you don't call SDL_WaitEvent() or SDL_PollEvent(), then you will need to do something like this:


SDL_PumpEvents(); // Updates the event queue
key_status = SDL_GetKeyState(NULL); // Get information about the status of the keyboard

if ( !key_status[SDLK_UP] ) { // The UP key has been released, so exit the loop
break;
}


If you don't call SDL_PumpEvents() before you call SDL_GetKeyState(), then you won't be getting the latest status of the keyboard. (SDL_WaitEvent and SDL_PollEvent both internally call SDL_PumpEvents, so if you have either one of those you shouldn't need to do this).

Share this post


Link to post
Share on other sites
This is what I am doing...

while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_MOUSEMOTION:
break;
case SDL_MOUSEBUTTONDOWN:
break;
case SDL_KEYDOWN:
switch( event.key.keysym.sym )
{
case SDLK_n:
shift_model_x +=1.0f;
break;


Share this post


Link to post
Share on other sites
Hey, all. After you get the pointer to the keyboard with SDL_GetKeyState, you never have to call it again. You just call SDL_PumpEvents once per frame.

I found that out while browsing the doc files.

Share this post


Link to post
Share on other sites
hmm, thats odd, i dont understand that.

how does it know to re-fill your array then ? Pump events tell it i guess, but how does it know WHICH pointer to fill? what if you send SDL_GetKeyState() a couple of different pointers? sounds kind of weird.

Share this post


Link to post
Share on other sites
You misunderstand; it re-fills the same internal array. The pointer you pass to it is points to an integer that it fills with the size of that array.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!