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

Author Topic: [SOLVED] Stuck in event loop when event is triggered  (Read 6554 times)

0 Members and 1 Guest are viewing this topic.

user1

  • Newbie
  • *
  • Posts: 5
    • View Profile
[SOLVED] Stuck in event loop when event is triggered
« on: April 05, 2016, 07:18:55 pm »
First I would like to apologies for my English.
I looked for similar problem in here and on net but couldn't find any so I decide to open new topic.

I have started poking around with SFML and tried to make simple object movement on screen, but from some reason look like execution of my code, when there is some event, get stuck in while window.pollEvent(event) loop, and can't update object position. If there is no event (no mouse movement or key pressed) everything works fine. I attached gif with example what happens.

OS: Linux Mint 17.3 Cinnamon 64-bit
Linux kernel: 4.4.0-13-generic
CPU: intel core i7-6700
RAM: 16 GB
Graphic card: GeForce GTX 960M 4GB
SFML: 2.3.2 downloaded from site (didn't tried compiling). Tested on 2.1 from mint repository as well.
IDE: NetBeans 8.1
C++ compiler: g++

Here is my code:
#include <SFML/Graphics.hpp>

int main() {
    sf::RenderWindow window(sf::VideoMode(640, 490), "SFML works!");

    sf::Texture bombTexture;
    bombTexture.loadFromFile("data/img/bomb.png");

    sf::Sprite bomb;
    bomb.setTexture(bombTexture);
    sf::Vector2f increment(0.4f, 0.4f);

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
        }

        if ((bomb.getPosition().x + bombTexture.getSize().x > window.getSize().x && increment.x > 0) ||
                (bomb.getPosition().x < 0 && increment.x < 0)) {
            increment.x = -increment.x;
        }

        if ((bomb.getPosition().y + bombTexture.getSize().y > window.getSize().y && increment.y > 0) ||
                (bomb.getPosition().y < 0 && increment.y < 0)) {
            increment.y = -increment.y;
        }

        bomb.setPosition(bomb.getPosition() + increment);

        window.clear();
        window.draw(bomb);
        window.display();
    }

    return 0;
}
 
To see what happens I changed my code a little bit and did this:
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
...
int i = 0;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            i++;
            cout << i << endl;
        }
...
 

it seems that there is triggered 50 - 70 events per sec. And that there is no execution of other code unless event stops (stop moving mouse or release pressed key). Every frame should reset counter (I think), but until event stops console print incremented number. If there is 2-3 sec of mouse movements console will print numbers from 1 to 180 – 200. This shows that program is not exiting loop.

I tried this code on my other computer and for some unknown reason it works fine, object moves no matter what or how many events are triggered.

OS: Linux Mint 17 Cinnamon 64-bit
Linux kernel: 3.13.0-24-generic
CPU: intel core2duo
RAM: 4 GB
Graphic card: Mobile Radeon HD 1GB
SFML: 2.3.2  downloaded from site
IDE: NetBeans 8.0
C++ compiler: g++

Am I doing something wrong or something changed in Linux kernel that affect this? Or is it my hardware?

EDIT:

Ok, here is update if anyone is interested. I can't tell what is it, but I can tell what it isn't.

Building SFML from code didn't fix problem.
Upgrade g++ to version 5 didn't fix problem.
Log in with kernel 3.19 didn't fix problem.
« Last Edit: April 08, 2016, 05:00:44 pm by user1 »

doingit

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Stuck in event loop when event is triggered
« Reply #1 on: April 06, 2016, 12:15:18 am »
I highly doubt you're stuck in the event loop.

To prove so, try printing your "i" value after window.display(), it should print the number of cycles the event loop triggered.
The event loop can trigger a great many amount of times within 200 ms, and you'd most likely wouldn't even feel a delay.

What I'm saying is that it is normal for the event loop to trigger a bunch of times. The program most likely isn't locking up from the event loop. As for the animation, not sure.

First try printing outside of the event loop and if it prints, the fault lies elsewhere.

Hope this helps.

user1

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Stuck in event loop when event is triggered
« Reply #2 on: April 06, 2016, 01:40:56 am »
Thanks for reply.

