SFML community forums

Help => Window => Topic started by: _darkus on August 05, 2013, 08:41:40 pm

Title: Help with implementing framelimit/correct timestep
Post by: _darkus on August 05, 2013, 08:41:40 pm
Hello everybody.
I need some help with timestep. I tried a lot of methods to implement correct time step, but i none of them helped.
If i use setFrameLimit(60) my game will stutter hard.
If i use VSync - then game use 90% of CPU, but there is no stuttering.
Also, i tried to use this code: (it is from SFML's wiki)
 
        sf::Clock frameClock;
        sf::Clock updateClock;
        sf::Int32 nextUpdate = updateClock.getElapsedTime().asMilliseconds();
        float updateRate(1.0f / 20.f);
        float maxUpdates = 1;
        window.setFramerateLimit(60);

        while (window.isOpen())
        {
                sf::Int32 updateTime = updateClock.getElapsedTime().asMilliseconds();
                Uint32 updates = 0;

                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();

                        SceneManager::instance().input(event);
                }      

                while((updateTime - nextUpdate) >= updateRate && updates++ < maxUpdates)
                {
                        SceneManager::instance().updateFixed(sf::seconds(updateRate)); 
                        nextUpdate += updateRate;
                }

                SceneManager::instance().update(frameClock.restart());

                window.clear();
                SceneManager::instance().draw(window);
                window.display();
        }
 
Looks better than just setFrameLimit() but there is still some stuttering, even when i use * dt.asSeconds() when moving.
Need some help with it, i will be glad to see your implementation of timestep.
Thanks/
Title: Re: Help with implementing framelimit/correct timestep
Post by: zsbzsb on August 06, 2013, 12:18:23 am
Here is how I do it in C#, but the same concept still applies to C++.

            long elapsedtime = 0;
            int fixedtimestep = 15;
            while (_window.IsOpen())
            {
                elapsedtime += elapsedtimer.ElapsedMilliseconds;
                elapsedtimer.Reset();
                elapsedtimer.Start();
                _window.DispatchEvents();
                _window.Clear(Color);
                while (elapsedtime >= fixedtimestep)
                {
                    elapsedtime -= fixedtimestep;
                    // Update now
                }
                // Draw here
                _window.Display();
            }
Title: Re: Help with implementing framelimit/correct timestep
Post by: _darkus on August 06, 2013, 01:08:30 pm
Sorry, but your code stutters as hell too.
Title: Re: Help with implementing framelimit/correct timestep
Post by: Laurent on August 06, 2013, 01:20:05 pm
Have you read the article "Fix your timestep"?
Title: Re: Help with implementing framelimit/correct timestep
Post by: _darkus on August 06, 2013, 04:37:10 pm
I tried all methods from there.
But none of them helped  :(
Okay, i will try them again, i could miss something.
Also i asked about VSync hish CPU usage. Anybody had this problem?
Thanks.
Title: Re: Help with implementing framelimit/correct timestep
Post by: Jove on August 09, 2013, 12:49:06 am
Also i asked about VSync hish CPU usage. Anybody had this problem?

Going back when I had an Nvidia card running SFML2 (RC) I remember having to change something in the driver control panel for a similar problem. The item in the control panel was Threaded Optimization (I switched it 'off' from 'auto'). Try that if you have an Nvidia card.

I also remember different behaviours between Windows Aero and Windows Basic themes.

I switched to use the "SFML Game Development" book's timestep code, an easy change, just a few lines. That works really well except for one minor issue which may be an ATI driver thing. Running the app after first boot or log-off/log-on causes a bad stutter that slowly and gradually corrects itself and everything is fine after 10secs (or if I restart the app). I only mention it here in case someone else has seen it too.

*Small update....I've had to reinstall my Nvidia card due to an intermittent BSOD issue on the Radeon HD6870. The above info is correct although it seems you need to run in a window or Window Style::None to see the correct CPU usage.