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

Author Topic: mouse button released is spammed?  (Read 3360 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
mouse button released is spammed?
« on: October 06, 2014, 10:38:28 pm »
I am using SFML and C++ and I am getting an odd problem,

Here is my main game update method

   
while (renderService.Window.isOpen())
        {
                //Poll events
                sf::Event event;
                while (renderService.Window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                renderService.Window.close();
                        running = false;
                }

                MouseMovment(event);
                MouseClick(event);
                Update();
                Draw();
        }

and here is my MouseClick method

   
void Game::MouseClick(sf::Event event)
    {
        sf::Vector2i position = sf::Mouse::getPosition(renderService.Window);
       
            if (event.mouseButton.button == sf::Mouse::Left && event.type == sf::Event::MouseButtonReleased)
            {
                    std::cout << "Mouse released" << std::endl;
        }
    }

now here is the weird part, in my console sometimes my cout will be spammed like 10/20 times, but sometimes it will work perfectly, am I calling the event wrongly?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: mouse button released is spammed?
« Reply #1 on: October 06, 2014, 10:43:13 pm »
and here is my MouseClick method

   
void Game::MouseClick(sf::Event event)
    {
        sf::Vector2i position = sf::Mouse::getPosition(renderService.Window);
       
            if (event.mouseButton.button == sf::Mouse::Left && event.type == sf::Event::MouseButtonReleased)
            {
                    std::cout << "Mouse released" << std::endl;
        }
    }
You are accessing members of "event" without first testing if it is the right type.
Accessing a non-active member of a union is undefined behaviour in C++ - meaning; anything could happen. You need to check the type and then only access members that are valid for the specific type.
Your "event.type == sf::Event::MouseButtonReleased" check needs to be first.
« Last Edit: October 06, 2014, 10:45:05 pm by Jesper Juhl »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: mouse button released is spammed?
« Reply #2 on: October 06, 2014, 10:48:17 pm »
Yes.

The event object is meaningful after a call to pollEvent returns true.  Aside from that, it's probably just going to "remember" its previous value.  With the code you have right now, it's probably becoming a MouseReleasedEvent when you release the mouse and then remaining that type for several frames where no events occur, making your MouseClick function print several times (of course, it's actually undefined behavior, so this is only one possible explanation).

The correct way to handle events is to do all of the initial processing in the while(pollEvent) loop, like this:
        sf::Event event;
        while (renderService.Window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) {
                renderService.Window.close();
            } else if (event.type == sf::Event::MouseReleased) {
                MouseClick(event);
            } else if (event.type == sf::Event::MouseMoved) {
                MouseMovment(event);
            }
        }

        Update();
        Draw();
 
It's up to you whether you immediately respond to the event with a MouseClick() function or simply set a hasMouseBeenClicked flag to be processed later in the game loop, but any code involving "event.type == sf::Event::..." needs to be in that while(pollEvent) block.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Re: mouse button released is spammed?
« Reply #3 on: October 06, 2014, 11:06:20 pm »
Cheers guys, Not sure why I was checking outside of the pollevent, seems quite stupid if you ask me :P

 

anything