Sign in to follow this  
theSecondt

[C/SDL] input question

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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 do
if(up && left){stuff}


this generally worked.. but the performance was more of a dirty hack than a real solution.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

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