[C/SDL] input question

Started by
5 comments, last by Cheery 17 years, 10 months ago
hello, i'm having troubles with getting the input from keyboard and then using that data for my game. the game should allow the user to move the tank forward AND rotate it at the same time, but i can do or only the rotating bit or only the forward moving. i used the keystate function and the event thing and i got the same problem, i'm prolly doing something wrong. could someone show me how i can rotate and move at the same time, or some literature would be nice. ty.
Advertisement
Maybe you could show us your input code?
keystate = SDL_GetKeyState(NULL);if (keystate[SDLK_LEFT])left=1;else if (keystate[SDLK_RIGHT])right=1;else if (keystate[SDLK_UP])up=1;else if (keystate[SDLK_DOWN])down=1;//and then i'd doif(up && left){stuff}


this generally worked.. but the performance was more of a dirty hack than a real solution.
So is it working? In your first post, you say "i used the keystate function and the event thing and i got the same problem", but in your second "this generally worked".

However, looking at the code, all your tests are excluding one another:

if (keystate[SDLK_LEFT])left=1;else if (keystate[SDLK_RIGHT])right=1;// why should there be an else here?else if (keystate[SDLK_UP])up=1;else if (keystate[SDLK_DOWN])down=1;


Did you mean:

if (keystate[SDLK_LEFT]){    left=1;}else if (keystate[SDLK_RIGHT]){   right=1;}if (keystate[SDLK_UP]){   up=1;}else if (keystate[SDLK_DOWN]){   down=1;}
Or perhaps use a switch?

switch(event.key.keysym.sym){case SDLK_UP: break;case SDLK_DOWN:break;case SDLK_LEFT:break;case SDLK_RIGHT:break;}


//edit: read over the part about pressing 2 buttons at the same time...
------------Something useful could be written here...
the code i posted above is in a function, and that function is called in the main loop.
i used the elses for faster computing.. hmm i'll use switch..

anyways.. this doesn't even matter, because i'm asking if there is any other way to solve this or not?
Put the input keys and actions into sequence of pairs, then make input handler(invent better name) which call actions paraller to input key. Input handler pools those events from SDL.

This way you have a control table, which allows you to handle this all, thought very primitively, but anyway simply.
~ person who has nothing to say feels itself naked in enviroment where nonsense is hard to camouflage into the nonexistent myriad of noises. ~

This topic is closed to new replies.

Advertisement