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

Author Topic: Stuttering when trying to limit fps  (Read 1546 times)

0 Members and 1 Guest are viewing this topic.

ligazetom

  • Newbie
  • *
  • Posts: 1
    • View Profile
Stuttering when trying to limit fps
« on: November 19, 2018, 10:24:37 am »
Hey everyone,

I spent 3 days on this, trying every little thing I found on the internet.

At first I created game loop without any restrictions /*while(true)*/, however I decided there is no reason to let run my app at 1400 fps, just to draw 5 rectangles. So I tried manual sleep functions with different ways of computing the wait time, window method setFrameLimit, vSync but no use. I ended up with this, as I am now fed up with this whole thing.

 Clock gameClock = new Clock();
            Clock sleepClock = new Clock();
            fpsCounter.Start();
            int sleepTime = 0;
            int frameTime = 0;
            int lag = 0;
            const int MS_UPDATE = 10;

            while (window.IsOpen)
            {
                deltaTime = gameClock.Restart();    //I need this for tank movement, so yeah
                lag += deltaTime.AsMilliseconds();
                window.DispatchEvents();
                CheckForInputs();

                while(lag >= MS_UPDATE)
                {
                    scene.UpdateProjectiles(MS_UPDATE / 1000f);
                    CheckCollisions();
                    lag -= MS_UPDATE;
                }

                window.Clear(Color.White);
                renderer.RenderScene(scene, (lag / MS_UPDATE) / 1000);
                window.Display();
                fps++;
                frameTime = gameClock.ElapsedTime.AsMilliseconds();

                sleepClock.Restart();
                sleepTime += (int)(1000f / 100f - frameTime);

                if (sleepTime > 0)
                {
                  System.Threading.Thread.Sleep(sleepTime);
                }

                sleepTime -= sleepClock.ElapsedTime.AsMilliseconds();
            }
        }
 

I tried my best, but I ended up with this result *attachment* (I made point everytime I move forward),
you clearly can see when stuttering occured.

Anyone any ideas? It is still the same with any form of fps limiting.

I programming it in C# with 2.2 .NET wrapper

Thankful for any new ideas!

NGM88

  • Full Member
  • ***
  • Posts: 162
    • View Profile
Re: Stuttering when trying to limit fps
« Reply #1 on: November 20, 2018, 06:37:11 pm »
I get stuttering in my game unless I do

WINDOW.setFramerateLimit(60);

AND

WINDOW.setVerticalSyncEnabled(true);

...even though the devs say you shouldn't do both. Give that a try.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Stuttering when trying to limit fps
« Reply #2 on: November 22, 2018, 02:44:52 am »
I'm not fully clear as to what your image is showing but I can see an occasion 'slight' gap/break in those lines made of circles (?).

Remember that programs do not ever run with consistent timing.
Operating systems are in charge and may allow more or less time for your program depending on what itself has to do. If the operating system is particularly busy, it may delay your program for a number of seconds. This is, of course, rare but an operating system delaying your program for a frame or two is extremely common and should be accounted for.

You may find this article interesting as it explains a decent way of dealing with such things in your logic:
https://gafferongames.com/post/fix_your_timestep/



I do
WINDOW.setFramerateLimit(60);
AND
WINDOW.setVerticalSyncEnabled(true);
...even though the devs say you shouldn't do both.
If vertical sync is enabled, it's already limiting the framerate so why would you need another limit that can add extra waiting and can also cause slower frame rates?

The only time you'd want to set the framerate limit as well as (after) attempting to enable the vertical sync is if the vertical sync fails - if you are able to find out whether it succeeded or failed.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*