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

Author Topic: Help with implementing framelimit/correct timestep  (Read 3130 times)

0 Members and 1 Guest are viewing this topic.

_darkus

  • Newbie
  • *
  • Posts: 7
    • View Profile
Help with implementing framelimit/correct timestep
« 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/
« Last Edit: August 05, 2013, 10:15:03 pm by _darkus »

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Help with implementing framelimit/correct timestep
« Reply #1 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();
            }
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

_darkus

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Help with implementing framelimit/correct timestep
« Reply #2 on: August 06, 2013, 01:08:30 pm »
Sorry, but your code stutters as hell too.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Help with implementing framelimit/correct timestep
« Reply #3 on: August 06, 2013, 01:20:05 pm »
Have you read the article "Fix your timestep"?
Laurent Gomila - SFML developer

_darkus

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Help with implementing framelimit/correct timestep
« Reply #4 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.

Jove

  • Full Member
  • ***
  • Posts: 114
    • View Profile
    • http://www.jestofevekites.com/
Re: Help with implementing framelimit/correct timestep
« Reply #5 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.
« Last Edit: August 29, 2013, 12:28:34 pm by Jove »
{much better code}