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

Author Topic: Event loop causes stuttering  (Read 7536 times)

0 Members and 1 Guest are viewing this topic.

Ushi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Event loop causes stuttering
« on: January 21, 2017, 02:25:04 am »
My SFML programs stutter about 80 ms every half second. I've created a simple program of a sprite moving to demonstrate. Removing the event loop stops the stuttering.

The length of time of each game loop iteration, dt, is printed. dt is usually 0.3 ms, except for every half second or so, when it is 80 ms. And the sprite's stuttering is clearly visible. If I remove the event loop, dt goes up to only 2 ms every half second or so, and the sprite moves smoothly.

#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1920, 1080), "GAME TEST 1");

        sf::Texture texture;
        texture.loadFromFile("media/cat.png");
        sf::Sprite sprite;
        sprite.setTexture(texture);

        //physics
        sf::Vector2f position(50, 100);
        sf::Vector2f velocity(20, 0);
        sf::Clock clock;

        while (window.isOpen())
        {
                float dt = clock.restart().asSeconds();

                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        default:
                                break;
                        }
                }

                // update physics
                position += velocity*dt;
                sprite.setPosition(position);

                //render
                window.clear(sf::Color::Red);
                window.draw(sprite);
                window.display();

                //report
                std::cout << dt << std::endl;
        }
        return 0;
}
 

Even if the event loop is empty the stuttering occurs. Replacing the loop with sf::Joystick::update() also causes stuttering. VSync makes dt more like 16 ms, but it still stutters at 80 ms at the same rate.

I am using:
Visual Studio 2015
Windows 10
SFML 2.4.1 C++14
All static SFML libraries

I've also tried SFML 2.4.0 and 2.3.2 with no success. Any help would be greatly appreciated!
« Last Edit: January 21, 2017, 09:33:10 am by Laurent »

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Event Loop Causes Stuttering
« Reply #1 on: January 21, 2017, 07:43:20 am »
First: don't remove event polling at any case, you may cause event queue overflow

Secondly: did you try to remove std::cout call? standard output operations may slow down your code a little bit. So, consider testing the code by using VS2015 debugger.

Ushi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Event Loop Causes Stuttering
« Reply #2 on: January 21, 2017, 04:29:32 pm »
First: don't remove event polling at any case, you may cause event queue overflow

I do not want to remove event polling, but when I tried commenting out each line to find the source of the problem, that stopped the stuttering, indicating that some part of the PollEvent() function is the source. I would definitely like to fix the problem without removing event polling.

Quote
Secondly: did you try to remove std::cout call? standard output operations may slow down your code a little bit. So, consider testing the code by using VS2015 debugger.

The sprite stutters exactly the same without the std::cout call. I have this same problem in Windows application projects that don't use the console at all. I've tried commenting out each line one at a time and the only thing that stops the stuttering is removing the event loop.

I am currently reading up on how to use the VS2015 debugger tools (I'm pretty new to coding). Is there a specific tool I should be using here?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Event loop causes stuttering
« Reply #3 on: January 21, 2017, 04:43:49 pm »
Do you have a joystick connected to your PC? Do you want to use a joystick?

I suspect this might be the joystick polling, we've seen similar reports in the past.

Try to run it through a profiler to see where the time is spent.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Wolfhound

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Event loop causes stuttering
« Reply #4 on: January 22, 2017, 02:53:16 am »
Do you have a joystick connected to your PC? Do you want to use a joystick?

I suspect this might be the joystick polling, we've seen similar reports in the past.

Try to run it through a profiler to see where the time is spent.

I had just joined to ask a question identical to the OP's before finding this thread. This turned out to be the source of my problems. Thank you!

If it helps the OP, I had both a physical joystick and a virtual joystick connected to my machine when I was experiencing the issue. After testing, it seems to be that the virtual joystick (specifically vJoy) was solely responsible.

Does SFML provide any means to ignore input from certain devices to work around this issue?

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Event loop causes stuttering
« Reply #5 on: January 22, 2017, 03:19:32 pm »
No, SFML doesn't provide anything like that  :P

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Event loop causes stuttering
« Reply #6 on: January 22, 2017, 08:01:49 pm »
No, we don't have such an option. What you can do if you don't want to use a joystick anyways, is remove the joystick processing line from SFML's source code and recompile it.

Alternatively you could try and figure out the cause for this, i.e. which some controller drives introduce such a lag.

In the near future we may have to think about a different approach to handle joysticks.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Ushi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Stuttering Stopped!
« Reply #7 on: January 22, 2017, 09:51:10 pm »
Reinstalling VS2015 solved the problem!

Yes, I think the stuttering only began when I started using Joystick events. However, it remained even when I disconnected the joystick and created a new program without Joystick events. I noticed that removing window.pollEvent() stopped the stuttering and I tried to replace it with sf::Joystick::update() only to find that caused stuttering too.

The profiler showed a constant fps and minimal GPU/CPU usage. I decided to repair VS; didn't work. I decided to reinstall without the Mobile Platform and Web SDKs. Problem solved!--even with joystick input! My sprite's jumping and running around like Mario! No stutters!

I'll try adding those extra SDKs and report back if the stutters return. I feel like this is probably buggy old Windows 10's fault. I didn't even have to erase my VS projects directory. Sorry for not trying this obvious solution sooner and thank you all for your support! 

Ushi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Stuttering Returns!
« Reply #8 on: January 23, 2017, 04:41:02 pm »
The stuttering returned! I realized I hadn't actually used Joystick events since reinstalling VS2015; I had only been using  sf::Joystick::isButtonPressed(x, x) outside the event loop. I believe the stuttering began this time when i first compiled after putting a Joystick event in the event loop.

However, retracing my steps and reinstalling VS2015 did nothing! I'm back to square one! I'm using SFML mainly for this controller support so I can't remove it from the library.

I'm happy to just use conditionals for the Joystick outside the event loop, which worked fine yesterday. But now the problem remains even after removing the Joystick event and repairing and reinstalling VS2015 and everything it came with.

EDIT: So I've been using a PS4 controller connected via bluetooth to my PC, which surely complicates things, but it was working fine yesterday. I unpaired it and then disconnected and reconnected my Logitech USB controller (which I had tried when the problem first arose to no success), but this time it worked! No stuttering! I will try reinstalling drivers to see if I can get my PS4 controller working again if I don't use Joystick events.

EDIT 2: Stuttering returned without noticeable cause. Then, following Wolfhound's advice, I disabled vJoy (which my Logitech controller wasn't using) and the stuttering stopped again. I agree that a future means of ignoring specific devices could be useful.
« Last Edit: January 23, 2017, 07:28:33 pm by Ushi »

