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?
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();
}
// ...