SFML community forums

Help => Window => Topic started by: paullik on October 25, 2012, 04:43:48 pm

Title: [SOLVED] Handling mouse input outside of Window::GetEvent()
Post by: paullik on October 25, 2012, 04:43:48 pm
Hi.

I'm trying to create a Player class so I can do Player::GetInput() in the main loop and leave that class worry about mouse, keyboard, joystick or whatever, at the moment Player::GetInput() should somehow handle mouse clicks and return the click's position.

The main loop looks something like:
while(window.IsOpened()){
        while(window.GetEvent(event)){
            if(event.Type == sf::Event::Closed){
                window.Close();
            }
            else if(event.Type == sf::Event::MouseButtonReleased
                    && event.MouseButton.Button == sf::Mouse::Left){
                pos = std::make_pair(event.MouseButton.X, event.MouseButton.Y);
            }
        }

        window.Clear();
        window.Display();
}
 

What I want to do is:
while(window.IsOpened()){
        while(window.GetEvent(event)){
            if(event.Type == sf::Event::Closed){
                window.Close();
            }
        }

        pos = player.GetInput(window); //player is an instance of Player

        window.Clear();
        window.Display();
}
 

But if I move that "else if" part of the window.GetEvent() loop in Player::GetInput() I won't get any input because I already popped everything on the stack in the main loop, thus the stack is empty when calling player.GetInput().

I also tried to use window.GetInput(), Input::IsMouseButtonDown() along with Input::GetMousex(), Input::GetMouseY(), but this isn't a good choice for getting mouse click positions because if I click somewhere the application registers 2-3-4 clicks when in fact I clicked once (I also tried to set window.EnableKeyRepeat(false) but that seems to work just for keyboard).

How should I implement Player.GetInput() so I get mouse click positions outside the main loop and only getting the click position, no more, no less?
Title: Re: Handling mouse input outside of Window::GetEvent()
Post by: eXpl0it3r on October 25, 2012, 04:48:09 pm
The best way would be to update to SFML 2, because SFML 2 uses two separated systems (sf::Mouse/sf::Keyboard/sf::Joystick + events).

If you can't/don't want to, then I guess you could just get the input object outside the loop and work with it inside the loop.
Title: Re: Handling mouse input outside of Window::GetEvent()
Post by: cire on October 25, 2012, 06:18:54 pm
It seems to me what you really want to do is have the Player object process some events, so why not add a member function to Player that does just that?

void Player::ProcessEvent( const sf::Event & event )
{
    // ...
}

// ...

    while(window.IsOpened()){
        while(window.GetEvent(event)){
            if(event.Type == sf::Event::Closed){
                window.Close();
            }
            else // player processes events we don't want to process here.
                player.ProcessEvent(event) ;
        }

        window.Clear();
        window.Display();
    }

// ...
 
Title: Re: Handling mouse input outside of Window::GetEvent()
Post by: paullik on October 25, 2012, 07:35:35 pm
@eXpl0it3r:
Ok, good to know about input handling in v2, but I won't make the switch right now.
And I didn't really get what you meant by getting the object outside the loop and using it inside...

@cire:
Thank you, I did what you suggested!


Marked as solved!