Newbie and input problem

Hello:

I am trying to learn SDL, so excuse me if my question is too simple:

I have this function on my code (a space invaders clone):

void update_player()
{

if ( keystate[SDLK_RIGHT] && (player.x < (screen->w - ship->w) ) ) {
	player.x += player.dx;
}

if ( keystate[SDLK_LEFT] && (player.x > 0) ) {
	player.x -= player.dx;
}

if ( keystate[SDLK_SPACE]  ) {
		shoot();
	
}

}

I have a SDL_PumpEvents(); before this function on the main loop. The shoot function:

void shoot()
{

int i;
for (i = 0; i < SHOTS; i++) {
	
	if ( shot[i].alive == 0) {
		
		shot[i].x = player.x + ( (ship->w - bullet->w) / 2 );
		shot[i].y = player.y - bullet->h - 2;
		shot[i].alive = 1;
		break;
	}
}

}

Everything works fine, but the shooting: when i press the space key, all the bullets are shot (almost) together. I need to find a way to wait the space_key to be released to shot the next bullet.

Thanks, and excuse my bad english.

Everything works fine, but the shooting: when i press the space key, all
the bullets are shot (almost) together. I need to find a way to wait the
space_key to be released to shot the next bullet.

Get involved in the message pump that sends you SDL_KEYDOWN/KEYUP events
and act on them at that point.

while (SDL_PollEvent(&sEvents))
{
	if (SDL_KEYDOWN == sEvents.type || SDL_KEYUP ==

sEvents.type)
{
… do actions here
}
}

This way you’ll only catch the transitions.

–>Neil-------------------------------------------------------------------------------
Neil Bradley In the land of the blind, the one eyed man is not
Synthcom Systems, Inc. king - he’s a prisoner.
ICQ #29402898

Hello:

I am trying to learn SDL, so excuse me if my question is too simple:

I have this function on my code (a space invaders clone):

void update_player()
{
static int shooting = 0;
if ( keystate[SDLK_RIGHT] && (player.x < (screen->w - ship->w) ) ) {
player.x += player.dx;
}

if ( keystate[SDLK_LEFT] && (player.x > 0) ) {
player.x -= player.dx;
}

if ( keystate[SDLK_SPACE] && !shooting) {
shooting = 1;
shoot();

}
else {
shooting = 0;
}
}

Add those lines and it wont let it shoot until the key have been up so
that the state is up when that function is running. So this works okay
if your frame rate is adecuate. If it should work in slower framerates
as well then better start looking about the event driven main loop
idiom.On Saturday 08 February 2003 04:21, Wil_Wal wrote: