Jump to content
  • Advertisement
Sign in to follow this  
theSecondt

[C/SDL] input question

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

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
Advertisement
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
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!