Changeable Input

This topic is 2199 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hi all !
I'm writing a simple 2d game using SDL. I would like my game has got a changeable input from user. For the example: left arrow on the keyboard moves sth to the left. User change it, now Joystick axis motion moves sth to the left. I have no idea how do that. Can anybody help me ?

Share on other sites
Exact details depend on what programming language you are using, but there are a number of ways to do this. One way is to create a map or associative array of event objects to function pointers or function objects. Then in each event handling block invoke the functor for the received events. Alternately you can use a map of events to tokens and then have your input handling functionality dispatch tokens to game state objects.

Share on other sites
I'm using C++, could you explain it a little more with short code example ?

Share on other sites

I'm using C++, could you explain it a little more with short code example ?

 typdef void (*action)(Player&); void Action1(Player &p) { code } void Action2(Player &p) { code } ... ... ... ... std::map<int,action> actions = new std::map<int,action>(); actions[SDLK_SPACE] = Action1; actions[SDLK_LEFT] = Action2; (Assigning and reassigning can be done however you want) .... then you just read input and call: actions[keyThatWasPressed](playerobject); 

i might have messed up the function pointer syntax somewhere but you should get the general idea.

Share on other sites
Thanks, but how can I do it for SDL_Event structure ?
I'd like to use joystick too, it seems that is for keyboard only.

Exact details depend on what programming language you are using, but there are a number of ways to do this. One way is to create a map or associative array of event objects to function pointers or function objects. Then in each event handling block invoke the functor for the received events. Alternately you can use a map of events to tokens and then have your input handling functionality dispatch tokens to game state objects.

Could you write how to use a map of events ? I tried it myself but it doesn't work.

Share on other sites

Thanks, but how can I do it for SDL_Event structure ?
I'd like to use joystick too, it seems that is for keyboard only.

[quote name='SiCrane' timestamp='1326554293' post='4902658']
Exact details depend on what programming language you are using, but there are a number of ways to do this. One way is to create a map or associative array of event objects to function pointers or function objects. Then in each event handling block invoke the functor for the received events. Alternately you can use a map of events to tokens and then have your input handling functionality dispatch tokens to game state objects.

Could you write how to use a map of events ? I tried it myself but it doesn't work.
[/quote]

The key part of the map has to have a comparison operator defined (the < operator in this case) which SDL_Event doesn't have, You could write your own EventKey(ok, maybe not the best name) class that stores the necessary information and which overloads the < operator or you can pass a comparison function to the map.

Share on other sites
You can use event.key.keysym.sym as the lookup key for key events:
 // Somewhere handy for inclusion typedef void (*KeyFunction)(Player &player); typedef void (*MouseFunction)(Player &player, int x, int y); typedef std::map<SDL_KeySym, KeyFunction> KeyMap; typedef std::map<Uint8, MouseFunction> MouseMap; // Later, during setup in main(). KeyMap keyMap; keyMap[SDLK_UP] = &playerForward; keyMap[SDLK_DOWN] = &playerBackward; keyMap[SDLK_LEFT] = &playerLeft; keyMap[SDLK_RIGHT] = &playerRight; MouseMap mouseMap; mouseMap[SDL_BUTTON(1)] = &playerShoot; // Later again, during game loop... bool running = true; while(running) { SDL_Event event; while(SDL_PollEvent(&event)) { if(event.type == SDL_QUIT) { running = false; } else if(event.type == SDL_KEYDOWN) { KeyMap::iterator i = keyMap.find(event.key.keysym.sym); if(i != keyMap.end()) { KeyFunction action = i->second; action(player); } } else if(event.type == SDL_MOUSEBUTTONDOWN) { MouseMap::iterator i = mouseMap.find(event.button.button); if(i != mouseMap.end()) { MouseFunction action = i->second; action(player, event.button.x, event.button.y); } } } // ... } 

Having a general event mapping system is a little more difficult. For most games, you might want to map key presses or releases, and mouse clicks. Mapping complicated, stateful actions like mouse dragging will take a bit more thought.