SFML community forums

Help => General => Topic started by: natchos on October 23, 2012, 09:15:07 am

Title: Mouse input
Post by: natchos on October 23, 2012, 09:15:07 am
Hey I was wondering whether there is something wrong with the SFML mouse input class.
When I get input from the keyboard I use a pollEvent and a switch type structure

while(App.pollEvent(Event))
{
        switch(Event.type)
        {
                case sf::Event::Closed:
                        App.close();
                        break;
                case sf::Event::KeyPressed:
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                //Do stuff
                        }
                        break;
        }
}

And it works fine.

However attempting to put in a
case sf::Event::MouseButtonPressed: doesn't work.
In my application I did it like this

while(App.pollEvent(Event))
{
        switch(Event.type)
        {
                case sf::Event::Closed:
                        App.close();
                        break;
                case sf::Event::KeyPressed:
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                //Do stuff
                        }
                        break;
                case sf::Event::MouseButtonPressed:
                        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
                        {
                                //Do stuff
                        }
        }
}

But it never triggered/worked. Instead I had to do this:

while(App.pollEvent(Event))
{
        switch(Event.type)
        {
                //Process events
        }
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
        //React to the mouse button
}
Am I doing an incorrect implementation or is it something else?
Title: Re: Mouse input
Post by: Laurent on October 23, 2012, 09:48:19 am
First, direct input and events are two totally different things, it doesn't make sense to use sf::Keyboard or sf::Mouse in an event loop. As explained in the tutorial.

And... how did you manage to get this crazy indentation? There are less than 10 lines of code but it's already unreadable ???

And please post a complete/valid piece of code, because those "if (...) // Do stuff   case ..." can be interpreted differently than what's really inside your program (the case is in the if) and won't help us to help you ;)
Title: Re: Mouse input
Post by: natchos on October 24, 2012, 05:06:59 pm
Well, as I said, the structure actually works fine for keyboard input. It hasn't given missed a single stroke so far. The reason for the crazy indentation was that since the actual structure is some hundred or so lines long, I didn't feel like burdening you with all the unneccesary code, and instead just copy pasted. I however discovered that attempting to use tab to create indentations in the webpage didn't work so I instead attempted to use spaces. It obviously didn't work.

Edit: Fixed the original post.
Title: Re: Mouse input
Post by: eXpl0it3r on October 24, 2012, 05:14:14 pm
Well, as I said, the structure actually works fine for keyboard input.
So? Everyone gets lucky once. ;)

It seems like the event and real-time system have an equally fast reaction, where as for the mouse input the real-time system is faster. But that's just some blind guessing and as Laurent said, the systems are completely different and should not get mixed. Use the second version and you'll be find. :)