Hey, I too have thought about this.
I came up with a solution, though long before I swtiched to SFML.
Here's the basic idea (adjusted to use SFML input style):
Have a variable in the player class for each possible command, i.e. MoveForward
Declare them of type sf::Key::Code, like so:
sf::Key::Code moveForwardKey;
sf::Key::Code moveBackwardKey;
Then, at runtime, read a settings file, or have a settings screen at which you pick the input. Say, I want the 'W' key to be my move forward. Then, after figuring that out, you would make moveForwardKey equal to W (from the sf::key:: namespace), like so:
moveForwardKey = sf::Key::W;
And finally, when handling the input, simply check the input key vs your variable, like this:
sf::Event Event;
while (App.GetEvent(Event))
{
// If a key is pressed
if (Event.Type == sf::Event::KeyPressed)
{
// and that key code is equivalent to whatever we wanted our MoveForwardKey to be
if (Event.Key.Code == Player1.moveForwardKey)
{
// call the function to move player 1 forward here
}
if (Event.Key.Code == Player2.moveForwardKey)
{
// call the function to move player 2 forward here
}
}
}
That's the method I've used in the past.
Please note that each Player instance has all the variables (MoveForwardKey, MoveBackwardsKey etc), but they each can be assigned different values at runtime to handle differently.
I'm sort of assuming an OOP approach here, but it's doable without OOP as well.
I hope this answers your question.
- Zenon