SFML community forums

Help => General => Topic started by: Ltp0wer on October 14, 2011, 01:29:54 am

Title: Polling for events drastically lowers frame rate
Post by: Ltp0wer on October 14, 2011, 01:29:54 am
I am having a strange problem.

Anytime I am polling for events, my frame rate drops incredibly. Even more so if I am moving the mouse while polling for events.

For example, with this code:
Code: [Select]

#include <SFML/Graphics.hpp>


int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "SFML window");


    while (window.IsOpened())
    {
        window.Clear();


        /*
        sf::Event event;
        while (window.PollEvent(event))
        {
            if (event.Type == sf::Event::Closed)
                window.Close();

            if (event.Type == sf::Event::KeyPressed)
            {
                if (event.Key.Code == sf::Keyboard::Key::Escape)
                    window.Close();
            }
        }
        */

        window.Display();
    }

    return EXIT_SUCCESS;


I get well over 1000 FPS with this code. When I uncomment the block polling for events, I get around 50 fps, and if I move my mouse around the window (slowly or quickly), it drops to a range of 1-20.

My specs are:
Q6600 Intel Core2Quad 2.66ghz
Nvidia GeForce GTX 260
3.5GB of RAM

I am using SFML 2.0

I don't think this is an SFML problem, as old programs I made in SFML 1.6 on a different computer, run slow as well on this computer. Also, my friend who also uses SFML 2.0, sent me a program he made that just displays the FPS in the graphics window. As I expected, I get the same low fps problem.

Are there any known issues that would cause this? I've already tried updating my graphics card drivers, and it hasn't helped.

Thanks.

Edit:

Operating system is Windows 7 32bit

Also, I have tried compiling my programs in both debug and release mode, as well as both dynamic linking and static linking, and I have the same issue.
Title: Polling for events drastically lowers frame rate
Post by: OniLinkPlus on October 14, 2011, 03:43:38 am
That... is weird. It doesn't even sound like a driver issue. It sounds more like your computer is on the verge of imploding. Do you have anti-virus installed? What operating system?
Title: Polling for events drastically lowers frame rate
Post by: Ltp0wer on October 14, 2011, 04:06:49 am
Quote from: "OniLink10"
That... is weird. It doesn't even sound like a driver issue. It sounds more like your computer is on the verge of imploding. Do you have anti-virus installed? What operating system?


No anti-virus, but I run Spybot S&D and Ad-aware SE Personal every once in awhile. I'll try doing that now.

