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

Author Topic: How to Clock framing the game  (Read 1850 times)

0 Members and 1 Guest are viewing this topic.

ronenp88

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to Clock framing the game
« 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 ?

ronenp88

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to Clock framing the game
« Reply #1 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();
                }
        }
}
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to Clock framing the game
« Reply #2 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' classes to help:
Timestep or 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 to see how easy it really is!
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything