Hi everybody,
I am thinking in how to implement my game engine and started with the input system. I read http://www.gamedev.net/blog/355/entry-2250186-designing-a-robust-input-handling-system-for-games/ and came up with this. What i have is an InputManager that handles SDL way of input and send them to a list of InputContexts. Each InputContext has a pointer to IInputListenner, that is the one who handles the input, but the InputContext has the role of translate the InputEvent object into a game message. And is here that i dont know how to really do it. I thought put the information needed by InputContext inside a XML file, but i dont know how to parse a XML into a generic type of data, which may have one, two or three values inside it and pass it to game logic.
My problem isn't how to load a XML file, it is how do i create a generic MessageObject that my game can handle.
InputManager.h:
class InputManager
{
public:
static InputManager* getInstance();
void dispatchEvents();
void registerContext(InputContext* c);
void deregisterContext(InputContext* c);
private:
InputManager();
static InputManager* instance;
std::list<InputContext*> contexts;
}
The only interesting function is the dispatchEvent(). which is called every frame:
void InputManager::dispatchEvents()
{
SDL_Event event;
while(SDL_PollEvent(&event) != 0)
{
InputEvent inputEvent;
std::map<unsigned int, InputEventType>::iterator it = inputEventMap.find(event.type);
if(it == inputEventMap.end())
{
std::cout<<"Unknown Event"<<std::endl;
continue;
}
inputEvent.type = it->second;
inputEvent.timestamp = event.common.timestamp;
if(inputEvent.type == KEYBOARD)
{
inputEvent.key = mapKeyInput(event);
}
else if(inputEvent.type == MOUSE)
{
inputEvent.mouse = mapMouseInput(event);
}
for(std::list<InputContext*>::iterator it = contexts.begin(); it != contexts.end(); it++)
{
if((*it)->dispatch(inputEvent))
{
break;
}
}
}
}
Here is a possible syntax for the XML :
<?xml version="1.0" encoding="ISO-8859-1"?>
<context>
<key value="W" pressed="true" message="MOVE_FORWARD"/>
<key value="SHIFT + W" pressed="true" message="RUN_FORWARD"/>
...
<mouse value="MOTION" message="CAMERA_MOTION">
<param value="X" inverted="false"/>
<param value="Y" inverted="false"/>
</mouse>
</context>
i dont like the idea of passing the messages as string because string comparison isnt efficient. I am open to suggestions.
Thanks in advance.