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

Author Topic: SFML 2.0 mouse position  (Read 4772 times)

0 Members and 1 Guest are viewing this topic.

hvince95

  • Newbie
  • *
  • Posts: 4
    • View Profile
SFML 2.0 mouse position
« on: July 25, 2012, 11:46:55 am »
Hey, new to the forums and SFML in general.

Just trying to follow this "http://www.gamefromscratch.com/page/Game-From-Scratch-CPP-Edition.aspx" tutorial and porting it over to SFML 2.0 in the process.  I cannot for the life of me get the mouse position in part 3 to port.

here is the code I am trying to port:
sf::Event menuEvent;
if(menuEvent.type == sf::Event::MouseButtonPressed) {
return sf::Event::MouseButtonEvent.x;
return sf::Event::MouseButtonEvent.y;
}
Just ignore that I returned twice for the sace of the this.

How should I be doing this?  I tried a few variants:
sf::Event menuEvent;
if(menuEvent.type == sf::Event::MouseButtonPressed) {
return menuEvent.MouseButtonEvent.x;
return menuEvent.MouseButtonEvent.y;
}
But this doesn't work.  Any ideas?

And one more quick question, when should I be using '::' or when should I be using '.' a full stop?

Marukyu

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: SFML 2.0 mouse position
« Reply #1 on: July 25, 2012, 12:00:33 pm »
Hi,

I'm not entirely sure what exactly you're trying to make your function do here, but you cannot chain return calls like this. If you want your function to return the position at which the mouse button was clicked according to some sf::Event, return it as a vector, like this:
return sf::Vector2i(menuEvent.mouseButton.x, menuEvent.mouseButton.y);

Also, not sure if this is just stripped down code or an extract of what you're actually using, but if the latter is the case, you might have to change it use window.pollEvent(menuEvent) in a while()-loop to catch all events from an sf::Window (unless menuEvent is being passed as a parameter to the function, or something).

About the "::" vs "." question: accessing static class members or namespaces requires "::" to be used. The "." operator is for accessing members and functions of class instances / objects.

~ def8x

ninja edit: fixed a stupid mistake I made with the vector code

hvince95

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML 2.0 mouse position
« Reply #2 on: July 25, 2012, 12:10:34 pm »
Thankyou, and Yes, it was just an extract, here is the actual code:
sf::Event menuEvent;
while(true) {
        while(window.pollEvent(menuEvent)) {
                if(menuEvent.type == sf::Event::MouseButtonPressed) {
                        return HandleClick(menuEvent.MouseButtonEvent.x, menuEvent.MouseButtonEvent.y);
                }
        }
}
Where HandleClick is a function which takes two ints, and returns another value.
The thing is, the current code does not work, I'm not sure why.

I also tried doing:
return HandleClick(menuEvent.MouseButtonEvent.x, menuEvent.MouseButtonEvent.y);
but to no avail.

Marukyu

  • Newbie
  • *
  • Posts: 36
    • View Profile
Re: SFML 2.0 mouse position
« Reply #3 on: July 25, 2012, 12:13:16 pm »
Well, it's not easy to tell you what is going wrong if the only thing provided is a part of the main loop. It's likely related to the way you structured the rest of your code. Could you elaborate on "the code does not work"? What exactly does not work?

hvince95

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML 2.0 mouse position
« Reply #4 on: July 25, 2012, 12:54:51 pm »
Sorry,
using the
menuEvent.MouseButtonEvent.x
version, it throws an error (in codeBlocks):
error: invalid use of 'struct sf::Event::MouseButtonEvent'

This is during compilation.
« Last Edit: July 25, 2012, 12:56:23 pm by hvince95 »

Sir Wolf

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: SFML 2.0 mouse position
« Reply #5 on: July 25, 2012, 01:45:46 pm »
Sorry,
using the
menuEvent.MouseButtonEvent.x
version, it throws an error (in codeBlocks):
error: invalid use of 'struct sf::Event::MouseButtonEvent'

This is during compilation.

Try menuEvent.mouseButton.x instead.
"We don't stop playing because we grow old; we grow old because we stop playing."
-George Bernard Shawn

hvince95

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: SFML 2.0 mouse position
« Reply #6 on: July 25, 2012, 01:51:51 pm »
thanks very much.

The problem was I had MouseButton, not mouseButton.

Thanks for your help!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: SFML 2.0 mouse position
« Reply #7 on: July 25, 2012, 01:55:29 pm »
The problem was I had MouseButton, not mouseButton.
You should make more use of the great documentation! ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/