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

Author Topic: How could I implement a fixed-timestep game loop?  (Read 11524 times)

0 Members and 1 Guest are viewing this topic.

sofakng

  • Newbie
  • *
  • Posts: 18
    • View Profile
How could I implement a fixed-timestep game loop?
« on: January 16, 2009, 03:22:25 pm »
Has anybody implemented a fixed-timestep game loop with SFML?

Also, I have a couple of questions about GetFrameTime():

1) What type of performance counter does GetFrameTime use?  How accurate is it?

2) What time frame does it measure?  (eg. does it measure the time from the end of Display() to the start of the next call to Display()?)

Thanks for any help!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: How could I implement a fixed-timestep game loop?
« Reply #1 on: January 16, 2009, 03:57:18 pm »
Quote from: "sofakng"
Has anybody implemented a fixed-timestep game loop with SFML?
To achieve this, you can either use sf::Window::SetFramerateLimit() or measure the time in the main loop and wait until it has reached a certain limit (in mean time you could call sf::Sleep() to relieve the CPU).
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

sofakng

  • Newbie
  • *
  • Posts: 18
    • View Profile
How could I implement a fixed-timestep game loop?
« Reply #2 on: January 16, 2009, 05:11:22 pm »
Thanks for the reply.

I think I wasn't that clear in my original question.

My actual goal is to seperate my update logic from my display/render logic.

For example, I think XNA does it perfectly: http://blogs.msdn.com/shawnhar/archive/2007/11/23/game-timing-in-xna-game-studio-2-0.aspx

However, in order for me to implement that I need to know how GetFrameTime() is working.

By seperating the update and render logic you can render as fast as possible (200+ FPS) while the update logic runs slower if needed.  That way the user gets a very good framerate without sacrificing update speed.

Lagiv

  • Newbie
  • *
  • Posts: 3
    • View Profile
How could I implement a fixed-timestep game loop?
« Reply #3 on: January 16, 2009, 11:05:46 pm »
Thanks for that XNA link, they've really nailed how to do fixed-timestep gameloop with independent drawing rate.

Here is also a good article for tips:
http://gafferongames.wordpress.com/game-physics/fix-your-timestep/

I'm using the latter article as a guide for a fixed-timestep loop. It has changeable physics rate and it limits maximum number of drawn frames to the physics rate. I haven't tested it thoroughly, but it seems to work ok. It currently looks like this:

Code: [Select]
float dt = 1.f/40.f; // Modify this to change physics rate.
float accumulator = 0.f;
bool drawn = false;

while (App.IsOpened())
{
    accumulator += clock.GetElapsedTime();
    clock.Reset();

    while (accumulator >= dt)
    {
        // Physics and gameplay updates.

        accumulator -= dt;
        drawn = false;
    }

    if (drawn)
    {
        sf::Sleep(0.01);
    }
    else
    {
        // Draw everything.

        drawn = true;
    }
}