Sign in to follow this  

Input handling

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

I'm wondering how to handle input in my game. I figure that theres some layer of abstraction between SDL and the physics system that needs to know which way to point the rocket thruster, but I can't figur e out an easy way to do it. I could set up a whole mess of #defines for names of keys, set up a bool key[256] and then make each and every SDL_KEYWHATEVER point turn on or off a key[KEYWHATEVER], but I sense that there is a solution that doesn't involve quite so much work.

Share this post


Link to post
Share on other sites
Searching these forums should give you a good start. There have been a ton of threads like this one already. As I'm heading out the door I don't have time to search right now but see what you can come up with.

Share this post


Link to post
Share on other sites
I'm not sure I understand your question, but the way I did it when I was using SDL was:

void Input()
{
// Bool values for whether or not keys are held down.
static bool DownPressed = false;
static bool UpPressed = false;
static bool LeftPressed = false;
static bool RightPressed = false;


// Fill the event structure
if(SDL_PollEvent(&gEvent))
{
// Handle user manually closing the window
if(gEvent.type == SDL_QUIT)
{
// Game exiting code here.

return; // Game over
}

// A key is pressed down
if(gEvent.type == SDL_KEYDOWN)
{
/*ESC*/// Quit
if(gEvent.key.keysym.sym == SDLK_ESCAPE)
{
// Game exiting code here.
return; // Game over
}
if(gEvent.key.keysym.sym == SDLK_UP)
UpPressed = true;
if(gEvent.key.keysym.sym == SDLK_DOWN)
DownPressed = true;
if(gEvent.key.keysym.sym == SDLK_LEFT)
LeftPressed = true;
if(gEvent.key.keysym.sym == SDLK_RIGHT)
RightPressed = true;
}

// A key is released.
if(gEvent.type == SDL_KEYUP)
{
if(gEvent.key.keysym.sym == SDLK_UP)
UpPressed = false;
if(gEvent.key.keysym.sym == SDLK_DOWN)
DownPressed = false;
if(gEvent.key.keysym.sym == SDLK_LEFT)
LeftPressed = false;
if(gEvent.key.keysym.sym == SDLK_RIGHT)
RightPressed = false;
}

// if(LeftPressed) move something left, or whatever logic here...

}
}


And I called that in my gameloop, before the rendering logic. This way also allows for holding a key down and having continuous movement.

Share this post


Link to post
Share on other sites
Hmm... its this sort of laundry list that I'm trying to avoid. It seems inevitable though. Anyone else got a solution?

Share this post


Link to post
Share on other sites
Quote:
Original post by NIm
Hmm... its this sort of laundry list that I'm trying to avoid. It seems inevitable though. Anyone else got a solution?


Use SDL_GetKeyState:

unsigned char *keys = SDL_GetKeyState(NULL);

if (keys[SDLK_UP]) ...
if (keys[SDLK_ESCAPE])...


And so on. I'm in a hurry right now so all I have to say that this is a snapshot of the keyboard :).

Share this post


Link to post
Share on other sites
Here's how I would do it:

  • Define a set of "action code" constants like Action_Move_Left, Action_Fire_Gun, and so on

  • Set up a std::map that maps SDL keycodes to the action codes

  • Use SDL_GetKeyState to fetch the keystate array

  • Loop over the array; any time you find an entry that is set to True, see if there is a corresponding mapped Action Code in the std::map you set up earlier

  • If you can map from the keystate array to an action code, add that action code to a list of "current actions"

  • Once the "current actions" list is built (I recommend a std::vector or something similar to hold it), pass that to a function that carries out each action.



This has two advantages. First, and most obviously, it separates SDL input codes from your internal game logic, which is a good abstraction to put into place. Secondly, it lets you trivially support custom keymappings just by changing the contents of the std::map.

Share this post


Link to post
Share on other sites

This topic is 4203 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.

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