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

Author Topic: Frame Rate/Periodic Lag Issues  (Read 6940 times)

0 Members and 1 Guest are viewing this topic.

CombustibleL3m0n

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Frame Rate/Periodic Lag Issues
« on: August 25, 2013, 07:51:57 pm »
Hi everybody,

I'm new to SFML and new to the forum so sorry if I haven't posted correctly, but I'm having some issues with my C++ game.

My problem is that I'm getting inconsistent lag. I have searched around for solutions to frame rate issues and found people telling me to use Vsync, which I did but it didn't improve the situation. I've tried using an if statement inside my while(window.isOpen()) loop with sf::Clock.getTimeElapsed.asSeconds() > 1.0f/30.0f (my desired frame rate), and I've also tried to use the setFramerateLimit(30) function, but none of this seems to help.

The strange thing is, that I have tested the framerate by outputting the elapsed time to the console every frame, and the elapsed time is close to 0.033 for 9 or so frames, then it randomly increases to about 0.08, then back down again, and this cycle repeats, so the spike is about every half second.

Can anyone explain this weird behaviour? I am running this on a powerful machine, and my game has fairly efficient game logic, so I can't explain why these lag spikes occur. I didn't think including source code would be useful, but I can if anyone wants to take a look.
Any help would be greatly appreciated.

Thanks.  :)


Update:

I'm still at a loss, I've tried it even using basic minimal coding, and i'm still getting lagg spikes
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "Window");
        window.setVerticalSyncEnabled(true);
        sf::Clock clock;
        float currTime = 0.0f;
        float prevTime = 0.0f;

    while (window.isOpen())
    {
                        sf::Event event;
                        while (window.pollEvent(event))
                        {
                                if (event.type == sf::Event::Closed)
                                        window.close();
                        }
                        currTime = clock.getElapsedTime().asSeconds();
                        std::cout << currTime - prevTime << std::endl;
                        prevTime = currTime;
                        window.clear(sf::Color::Black);
                        window.display();
                        sf::sleep(sf::Time(sf::milliseconds(20)));
    }

    return 0;
}
 

I've got a screenshot of what the console outputs here, showing where the spikes occur:

Any help greatly appreciated :)
« Last Edit: August 26, 2013, 09:19:59 pm by CombustibleL3m0n »

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Frame Rate/Periodic Lag Issues
« Reply #1 on: August 25, 2013, 09:08:18 pm »
With only those we informations we can't help you too much. If you can show the loop( while(window.isOpen() ) code maybe we will can help you more.
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

CombustibleL3m0n

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Frame Rate/Periodic Lag Issues
« Reply #2 on: August 25, 2013, 10:53:43 pm »
This is the source:

// Create the Window
 sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
 window.setVerticalSyncEnabled(true);

 sf::Clock clock;
 float lastTime = 0.0f;
 float currTime = 0.0f;
 // Run loop
 while (window.isOpen())
 {
   currTime = clock.getElapsedTime().asSeconds();
   if(currTime - lastTime >= 1.0f/30.0f) // 30FPS
   {
     // Check Events
     sf::Event event;
     while (window.pollEvent(event))
     {
       if (event.type == sf::Event::Closed)
         window.close();
     }
     // Game Logic
     if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
     {
       sf::Vector2i mousePos = sf::Mouse::getPosition(window);
       player2.SetPos(Vec((float)mousePos.x, (float)mousePos.y));
     }
     // Update Object positions and do collision checks
     world.Step(currTime - lastTime);
     std::cout << currTime - lastTime << "s" << std::endl;
     lastTime = currTime;
   }

   // Clear Window
   window.clear(sf::Color(185, 122, 87));
   // Draw everything here...
   window.draw( *( player.GetSprite() ) );
   window.draw( *( player2.GetSprite() ) );
   window.draw( *(wallLeft.GetSprite() ) );
   window.draw( *(wallRight.GetSprite() ) );
   window.draw( *(ground.GetSprite() ) );
   window.draw( *(ceiling.GetSprite() ) );
   // Display at Monitor's refresh rate
   window.display();
 }
 
