Jump to content
  • Advertisement
Sign in to follow this  
Mizipzor

Sdl, continued input

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

Right now my main loop looks like this:
	while(running) {
		while(SDL_PollEvent(&event)) {
			if(event.type == SDL_QUIT)
				running = false;

			if(event.key.keysym.sym == SDLK_ESCAPE)
				running = false;

			if(event.key.keysym.sym == SDLK_UP) {
				sprite.SetDirection(0);
				sprite.SetPos(sprite.GetPos().X, sprite.GetPos().Y-2);
			}
			if(event.key.keysym.sym == SDLK_RIGHT) {
				sprite.SetDirection(1);
				sprite.SetPos(sprite.GetPos().X+2, sprite.GetPos().Y);
			}
			if(event.key.keysym.sym == SDLK_DOWN) {
				sprite.SetDirection(2);
				sprite.SetPos(sprite.GetPos().X, sprite.GetPos().Y+2);
			}
			if(event.key.keysym.sym == SDLK_LEFT) {
				sprite.SetDirection(3);
				sprite.SetPos(sprite.GetPos().X-2, sprite.GetPos().Y);
			}

		}

		// DRAW	
		Sdl->ClearScreen();

		sprite.Draw();

		Sdl->Flip();
	}

Right now if I press, the sprite jumps two pixel but then stops. How would the correct loop look if I wanted to be able to press and hold the key for the sprite to continue moving?

Share this post


Link to post
Share on other sites
Advertisement
your "sprite" class probably has a x and a y varaible. add an xVelocity and a yVelocity.

so you go:

if( upkeyPressed )
{
sprite.setYVelocity( 2 );
}
if( upkeyReleased )
{
sprite.setYVelocity( 0 );
}



you can add an update method to your sprite aswell, that does:


void Sprite::update()
{
xpos += xvel;
ypos += yvel;

}



and call upate along with draw...

Share this post


Link to post
Share on other sites
I haven't used SDL that much, but I'm going to venture a guess here. First of all, make sure that SDL key repeat is turned off. Then, you need to actually check for the event types rather than just assuming they're keypresses. More specifically, you need to differentiate between 'key down' and 'key up' events. When you receive a 'key down' event, set a flag indicating that you are moving in the direction associated with the key; when you receive a 'key up' event, disable the flag. Also add some code to the 'set flag' function that makes sure only one flag is on at a time.

Then, elsewhere in the event loop, apply the appropriate motion depending on which flag is set, if any. The thing is that SDL generally only issues an event when a key is first pressed, and when it's released (if key repeat is off). So in your current loop, the behavior you're describing is exactly what you should expect to happen.

Share this post


Link to post
Share on other sites
Try using SDL_GetKeyState instead. That should help you detect when keys are being held down, as opposed to just checking when they are pushed down initially.

Share this post


Link to post
Share on other sites
Quote:
Original post by Ravuya
Try using SDL_GetKeyState instead. That should help you detect when keys are being held down, as opposed to just checking when they are pushed down initially.


Exactly what I wanted to know. Thanks :)

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!