Help with SDL events, movement

Started by
6 comments, last by Kylotan 19 years, 7 months ago
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
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.
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!
FTA, my 2D futuristic action MMORPG
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 queuekey_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).

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement

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;

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.
HxRender | Cornerstone SDL TutorialsCurrently picking on: Hedos, Programmer One
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.
FTA, my 2D futuristic action MMORPG
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.

This topic is closed to new replies.

Advertisement