« Last Edit: August 25, 2013, 10:55:23 pm by CombustibleL3m0n »

CombustibleL3m0n

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Frame Rate/Periodic Lag Issues
« Reply #3 on: August 28, 2013, 03:04:21 pm »
---Bump---

Have sent the release to my friends and they report the same problem, so I don't think it's my machine.
Have tried linking the libraries statically and dynamically, but still same problem

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Frame Rate/Periodic Lag Issues
« Reply #4 on: August 28, 2013, 11:56:43 pm »
Random guess without further testing would suggest your problem here is vertical sync (you're sleeping longer than your typical frame time). Or don't you sleep anymore (don't see it in your latest release).

Also make sure to process your window events as fast as possible, not just limited to 30 fps.

Another timing issue:
By assigning "lastTime = currTime" you're dropping leftover time.

  • For example, let's assume you're aiming for 50 updates per second (so 20 ms between updates).
  • Drawing takes 15 ms.

So now you'll with each iteration:
  • lastTime = 0; currTime = 0; nothing
  • lastTime = 0; currTime = 15; nothing
  • lastTime = 0; currTime = 30; update happening; lastTime = 30
  • lastTime = 30; currTime = 45; nothing
  • lastTime = 30; currTime = 60; update happening; lastTime = 60
So we've got 2 updates in the first 60 ms. However, there should have been 3 updates in these 60 ms, but you dropped leftover time. This can be neglectable, but this can become rather significant (in this example up to almost 20 ms).

So, rather than setting "lastTime = currTime" you should only add the intervall: "currTime += 20" (with my numbers above), but the best solution would be working with time differences only.

Quick example (consider it pseudo code; it's late already):

sf::Duration passedTime = 0;
while(running) {
    // process your events here

    passedTime += myClock.restart();
    while (passedTime > frameTime) {
        // do some update
        passedTime -= frameTime;
    }

    // drawing happens here

    // this is a minimal sleep to ensure some
    // measurable time passes even on very fast machines
    sf::Sleep(sf::microsecond(1));
}
« Last Edit: August 28, 2013, 11:59:05 pm by Mario »

CombustibleL3m0n

  • Newbie
  • *
  • Posts: 4
    • View Profile
    • Email
Re: Frame Rate/Periodic Lag Issues
« Reply #5 on: August 29, 2013, 12:31:58 pm »
Thank you! Your code improved the lag a lot.

I am still getting random spikes of about 30-40ms (double the usual 20ms frame time, but still less than it was before), but I've also compiled some other people's projects made in VS 2012 (which is what I'm using), and then I get the same problem with theirs too D:

Could I be missing some dll's from my computer/VS?
« Last Edit: August 29, 2013, 02:30:22 pm by CombustibleL3m0n »

AlexxanderX

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • AlexanderX
Re: Frame Rate/Periodic Lag Issues
« Reply #6 on: August 29, 2013, 02:31:58 pm »
Yes, you should try to turn off VSync and maybe use this:
window.setFramerateLimit(60)

About VSync:
"VSync stands for Vertical Synchronization. The basic idea is that synchronizes your FPS with your monitor's refresh rate"
Here you can find my blog and tutorials about SFML - http://alexanderx.net/ (died...) - http://web.archive.org/web/20160110002847/http://alexanderx.net/

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Frame Rate/Periodic Lag Issues
« Reply #7 on: August 30, 2013, 12:31:57 am »
If you're running an Nvidia card, try disabling the "Multithreading Optimization" in the drivers. SFML usually doesn't like it (or more specifically its use of OpenGL).

TobyKaos

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • http://aubrun.thibaut.free.fr
Re: Frame Rate/Periodic Lag Issues
« Reply #8 on: October 03, 2013, 05:01:23 pm »
Me, it is when I turn vSync on then I have some lags.

But I use SFML 1.6. I will update to 2.1