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

Author Topic: sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent  (Read 1857 times)

0 Members and 1 Guest are viewing this topic.

r4zzz4k

  • Newbie
  • *
  • Posts: 4
    • View Profile
sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent
« on: July 31, 2012, 03:36:10 pm »
First of all, I want to express my gratitude and respect to developers of this really great library. I find it quite easy-to-use and at the same time very powerful. Great job! :)

I'm doing small framework to make a GUI based on Graphics package. After polling new event I check it's source (keyboard or mouse). If it's related to mouse, I get cursor position and use it onwards.
window.pollEvent(event);
sf::Vector2i mousePointer(event.mouseMove);
Control* curUnderMouse = nullptr;
...
for(auto control: m_controls)
{
    if(control->hitTest(mousePointer))
    {
        curUnderMouse = control;
        break;
    }
}
 
I already realized my fault that event.mouseMove and event.mouseButton store cursor coordinates differently.
struct MouseMoveEvent
{
    int x;
    int y;
};
struct MouseButtonEvent
{
    Mouse::Button button;
    int x;
    int y;
};
 
I've corrected my error, but here is my question: won't be better to declare MouseButtonEvent as
struct MouseButtonEvent
{
    int x;
    int y;
    Mouse::Button button;
};
 
to make user code simpler in many situations? Just because these structs are in union, there will be no need to check mouse event type just to get mouse cursor position.

Anyway, it's really great library and I'm looking forward it's further development. And sorry for my English :)
« Last Edit: July 31, 2012, 04:26:26 pm by Laurent »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent
« Reply #1 on: July 31, 2012, 03:49:36 pm »
What does the declaration order of the members change?

Do you want to access x and y of a MouseButtonEvent even when accessing through MouseMoveEvent? This is a bad idea -- reading from every union member except the one written most recently results in undefined behavior.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

r4zzz4k

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent
« Reply #2 on: July 31, 2012, 04:00:32 pm »
Thanks for really fast answer!
I realize that it can be an undefined behavior, but this works in gcc (4.7) and msvc (2010 express), and probably on other compilers due to representation of this structures inside of union in memory. Moreover, I've seen such assumptions in several other unrelated frameworks. I'm going to look into the C++ Standard and find some information there.

r4zzz4k

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent
« Reply #3 on: July 31, 2012, 04:18:43 pm »
Paragraph 9.5.1 of C++ Standard says:
Quote
one special guarantee is made in order to
simplify the use of unions: If a POD-union contains several POD-structs that share a common initial
sequence (9.2), and if an object of this POD-union type contains one of the POD-structs, it is permitted to
inspect the common initial sequence of any of POD-struct members
Because these structs contain only ints and enum, they are PODs. So it's really legal to use both sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent no matter of what kind of mouse event is got.
So this change to members' order will really simplify user code a bit.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent
« Reply #4 on: July 31, 2012, 04:28:53 pm »
Reading another event's members is not a "simplification", in my opinion it is just lazy and ugly. Forcing programers to do the right thing is better, even if it requires a few more lines of code ;)
Laurent Gomila - SFML developer

r4zzz4k

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: sf::Event::MouseMoveEvent and sf::Event::MouseButtonEvent
« Reply #5 on: July 31, 2012, 04:32:19 pm »
Maybe you're right. Anyway thanks for taking the time to answer and good luck with the project development :)

 

anything