Wolfhound

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Event loop causes stuttering
« Reply #9 on: January 23, 2017, 08:04:55 pm »
I ended up with the same issue of the stuttering returning after rebooting, even with vJoy entirely uninstalled and my physical joystick unplugged. At that point I tried getting the peripherals down to only my mouse, keyboard, and headset. That still didn't work. At that point I ended up launching the Logitech configuration software I use for my mouse and headset, and that seemed to have reduced the lag (but it still exists).

To give details regarding this, the lag from the polling happens in approximately 500ms intervals. I would be curious if the interval is similar for you, Ushi.

At this point I'll try building 2.4.1 without the joystick polling and report back the results.

Edit: Ran a bare-bones example through the profiler after compiling SFML 2.4.1 to get the debug symbols. This definitely stems from the joystick polling, even when all of my joystick peripherals are disconnected. The call to getJoyPosEx in the Win32 JoystickImpl::isConnected seems to be the culprit, which seems to be a (partially?) known problem given the comment in the function and the implemented workaround (which is to implement a 500ms delay between checking the joystick connection, which explains why I get the stuttering every 500ms).

Here's a link to the specific line on GitHub for reference.

Will update again if I figure out a possible solution. Obviously (as mentioned earlier in the thread) compiling a version with joystick support disabled would work, though it sounds like that's not going to work for you, Ushi.

Edit 2: Looked around a bit longer out of curiosity (I just disabled joystick event polling, which worked as eXpl0it3r said it would). From what I garnered from some Google searches, WinMM, the library that SFML uses for handling joystick input, is deprecated in favor of DirectInput. It might be possible that there are some problems running on Windows 10 systems? I don't really pretend to know and I'm hardly an expert, but I thought it might help out if you want to narrow it down.
« Last Edit: January 24, 2017, 03:55:19 am by Wolfhound »

Ushi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Event loop causes stuttering
« Reply #10 on: January 24, 2017, 04:31:37 am »
My original stutters did appear to be every 500 ms.

My program is now starting up fine, but then gradually starts stuttering more and more. About a minute after start it's just stuttering all over the place, but the biggest stutters happen about every 500 ms.

It seems like you've uncovered most of the issue, Wolfhound. I guess it's my turn to build SFML without joystick polling and switch over to DirectInput.

Edit: All this trouble is starting to make me consider giving in to evil Microsoft and just learning DirectX. But surely that can't be a good idea for someone who taught himself how to program starting a few months ago.
« Last Edit: January 24, 2017, 05:04:49 am by Ushi »

tharos

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Event loop causes stuttering
« Reply #11 on: February 18, 2017, 06:55:35 pm »
I've the same problem - the ugly stuttering caused by joystick handling in event loop (although i unplugged all joysticks).

See CPU Profiler Screenshot:


Is there anything that can be done against it beside compile SFML without joystick support?

Ushi

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Event loop causes stuttering
« Reply #12 on: March 05, 2017, 11:03:23 pm »
Is there anything that can be done against it beside compile SFML without joystick support?

From my experience it seems that if you want to use SFML on Windows 10, you're gonna have to build SFML without joystick event polling.

Possible solutions:
- I built SFML with no joy and implemented XInput. It works great and wasn't too hard since DirectX is installed with Virtual Studio now, but I'll have to add DInput eventually to make my game work with old controllers
- I think Windows 7 would work assuming they haven't deprecated WinMM there as well
- And then you could of course try other OSs entirely like Mac OS

Here's my no-joystick build of SFML; it's only static, but has both debug and release libraries:
https://www.dropbox.com/s/rfhbd6zj78lh54e/SFML%202.4.1%20STATIC%20NOJOY.zip?dl=0

Edit: Also I tried this old XInput wrapper for SFML and it worked fine: http://en.sfml-dev.org/forums/index.php?topic=12155.0
« Last Edit: March 05, 2017, 11:07:54 pm by Ushi »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Event loop causes stuttering
« Reply #13 on: March 06, 2017, 12:01:59 am »
We've just merged PR #1195, which should fix this stuttering.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

tharos

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Event loop causes stuttering
« Reply #14 on: March 19, 2017, 09:57:36 am »
I tried the latest master-builds from http://www.sfml-dev.org/artifacts/by-branch/master/ and the problem seems to be gone  :)