Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - burikko

Pages: [1]
1
General / Re: Hashing sf::Event
« on: August 02, 2018, 03:04:07 pm »
I actually read about that on Google before trying it myself, but since sf::Event seems to use union for its members I don't know how to hash all the members like you say. Good point about the modifiers!

2
General / Hashing sf::Event
« on: August 02, 2018, 01:54:37 pm »
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?

Pages: [1]
anything