Sign in to follow this  

SDL Key Delay

This topic is 816 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

while (SDL_PollEvent(&ev) !=0)
		
		{ //inicio do eventHandle
			
			if (ev.type == SDL_QUIT || ev.key.keysym.sym == SDLK_ESCAPE)
			{
				isRunning = false;
			}
			
			if(ev.type == SDL_KEYDOWN)
			{	
				switch(ev.key.keysym.sym)
				{
					case SDLK_RIGHT:
						
						flipTheDog = frontDog;
						quadradoOrigem.x +=35;
						if (quadradoOrigem.x > 70)
						{
							quadradoOrigem.x = 0;
						}
						break;

Hello Again Guys!
 
Programming my first game in SDL, the Kakorro's Quest it was that I found a doubt..
I was programed the basic walk animation. Until here, it's all ok.
But, when i pressed a Key, and keep it pressed, the game takes a delay to recognize the next keypressed...
this causes a stop motion in the animation, like the dog walking animation, then stops for a little delay, then do the animation normally.
 
How i can fix that?
 
My code:

 

 

Share this post


Link to post
Share on other sites

while (SDL_PollEvent(&ev) !=0)
		
		{ //inicio do eventHandle
			
			if (ev.type == SDL_QUIT || ev.key.keysym.sym == SDLK_ESCAPE)
			{
				isRunning = false;
			}
			
			if(ev.type == SDL_KEYDOWN)
			{	
				switch(ev.key.keysym.sym)
				{
					case SDLK_RIGHT:
						
						flipTheDog = frontDog;
						quadradoOrigem.x +=35;
						if (quadradoOrigem.x > 70)
						{
							quadradoOrigem.x = 0;
						}
						break;
Hello Again Guys!
 
Programming my first game in SDL, the Kakorro's Quest it was that I found a doubt..
I was programed the basic walk animation. Until here, it's all ok.
But, when i pressed a Key, and keep it pressed, the game takes a delay to recognize the next keypressed...
this causes a stop motion in the animation, like the dog walking animation, then stops for a little delay, then do the animation normally.
 
How i can fix that?
 
My code:


show more code.

you can simplify the inner loops but show the full structure (if you for example update game state and render inside the event polling while loop you will get significant delays on all events, not just keyboard events)

Share this post


Link to post
Share on other sites
How do you do timing?

Afaik the usual approach is to fixate frame rate, so you become unaffected by the exact amount of CPU time that you use for each iteration.
http://lazyfoo.net/SDL_tutorials/lesson14/index.php (this is SDL1, at the top is a link to the SDL2 equivalent). Lazy Foo' is very nice to read, lots of useful tips there.

Share this post


Link to post
Share on other sites

There are two ways to get input with SDL:

 

1.  Listener for events( such as keyboard or mouse ), and respond to them in the event loop.

2.  Poll the keyboard and mouse in the game loop and respond to their current state.

 

You need both for a game.  As you have discovered, handling the keyboard in the event loop is great for typing, but terrible for movement.

 

TL;DR - use the SDL_KEYDOWN, SDL_KEYUP, SDL_MOUSEBUTTONDOWN, and all those other events in the message loop to handle typed keys (that pause and repeat on hold)

 

Use SDL_GetMouseState, SDL_GetRelativeMouseState, SDL_GetKeyboardState, and SDL_GetModState in the game loop to handle real-time input.

Share this post


Link to post
Share on other sites

Hi,

 

SDL_KEYDOWN actually means that the Key got pressed, not that it is held down so one would expect to only get this event once for every key pressed, regardless of the duration (likewise SDL_KEYUP means that it got released).

In your case, you do get additional SDL_KEYDOWN events when holding down the key because you probably didnt disable the KeyRepeat feature. That means that the SDL_KEYDOWN event will be sent repeatedly. However there will be an initial delay and a much shorter delay between subsequent events (thats what also happens if you keep a key pressed in most Edit Boxes, text editors etc.).

 

I think what you actually want to do is cache whether the key is currently down or not (--> was the last event an SDL_KEYUP or an SDL_KEYDOWN event) and move based on that.

Share this post


Link to post
Share on other sites

This topic is 816 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this