SFML community forums

Help => General => Topic started by: sirchick on November 09, 2012, 09:59:25 pm

Title: Get mouse position without event
Post by: sirchick on November 09, 2012, 09:59:25 pm
Hey

I have a slight issue im stuck with .... i have a button which when the mouse hovers over the button the image changes - this works perfectly fine.

How ever if i add a button pressed event the button no longer thinks the mouse is hovering over it. I'm not sure why! It seems i can't do both events at the same time...

This is my event loop in my main loop:

        bool click = false;
        int mouseX;
        int mouseY;
    // Start the game loop
     while (window.isOpen())
     {
         // Process events
         sf::Event event;
         while (window.pollEvent(event))
         {
             if (event.type == sf::Event::Closed) {
                 window.close();
                         }else{
                                 if(event.type == sf::Event::MouseButtonPressed){
                                          click = true;
                                 }
                                 if(event.type == sf::Event::MouseMoved){
                                          mouseX = event.mouseMove.x;
                                          mouseY = event.mouseMove.y;
                                }
                         }
                 }

         // Clear screen
         window.clear();

           btn_quit.RenderBttn(window,mouseX,mouseY,button,button_on,click);
          window.display();
 


Basically if i do a mouseButtonPressed event - the mouse position seems to be lost. Any reason this could happen ?

EDIT: i did a quick check and what occurs. When i do a mouseButtonPressed event, the mouse position becomes 0 : 0, i don't know why how ever.
Title: Re: Get mouse position without event
Post by: mercurio7891 on November 09, 2012, 10:05:33 pm
Why not use the X and Y field in the mouse button event to check for mouse coords too?

if(event.type == sf::Event::MouseButtonPressed)
{
        click = true;
        mouseX = event.mouseButton.x;
        mouseY = event.mouseButton.y;
}
if(event.type == sf::Event::MouseMoved)
{
        mouseX = event.mouseMove.x;
        mouseY = event.mouseMove.y;
}
 
Title: Re: Get mouse position without event
Post by: sirchick on November 09, 2012, 10:08:20 pm
EDIT: Ah that did work i just changed it to an Else If and seems to do the trick :)
Title: Re: Get mouse position without event
Post by: didii on November 11, 2012, 01:08:37 am
To answer to your question of the topics name:
You can get the position of the mouse with the command sf::Mouse::getPosition(). These are the mouse coordinates relative to the desktop. If you want it to be relative to your window, use sf::Mouse::getPosition(sf::RenderWindow) and it'll change relative to your window.