1
General / 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.
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!
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();
}
}
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!