SFML community forums

Help => Window => Topic started by: oni2180 on June 17, 2015, 07:55:43 pm

Title: Mouse wheel moved issue
Post by: oni2180 on June 17, 2015, 07:55:43 pm
Hello,
I have a problem with the MouseWheelMoved event but not only.
When I do this:

if(event.type == Event::MouseWheelMoved){
            std::cout << "wheel movement: " << event.mouseWheel.delta << "  " << event.mouseWheel.x << "  " << event.mouseWheel.y << std::endl;
        }

nothing happens when I turn the wheel (even very fast), so it does not enter the if().
When I get the if() off and just type :

std::cout << "wheel movement: " << event.mouseWheel.delta << "  " << event.mouseWheel.x << "  " << event.mouseWheel.y << std::endl;

I get several different data depending on the action. For the example, take x=125 and y=112. When just moving the mouse, I have the X value inside delta, the Y value inside the X and some very long number (looks like an adress) in the Y : 125  112  2002418353

If I click, I get the click value (0, 1 or 2 depending on the button) inside the delta and X and Y are normal : 1  125  112

Finally, when I rotate the wheel I have 0 in delta, some positive adress when up and negative when down in X, and X in Y : 0  106535816  125 when up and 0  -1082130432  125 when down.

Bonus : When offscreen : -21, then 0 and then an adress : -21  0  12089408.

Did I make a mistake inside the code? Is that normal? How should I access the good wheel numering?
Title: Re: Mouse wheel moved issue
Post by: G. on June 17, 2015, 08:11:14 pm
Is it inside the event loop?
PS: if you're using SFML 2.3, use the MouseWheelScrolled event instead.
Title: Re: Mouse wheel moved issue
Post by: Jesper Juhl on June 17, 2015, 08:16:33 pm
When I get the if() off and just type :

std::cout << "wheel movement: " << event.mouseWheel.delta << "  " << event.mouseWheel.x << "  " << event.mouseWheel.y << std::endl;
That is completely wrong. sf::Event is a union and accessing any member (except type) leads to undefined behaviour (http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html) if the accessed member does not match the actual type. You MUST check event.type before accessing any other member and then only access members that are actually valid for that type.

See also: A Guide to Undefined Behavior in C and C++ (http://blog.regehr.org/archives/213)
Title: Re: Mouse wheel moved issue
Post by: oni2180 on June 17, 2015, 08:31:22 pm
Is it inside the event loop?
PS: if you're using SFML 2.3, use the MouseWheelScrolled event instead.
It is not inside the loop, thanks, such a newbie error !

When I get the if() off and just type :

std::cout << "wheel movement: " << event.mouseWheel.delta << "  " << event.mouseWheel.x << "  " << event.mouseWheel.y << std::endl;
You MUST check event.type before accessing any other member and then only access members that are actually valid for that type.

See also: A Guide to Undefined Behavior in C and C++ (http://blog.regehr.org/archives/213)
You are absolutely right, thank you