SFML community forums

Help => General => Topic started by: ronenp88 on April 30, 2020, 08:51:11 am

Title: How to Clock framing the game
Post by: ronenp88 on April 30, 2020, 08:51:11 am
Hopefully this is the right place to ask,
I'm wondering if this is the proper way of doing this clock framing:
void game::run() {

        sf::Clock clock;
        sf::Time frameTime = sf::milliseconds(10);
        while (window.isOpen()) {
                sf::Time elapsed = clock.getElapsedTime();

                if (elapsed > frameTime) {
                        logic();
                        draw();
                        clock.restart();
                }
        }
}
 

Or this is done differently ?
Title: Re: How to Clock framing the game
Post by: ronenp88 on April 30, 2020, 01:55:30 pm
Btw, I have trouble with animation and logic times, the animation plays too fast and when I set the clock time to more slowly, it breaks the logic times, and if I use two clocks one for logic one for drawing, it gets ugly and not smooth, what should I do?

void PacmanGame::run() {
        sf::Clock drawClock;
        sf::Time drawframeTime = sf::milliseconds(150);

        sf::Clock logicClock;
        sf::Time logicframeTime = sf::milliseconds(50);

        while (window.isOpen()) {
                sf::Time elapsedDraw = drawClock.getElapsedTime();
                sf::Time elapsedLogic = logicClock.getElapsedTime();

                if (elapsedLogic > logicframeTime) {
                        logic();
                        logicClock.restart();
                }

                if (elapsedDraw > drawframeTime) {                     
                        draw();
                        drawClock.restart();
                }
        }
}
 
Title: Re: How to Clock framing the game
Post by: Hapax on May 01, 2020, 06:47:38 pm
is the proper way of doing this clock framing
Not the best way but it should give average (but unreliable) results.
You're basically making it wait until some time has passed before doing anything at all.

The first thing you could try is to do things all the time but scale it by how much time has passed.



For more accurate (and good descriptions of) timing setups, you should almost definitely read this:
https://gafferongames.com/post/fix_your_timestep/

Afterwards, you can implement it yourself or use Kairos (https://github.com/Hapaxia/Kairos/wiki)' classes to help:
Timestep (https://github.com/Hapaxia/Kairos/wiki/Timestep) or Timestep Lite (https://github.com/Hapaxia/Kairos/wiki/Timestep-Lite).
I'd recommend Timestep Lite to start with. Timestep is more powerful and automatic (and the one I always use myself) but requires the entire Kairos library whereas the lite version can be used on its own.

Have a look at Timestep Lite's simple example (https://github.com/Hapaxia/Kairos/wiki/Timestep-Lite#simple-example) to see how easy it really is!