Here's what I did. I edited code like you suggested, but I added one more thing. Before window.isOpen() loop I added int frame = 0. First thing in window.isOpen() loop I increment variable frame. This way I get frame number. On the end of loop I print frame number and number of events in that frame.
...
    int frame = 0;
    while (window.isOpen()) {
        frame++;
...
        int eventPerFrame = 0;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            eventPerFrame++;
        }
...

        window.display();
        cout << frame << " -> " << eventPerFrame << endl;
    }
...
 

Strangest thing is, when mouse is moving console stop printing, and when I stop moving mouse frame number and events per frame are printed again, frame number is incremented by 1 and number of events is 10 – 200 depending how long I was moving mouse.

With keypress is different. In this case frame -> eventsPerFrame pair is sometimes printed, and value is in some cases around 70, sometimes around 120 or 200 although I'm still pressing a key. Frame number is incremented by 1.

In addition I tried this code on my third (and last) computer and same problem has appeared. Program seems stuck in loop when mouse is moving. So here is whole info I gather.

PC
Graphic card: Mobile Radeon HD 1GB
OS: Linux Mint 17 Cinnamon 64-bit
Linux kernel: 3.13
everything works perfect (1-5 events per frame)

Laptop 1 (Acer Aspire v17 Nitro Black Edition)
Graphic card: GeForce GTX 960M 4GB
OS: Linux Mint 17.3 Cinnamon 64-bit
Linux kernel: 4.4
program freezes (up to 300 events per frame)

Laptop 1 (Acer Aspire 5736Z)
Graphic card: Intel Mobile 4 Series Integrated Graphic Controller
OS: Linux Mint 17.2 Cinnamon 64-bit
Linux kernel: 3.16
program freezes (up to 300 events per frame)

What I'm trying to say here is that same code, same project, works on my PC but doesn't works on my laptops (both of them).
« Last Edit: April 06, 2016, 01:45:26 am by user1 »

doingit

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Stuck in event loop when event is triggered
« Reply #3 on: April 06, 2016, 06:13:39 am »
Hmm, you mentioned that when the mouse is moving, the event loop is triggered somewhat-infinitely, but the event loop shown above does not test against mouse or keys pressed.

Are you implementing key and mouse movement detection?
if you are, how are you doing so?

if you are not detecting for keys and mouse movements, and then I can only suggest using the latest release of SMFL.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Stuck in event loop when event is triggered
« Reply #4 on: April 06, 2016, 08:28:47 am »
So your only conclusion that it gets "stuck" in the event loop comes from the fact that a lot of events get triggered when you move the mouse?

If there is 2-3 sec of mouse movements console will print numbers from 1 to 180 – 200. This shows that program is not exiting loop.
Don't just assume something. Code does never arbitrarily execute or magically do things, but when something happens there's a logical reason. ;)

SFML has an event called MouseMove and depending on your DPI of your mouse that event can be triggered many times per second, it's not just one event per movement you make.

The more important question is, does that behavior cause any kind of issue for the actual execution of the application?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

user1

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Stuck in event loop when event is triggered
« Reply #5 on: April 06, 2016, 01:09:37 pm »
Thank you both for taking time to try to help me with this.

Are you implementing key and mouse movement detection?
if you are, how are you doing so?
I didn't implement any key or mouse movement detection. I really can't see way mouse effect my code in any way.

if you are not detecting for keys and mouse movements, and then I can only suggest using the latest release of SMFL.
I'm using SFML v2.3.2.

The more important question is, does that behavior cause any kind of issue for the actual execution of the application?
Program/game is not working or works slowly if there is any input from mouse/keyboard.
Let me give you example. Let's say that I have made pong game and I'm trying to play it on my laptop. If I repeatedly pressed left arrow to move my pedal game will work fine. But if I try to hold left arrow game will run really slow. In case I try to use mouse game will simply stop until I stop moving mouse.

Don't just assume something.
Well, I wouldn't call it assumption.
Here is code to explain what I'm trying to say. This is whole code, I'm not joking

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;

