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

Author Topic: Mouse wheel moved issue  (Read 2367 times)

0 Members and 1 Guest are viewing this topic.

oni2180

  • Newbie
  • *
  • Posts: 2
    • View Profile
Mouse wheel moved issue
« 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?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Mouse wheel moved issue
« Reply #1 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.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Mouse wheel moved issue
« Reply #2 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 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++
« Last Edit: June 17, 2015, 08:22:48 pm by Jesper Juhl »

oni2180

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Mouse wheel moved issue
« Reply #3 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++
You are absolutely right, thank you
« Last Edit: June 17, 2015, 08:34:29 pm by oni2180 »

 

anything