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

Author Topic: Event handling  (Read 1806 times)

0 Members and 1 Guest are viewing this topic.

andrei186

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Event handling
« on: September 30, 2019, 08:25:26 pm »
Using CodeBlock compiled the following code to test the idea:  on mouse Click move a red dot to the cursor:
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(1024, 768), "Test");
    sf::RectangleShape bullet(sf::Vector2f(5.0f, 5.0f));
    bullet.setFillColor(sf::Color::Red);

    while (window.isOpen())
    {
       sf::Event event;
       while (window.pollEvent(event))
       {
            if (event.type == sf::Event::Closed)
                window.close();
       }
        if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
        {
            sf::Vector2i mousePos = sf::Mouse::getPosition(window);
            bullet.setPosition((float) mousePos.x, (float)mousePos.y);
        }
        window.clear();
        window.draw(bullet);
        window.display();
    }
    return 0;
}

It works fine with the mouse on Win XP, 7, 8 and 10.

However the final objective is to make a program which could be controlled on a big screen with a laser pointer. To this end a web camera is used for which a program is written in C# using NET Framework 4.5. This program receives frames from the webcam, locates a laser dot and emulates mouse click event.

On Windows XP, 7 and 8  this works fine. However on Win 10 it does not. It moves the cursor to the laser dot, yet does not move the red dot as if the mouse click event does not come through.

Other standard Windows applications like Paint or Windows Explorer react to the laser beam properly therefore the problem seems to be in misunderstanding between my SFML code and Windows-10.

What could be wrong?
« Last Edit: September 30, 2019, 08:34:02 pm by andrei186 »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Event handling
« Reply #1 on: September 30, 2019, 11:42:08 pm »
Have you tried checking to see if you are getting the mouse click event instead of relying on the real-time state?

if (event.type == sf::Event::MouseButtonPressed)
 

andrei186

  • Newbie
  • *
  • Posts: 26
    • View Profile
    • Email
Re: Event handling
« Reply #2 on: October 01, 2019, 09:10:20 pm »
Thank you, Arcade,
Compiled with
if (event.type == sf::Event::MouseButtonPressed)
instead of
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) as you suggested

Same story unfortunatelly. Still good to learn an alrternative way to handle this mouse event

PS. searching about sf::Event::MouseButtonPressed came across a relevant topic titled
 Mouse::isButtonPressed(Mouse::Left) vs (event.mouseButton.button == Mouse::Left) here:
https://en.sfml-dev.org/forums/index.php?topic=21084.0

Tried
if(event.mouseButton.button == sf::Mouse::Left)
it worked!

Read that topic twice as well as two relevant articles referred to there:
Events explained
www.sfml-dev.org/tutorials/2.4/window-events.php
Keyboard, mouse and joystick
www.sfml-dev.org/tutorials/2.4/window-inputs.php

and still not sure if I understand why it worked while
if (event.type == sf::Event::MouseButtonPressed)
and
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)) 
did not.

I obviously lack some fundamental understanding of C++ and SFML. While I am trying to get it, perhaps someone can explain me in layman terms what causes that big difference between those event handlings and why this difference shows itself on Windows-10 and is ignored by Windows XP, 7 and 8 ?
« Last Edit: October 02, 2019, 04:45:49 pm by andrei186 »

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: Event handling
« Reply #3 on: October 01, 2019, 09:57:55 pm »
It isn't clear from your post, but note that I wasn't suggesting you directly replace that one if statement with the other in the same line of code. I was suggesting that you should check for the event in your event loop. I'm not sure if you did this.
while (window.pollEvent(event))
{
     if (event.type == sf::Event::Closed)
         window.close();
     else if (event.type == sf::Event::MouseButtonPressed)
     {
         // DO STUFF HERE
     }
}
 

If you want to explicitly check if the left mouse button was pressed, you need to FIRST make sure event.type == sf::Event::MouseButtonPressed, and THEN ALSO check if event.mouseButton.button == Mouse::Left. Be sure to carefully read the event tutorial you linked again. It gives an example of this. You will get undefined behavior if you check event.mouseButton.button without first making sure the event.type is sf::Event::MouseButtonPressed.

Now, regarding sf::Mouse::isButtonPressed(sf::Mouse::Left). That function is used to check the current state of the button. If the mouse click is very fast, it's possible that the mouse button went down and then back up before sf::Mouse::isButtonPressed(sf::Mouse::Left) is ever called. In other words, it's possible to miss fast button presses. I'm simply speculating that this might be the case for you for whatever reason on Windows 10. Using events instead would solve that problem.

 

anything