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

Author Topic: waitEvent is not working as i expected  (Read 8810 times)

0 Members and 1 Guest are viewing this topic.

barnack

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
waitEvent is not working as i expected
« on: July 28, 2018, 03:09:06 am »
I thought using waitEvent instead of Poll event in the exact same cycle would make my program block until an event is recived (like a mouse click). Instead whatever i do the program is stuck there and does nothing...
int window_mode()
        {
        sf::RenderWindow window(sf::VideoMode(800, 600), "GraphMaker");
        while (window.isOpen())
                {
                // Process events
                sf::Event event;
                //while (window.pollEvent(event))
                while (window.waitEvent(event))
                        {
                        // Close window: exit
                        switch (event.type)
                                {
                                case sf::Event::Closed:
                                        window.close();
                                        break;

                                case sf::Event::MouseButtonPressed:
                                        //do stuff
                                        break;
                                case sf::Event::MouseButtonReleased:
                                        //do stuff
                                        break;
                                case sf::Event::MouseMoved:
                                        //do stuff
                                        break;
                                       
                                }
                        }

                // CLS
                window.clear();

                // DRAW
                //draw stuff
                window.display();
                }

        return 0;
        }

Compiling with visual studio, if that matters.
So i'm wrong thinking it should work this way or there's something else going wrong?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: waitEvent is not working as i expected
« Reply #1 on: July 28, 2018, 07:17:53 am »
while (window.waitEvent(event)) is basically an infinite loop that waits for an event. When there is an event, it runs what's inside the while. When there isn't an event, it does nothing but wait. You never go outside this while loop.

Maybe you would want to use an "if" instead of a "while"?

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: waitEvent is not working as i expected
« Reply #2 on: July 28, 2018, 08:50:10 am »
There has to be some other issue. What you want is exactly what should happen with that code in place.

@G.: It shouldn't make a difference. Only difference would be whether you'd want to process one or multiple events at once (the latter typically being the better choice).

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: waitEvent is not working as i expected
« Reply #3 on: July 28, 2018, 10:23:43 am »
No G. Is right, waitEvent only returns false if there's an error, so you're stuck in the while loop: https://www.sfml-dev.org/documentation/2.5.0/classsf_1_1Window.php#aaf02ab64fbc1d374eef3696df54137bc
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: waitEvent is not working as i expected
« Reply #4 on: July 28, 2018, 11:07:54 am »
Hm, never noticed that. :O

barnack

  • Jr. Member
  • **
  • Posts: 56
    • View Profile
Re: waitEvent is not working as i expected
« Reply #5 on: July 28, 2018, 12:05:43 pm »
wuoldn't it be a lot more intuitive to let waitEvent work with the same code of pollEvent by just replacing it?
________

nevermind i just realized it'd be stupid
« Last Edit: July 28, 2018, 12:22:46 pm by barnack »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: waitEvent is not working as i expected
« Reply #6 on: July 28, 2018, 06:38:48 pm »
wuoldn't it be a lot more intuitive to let waitEvent work with the same code of pollEvent by just replacing it?
________

nevermind i just realized it'd be stupid

It somewhat makes sense (I also made that mistake in the SFML example app). But considering the function's behavior this simply doesn't make any sense (since it should always return true).

However, you can essentially emulate that behavior by using (waitEvent(event), true).