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

Author Topic: Get mouse position without event  (Read 2679 times)

0 Members and 1 Guest are viewing this topic.

sirchick

  • Newbie
  • *
  • Posts: 13
    • View Profile
Get mouse position without event
« 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.
« Last Edit: November 09, 2012, 10:08:38 pm by sirchick »

mercurio7891

  • Jr. Member
  • **
  • Posts: 89
    • View Profile
Re: Get mouse position without event
« Reply #1 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;
}
 

sirchick

  • Newbie
  • *
  • Posts: 13
    • View Profile
Re: Get mouse position without event
« Reply #2 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 :)
« Last Edit: November 09, 2012, 10:11:14 pm by sirchick »

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: Get mouse position without event
« Reply #3 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.

 

anything