int main() {
    sf::RenderWindow window(sf::VideoMode(640, 490), "SFML works!");    
   
    int frame = 0;
    sf::Clock clock;
    sf::Event event;
    while (window.isOpen()) {
        frame++;
        int eventPerFrame = 0;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            eventPerFrame++;
        }
       
        window.clear();
        window.display();
       
        printf("frame = %d took %.6f sec\n", frame, clock.restart().asSeconds());
    }

    return 0;
}
and this is console output of that code
...
frame 54 took 0.031933 sec
frame 55 took 0.036067 sec
frame 56 took 0.027988 sec <- mouse start moving
frame 57 took 7.220153 sec <- mouse stops moving
frame 58 took 0.051939 sec
frame 59 took 0.032029 sec
frame 60 took 0.027931 sec
frame 61 took 0.039960 sec <- key pressed
frame 62 took 0.664005 sec
frame 63 took 0.180131 sec
frame 64 took 0.303995 sec
frame 65 took 0.060004 sec
...
frame 75 took 0.336035 sec
frame 76 took 0.151967 sec
frame 77 took 0.336002 sec
frame 78 took 0.124075 sec
frame 79 took 0.179965 sec
frame 80 took 0.459949 sec
frame 81 took 0.036032 sec <-- key released
frame 82 took 0.028049 sec
frame 83 took 0.027966 sec
frame 84 took 0.027885 sec
frame 85 took 0.028072 sec
...

Output on my PC looks like this:
... <- mouse start moving
frame = 557 took 0.000590 sec
frame = 558 took 0.018673 sec
frame = 559 took 0.017098 sec
frame = 560 took 0.036095 sec
frame = 561 took 0.020247 sec
frame = 562 took 0.007917 sec
frame = 563 took 0.021471 sec
frame = 564 took 0.028034 sec
frame = 565 took 0.000575 sec
... <- mouse stops moving

It executes on PC much faster even though mouse is moving.

Since it works fine on my PC I installed kernel 3.16 on it to see if that will make this code work same way on my PC as it does on laptop. Well, it still run smoothly on PC.
I will try upgrade my PC to Linux Mint 17.3 (when I find some time) to see if that will "broke" my code. There was changes in Cinnamon since Mint 17 so it might be it. Or something else. Time will show.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11030
    • View Profile
    • development blog
    • Email
Re: Stuck in event loop when event is triggered
« Reply #6 on: April 06, 2016, 02:01:00 pm »
Then I misunderstood you. A more understandable description of your problem probably would have been, that one frames sometimes takes up to multiple seconds and it most likely happens when polling events (it doesn't get stuck in a loop...). ;)

Best thing you can do is get the SFML debug libraries and a profiler/debugger and find out where the time is spent, but it sounds more like a WM/OS related issue.
Do you have a gamepad?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

user1

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Stuck in event loop when event is triggered
« Reply #7 on: April 06, 2016, 02:52:43 pm »
You are right, I express myself a wrong way. Program definitely didn't stuck, it's just delay execution.

I will try going through log first, maybe there is some trace of problem. If that doesn't work I'll try debug libraries.

Thanks for advice.

user1

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Stuck in event loop when event is triggered
« Reply #8 on: April 08, 2016, 04:58:57 pm »
I'm such a noob. I've removed SFML libraries that I installed from repository and first build and build debug libraries, and now it works fine.

Thanks guys for help

qwertywert

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: [SOLVED] Stuck in event loop when event is triggered
« Reply #9 on: April 06, 2017, 12:16:28 am »
Hey so i am having the exact same issue - when I move mouse over screen It gets stuck in the event polling loop and causes crazy fps drops, and my updating/drawing parts are never reached.

I am also on linux using Ubuntu 14.04.
So to fix this you just re-installed the libraries? Where did you get updated ones from github or sfml website?
Thanks.

Edit: Okay fixed, like you said I just needed to get the new 2.4.2 shared libs manually instead of doing 'sudo apt-get install libsfml-dev'.
« Last Edit: April 06, 2017, 01:04:12 am by qwertywert »

 

anything