Key binding in C++
Im trying to make a small game and the only thing the book i have hasnt helped me with is key binding.
Im programmming in C++ so can anyone help me please!
You could try using an std::map with the character from the keyboard as the key, and a pointer to a function as the value.
For example: (psuedocode)
That won''t work as-is, it would require a bit of tinkering with it, but it should get you where you need to be. There may be an easier way, but that''s just off the top of my head.
For example: (psuedocode)
typedef std::map< unsigned char, (void *)funcPtr > BindMapBindMap keyMapping;keyMapping[''A''] = MoveLeft();keyMapping[''S''] = MoveDown();keyMapping[''D''] = MoveRight();keyMapping[''W''] = MoveUp();
That won''t work as-is, it would require a bit of tinkering with it, but it should get you where you need to be. There may be an easier way, but that''s just off the top of my head.
Also, you''d probably want to be able to load the bindings from a config file, like in Quake or Half-Life, in which case you could just do something like:
char c;while( c = GetKeyBindingFromConfig() ){KeyMapping[c] = GetFunctionBindingFromConfig();}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement