• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Handling Input as Generic Data (Multiple Inputs)

4 posts in this topic

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

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

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 AllEightUp

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  
Followers 0