I wish to be able to hash sf::Event in a reliable manner, because I want to use events as keys in std::unordered_map. This is the best I've come up with so far:
int sfhash(const sf::Event & e) {
int seed = 1000000 * e.type;
int mod = 1;
switch (e.type) {
case sf::Event::KeyPressed:
seed = seed + e.key.code;
break;
case sf::Event::KeyReleased:
seed = -seed - e.key.code;
mod = -1;
break;
case sf::Event::MouseButtonPressed:
seed = seed + e.mouseButton.button;
break;
case sf::Event::MouseButtonReleased:
seed = -seed - e.mouseButton.button;
mod = -1;
break;
default:
break;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LControl)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::RControl))
{
seed += mod * 1000;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LShift)
|| sf::Keyboard::isKeyPressed(sf::Keyboard::RShift))
{
seed += mod * 2000;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::LAlt)) {
seed += mod * 3000;
}
return seed;
}
Can you tell me a reason why this will fail, or if there is a much simpler way to do it that I didn't think of?