Operating system is Windows 7 32bit (Can't believe I forgot to include that in the OP)

Also, I should mention that this doesn't happen with any other games I have played, but then again, most of those are Direct3D AAA titles.
Title: Polling for events drastically lowers frame rate
Post by: Laurent on October 14, 2011, 07:46:47 am
Can you try to comment the call to ProcessJoystickEvents in src/SFML/Window/Window.cpp?
Title: Polling for events drastically lowers frame rate
Post by: Ltp0wer on October 14, 2011, 08:19:42 am
Quote from: "Laurent"
Can you try to comment the call to ProcessJoystickEvents in src/SFML/Window/Window.cpp?


I commented out "ProcessJoystickEvents();" on line 121 and line 133 of src/SFML/Window/WindowImpl.cpp and recompiled.

It is working beautifully. :D

You are a god damn genius.

What does this mean though?
Title: Polling for events drastically lowers frame rate
Post by: Laurent on October 14, 2011, 09:32:56 am
Each time this function is called, it polls joysticks to see if they are connected/disconnected. Apparently this is a very slow operation.

Unfortunately, on Windows there's no asynchronous/callback system that would notify when a joystick is connected/disconnected, which would avoid this polling and save a lot of CPU. I still need to find a solution to this -- maybe just increase the polling interval.
Title: Polling for events drastically lowers frame rate
Post by: slotdev on October 14, 2011, 09:45:56 am
Quote from: "Laurent"
Can you try to comment the call to ProcessJoystickEvents in src/SFML/Window/Window.cpp?


Laurent is this an issue that anyone who doesnt use a joystick should also comment out from the code?
Title: Polling for events drastically lowers frame rate
Post by: Laurent on October 14, 2011, 09:51:06 am
Quote
Laurent is this an issue that anyone who doesnt use a joystick should also comment out from the code?

Until it's fixed, yes, it is a potential problem.
Title: Polling for events drastically lowers frame rate
Post by: Nexus on October 14, 2011, 12:05:30 pm
Is this function only needed to recognize the plugging-in of a joystick while SFML is running or does it poll the actual joystick events? In the first case, SFML could do it at startup and provide a function in sf::Joystick to call manually later. It's not beautiful however, and I would only do it if the automatic recognition is a severe performance issue.

By the way, are the IsConnected() and Update() functions of sf::Joystick somehow related to this?
Title: Polling for events drastically lowers frame rate
Post by: Laurent on October 14, 2011, 12:13:33 pm
In fact the guilty one is JoystickImpl::IsConnected, which is called by JoystickManager::Update, which is called by WindowImpl::ProcessJoystickEvents.

A simple workaround would be to call JoystickManager::Update only once per second (for example) instead of everytime PollEvent is called.
Title: Polling for events drastically lowers frame rate
Post by: mbro2k on October 17, 2011, 12:04:35 am
Hey I tried Laurent's suggestion and only called IsConnected every few seconds and it solved my problem!
(my problem was a choppy framerate when running with a newly connected xbox controller for the first time).

Actually I had to keep JoystickManager::Update running normally, because my joystick would not work properly.
I actually put the timer before the IsConnected function itself within
JoystickManager::Update
Title: Polling for events drastically lowers frame rate
Post by: Laurent on October 17, 2011, 08:00:03 am
Yes, that was the right thing to do, my previous message was incorrect :)
Title: Polling for events drastically lowers frame rate
Post by: Tex Killer on November 05, 2011, 03:27:20 am
Wouldn't it be possible to run that method on a separate thread, and every frame just check if the thread had finished, re-launching it if it had?
Title: Polling for events drastically lowers frame rate
Post by: Felheart on November 05, 2011, 07:01:05 am
I think a seperate thread would be acceptable untill there is a better solution.
In my game I need those functions (the easy access to gamepads / joysticks is one of the reasons I switched to SFML).
If you increase the polling intervall, wouldn't the input-delay rise with it?
That would be horrible :(
Title: Polling for events drastically lowers frame rate
Post by: Tex Killer on November 05, 2011, 03:14:10 pm
If it have a delay high enough to damage FPS, then it will be always bad to put it in the same thread as the game loop. If it is in a separate thread, it will not damage FPS and will be called right when it is ready to be called, without waiting for seconds or so.
Title: Re: Polling for events drastically lowers frame rate
Post by: Dungeon Throne Dev on February 01, 2017, 06:49:28 pm
I still have this issue.
At least that´s what I think.
The fps are stable only when i comment the window.pollevent(event); function.
I cant find the src/SFML/Window/Window.cpp file.
Where should it be?
I´m kinda new  ::)
Title: Polling for events drastically lowers frame rate
Post by: eXpl0it3r on February 01, 2017, 08:06:16 pm
You will have to download the source code on the download page or GitHub.
Title: Re: Polling for events drastically lowers frame rate
Post by: Dungeon Throne Dev on February 01, 2017, 08:47:18 pm
So, I downloaded  and commented it.
I´m not sure how to compile this ._.
Title: Polling for events drastically lowers frame rate
Post by: eXpl0it3r on February 01, 2017, 08:51:47 pm
That's why there are official tutorials. ;)
Title: Re: Polling for events drastically lowers frame rate
Post by: Dungeon Throne Dev on February 01, 2017, 11:56:18 pm
Thanks you for replying me.
I tried this guide but I´m still lost. :'(
http://www.sfml-dev.org/tutorials/2.4/compile-with-cmake.php
Title: Re: Polling for events drastically lowers frame rate
Post by: jamesL on February 04, 2017, 10:59:53 am
http://en.sfml-dev.org/forums/index.php?topic=21427.msg152505#msg152505
Title: Re: Polling for events drastically lowers frame rate
Post by: Dungeon Throne Dev on February 04, 2017, 03:46:51 pm
My bad for not explaining.
I´m using Visual Studio to compile and run code, I´m not using MinGW.
As far as I know I have to just select the source code of SFML in CMake, configure it if I want and just Generate it.
The thing is, when I do that, the DLL dont generate.
Title: Re: Polling for events drastically lowers frame rate
Post by: jamesL on February 07, 2017, 09:57:53 am
you have to tell cmake to generate a ms solution or project or something
 
the link I gave is telling cmake to generate a ningw solution 

you'll have to look at the cmake tutorial to figure out how to generate ms solution

once cmake generates a ms solution you compile that solution to create your dll

go here
http://www.sfml-dev.org/tutorials/2.4/compile-with-cmake.php

read
"With Visual C++, you can either run CMake from the "Visual Studio command prompt" available from the start menu, or run the vcvars32.bat batch file of your Visual Studio installation in the console you have open. The batch file will set all the necessary environment variables in that console window for you."

and the sections after that