Newbie's question about events

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello all,

I' coding my first programs with SDL, and I have found a problem.

I’m sure it’s very easy to solve, but I haven’t been able yet.
I have a duck (“pato”, in Spanish) graphic, which is controlled by
keyboard’s input. I use the following code to build each frame of the
game:

void pasar()
{
SDL_Rect rect;
rect.x=x;rect.y=y;rect.w=pato->w;rect.h=pato->h; //pato is the
surface with the duck’s graphic
while(SDL_PollEvent(&event)==1){
if(event.type==SDL_KEYDOWN &&
event.key.keysym.sym==SDLK_ESCAPE){quit=1;}
if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_UP &&
y>70){y–;}
if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_DOWN &&
y<410-pato->h){y++;}
if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_RIGHT &&
x<570-pato->w){x++;}
if(event.type==SDL_KEYDOWN && event.key.keysym.sym==SDLK_LEFT &&
x>70){x–;}
if(event.type==SDL_QUIT){quit=1;}
}
ciclos_realizados++;
}

Of course, after this I run frame(), blitting everithing needed to

screen.
My problem is that my duck only moves in one direction a time. It
moves vertically and horizontally, but never in a 45? degree (I think
it should, if I press two keys a time).
What should I do?

Thank you very much for your time.


Jes?s Carrete Monta?a ----> KUANTIKO___________________________________________
| .~. |
| /V\ jrcarmon(arroba)teleline.es |
| // \ Linux Registered |
| /( )\ User #158442 |
| ^`~’^ |
|___________________________________________|

Clave p?blica PGP disponible por e-mail.

-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.1i for non-commercial use http://www.pgpi.com/

iQA/AwUBOU0rbVXBzV6UX+HYEQLcBwCg+HP2EvJ6LF2qsnihGhTuzTDaI4YAn3jx
8qGQFqCYOkAPcfH44xMpYeEQ
=7ojH
-----END PGP SIGNATURE-----

Hi!

I wonder, why the code is working at all. First, I think some brackets
are missing, aren’t they? Nevertheless, you have to press the arrow key
again and again to move more than one step. Or have you activated key
repeating? I suppose so. The consequence is, that you get two events in
the beginning (if you press 2 directions simultaneously). But due to the
key repeating, only the second pressed key will yield repeated events.
So you move only in this second direction like you know it.

If you switch off the key repeating, you have to press again for each
step as I said above.

So the only solution is to use SDL_GetKeyState(). Afterwards you can
test if any keys are (held) down or up including simultaneously pressed
keys. I recommend to switch off the KEYDOWN events if you don’t need
them.

Was this your question?

Rainer Loritz

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

rloritz at gwdg.de escribi?:

So the only solution is to use SDL_GetKeyState(). Afterwards you
can test if any keys are (held) down or up including simultaneously
pressed keys. I recommend to switch off the KEYDOWN events if you
don’t need them.

Was this your question?

Yes. My code is now working fine. Thanks!

Jes?s Carrete Monta?a ----> KUANTIKO___________________________________________
| .~. |
| /V\ jrcarmon(arroba)teleline.es |
| // \ Linux Registered |
| /( )\ User #158442 |
| ^`~’^ |
|___________________________________________|

Clave p?blica PGP disponible por e-mail.

-----BEGIN PGP SIGNATURE-----
Version: PGP 6.5.1i for non-commercial use http://www.pgpi.com/

iQA/AwUBOVI/BVXBzV6UX+HYEQIj9ACeNcYITiqHkw+G39GMYzdXUTwSd7UAoNCc
yFfsOuyqJ71HUXkBORpxirLj
=g5gT
-----END PGP SIGNATURE-----