Jump to content
  • Advertisement
Sign in to follow this  

Handling Input as Generic Data (Multiple Inputs)

This topic is 2137 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 (re)working on my input system and I thought "what if I decide to add controller support? How would I do it?"


Since there are so many different controllers and systems, I was thinking in a way to keep it as generic as possible to easily adapt to whatever controller available.

At first I had a bool[256] for keyboard and bool[16] for mouse, but I was worried there might be different mouses (with even more buttons) or even those multimedia keyboards with > 256 keyCodes, so I was thinking of how to deal with that.


So, the way I thought to handle this is:

class ControllerKey
        int keyID;
        int keyDataPosition;
class Controller
        //Adds a new ControllerKey to the vector, keyType defines the size of it's data
        void AddKey(int keyID, KeyTypeEnum keyType);
        //Returns &_keyData[i], where _keys[i]->keyID == keyID;
        char* GetKey(int keyID);
        //Sets a bool value keyValue at _keys[i];
        void SetKey(int keyID, bool keyValue);
        //Sets a float value keyValue at _keys[i];
        void SetKey(int keyID, float keyValue);
        //Creates the _keyData array
        void CreateController();
        char* _keyData;
        vector<ControllerKey*> _keys;

So I keep a char array for a total of byte sizes equal to all keys before I "create" the controller, and when I change a value, it looks up for the keyPosition in the array and changes the address according to the keyType (4 bytes for float, 1 byte for bool).


This way I can "create" any type of controller on my game, and even filter some input that my game receives (instead of receiving all keyboard input and only using a few for example, if my game doesn't use other keys at all).


Mapping keys to actions would be done on a higher level.


I'd like to ask suggestions and opinions if this is a good idea or not, because I have no clue... I'm not sure how developers deal with different controllers/systems, and trying to google it I only found conceptual ideas on how to handle input events, not different types of input.

Share this post

Link to post
Share on other sites
Guest Hiwas
I actually tend to work even one step away from what you have there. Instead of thinking about keys and such, I end up with an action structure. For instance:

struct InputActions
  float   Forward;
  float   Left;
  float   Right;
  bool    Jump;
  ... etc ...
Now, given your input system, I just use a vector of bindings. I.e.

typedef std::function< void ( Controller& ctrl, InputActions& outActions ) >  ActionBinding_t;
typedef std::vector< ActionBinding_t > BindingVector_t;

BindingVector_t  bindings;
bindings.push_back( []( Controller& ctrl, InputActions& outActions ){ if( ctrl.GetKey( Key::A ) ) outActions.Forward = 1.0f; } );
So, basically you stick all your bindings in that vector and iterate it each loop. Now you can use any form of key/joystick/mouse whatever to action you want without so much worry that keys, joystick, mouse etc are all very different. The binding can do conversion work if needed and you can add remove controller types without much worry.

Share this post

Link to post
Share on other sites

I do something similar to you. I enumerate all devices and their controls (I've got libraries for pure Win32, DirectInput and RawInput).


I differ between digital values (interpreted as button presses) and analog values (joystick axes for example). Analog axes are also mapped to digital (with threshold).


All these virtual buttons and axes can then be assigned to different actions; customizable by the player. The player can then map any buttons at will (mix joystick, keyboard, mouse).


Fun fact: Mapping the analog axis to digital lets me play jump ´n´ runs with the mouse, and it's actually playable :)

Share this post

Link to post
Share on other sites

Not sure if I missed something, but I think my problem's a little before that.


This is my input process:


 ____              ______               ________
| OS |--inputMsg--| Game |--inputCode--| Engine |    _______________
|____|  (Windows) |______|    (int)    |________|---| Input         |
  |                                                 | keyboard[256] |
 _|______________                                   | mouse[16]     |
| keyboard/mouse |                                  |_______________| 
|________________|                                  (action mapping is here)
            (these are working the way it is now)


I'm thinking of what would happen if the input isn't a keyboard/mouse, if it's a joystick (still haven't implemented it), or if it's a different OS (iOS for example), how could I make the above code work.


 _________                      ______                 ________
| ?? (OS) |-----inputCode------| Game |--controllers--| Engine |   _______________________
|_________|(this must be known)|______|---inputCode---|________|--| Input                 |
   |                              |                               | does what it must do  |
 __|_________                     |(builds per game)              | but with (controller) |
| ?? (input) |                 ___|_________                      | generated class       |
|____________|                | Controllers |                     | from "Game"           |
                              |_____________|                     |_______________________|
                              (action mapping would be here)


So I think I'm trying to figure how you come up with the Controller class you mention, what must it have since they're all different.

Share this post

Link to post
Share on other sites
Guest Hiwas

So I think I'm trying to figure how you come up with the Controller class you mention, what must it have since they're all different.


Actually, the controller class doesn't need to exist at all, I was just using it as a simplified example.  In reality, you can change the entire thing to be completely generic using the bindings.  Change the given code as follows:

typedef std::function< void ( void ) > ActionBinding_t;
typedef std::vector< ActionBinding_t > BindingVector_t;

BindingVector_t bindings;
bindings.push_back( [&](){ if( inputSystem.Keyboard().GetKey( Key::A ) ) inputSystem.ActionStructure().Forward = 1.0f; } );

Using the lambda is of course another lazy example case, a more interesting solution for say putting this in a table somewhere would be:

void mapJoystickToAction( JoystickInput& ji, InputActions& output )
{  .... blah blah ... }
 std::bind( &someFunction, joystickInput, outputAction )

The trick here is that your bindings can take any objects needed to completely reshape how the input of any controller type maps into the output actions.  You don't even need the actions to map into the same structure at all, say for instance the 'escape' key maps directly to a game object function "popupMenu" or something, you can do whatever you want with this.  Of course the other nice bit is that you can fill in a table with all these bindings so they can be easily presented to the user.  Perhaps make the joystick action a bit more generic such that it takes in an axis, tolerance, etc and the output float in the action structure and then you can present the user with "map which joystick axis to move left?" and they can make up==left, 'a'==right and your game code is not going to give a damn how the inputs get into the structure.


Note, if you don't see how the bind works, look up "std::bind composition" and that should give you an idea of the fancy fun you can have with bind.  I used to do this stuff with pointer to member functions, but the flexibility of std::bind is well worth the slight hit to performance in 90+% of all cases.  And as always, if you notice this code in a profile, you have other issues to worry about. smile.png

Edited by Hiwas

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!