Skip to content
Hapax edited this page Aug 17, 2015 · 1 revision

Introduction

Light class to simplify timestep usage in a game loop, for example.

Usage

Basic usage is:

  1. declare TimestepLite,
  2. call TimestepLite::update() each frame, passing it the amount of time passed since the previous frame,
  3. perform your updates in a while loop with the condition: TimestepLite::isTimeToIntegrate().

In code:

kairos::TimestepLite timestep;
while (inMainLoop)
{
    processEvents();

    timestep.update(frameTime); // frameTime is in seconds and must be of type double
    while (timestep.isTimeToIntegrate())
    {
        performUpdates(timestep.getStep()); // updates should be multiplied by this step (commonly known as "delta time" or "dt")
    }

    render();
}

Simple Example

Complete example that allows you to move around a circle using the arrow keys.
It updates using a fixed timestep (using the default of 1/100th of a second). The actual usage of Timestep Lite is the three points listed above and are clearly marked in this example in comments.

#include <SFML/Graphics.hpp>
#include <Kairos/TimestepLite.hpp>
int main()
{
    sf::RenderWindow window(sf::VideoMode(600, 400), "Kairos - Timestep Lite");
    kairos::TimestepLite timestep; // (1)
    const float dt{ static_cast<float>(timestep.getStep()) };
    const float movementSpeed{ 250.f };
    sf::CircleShape circle(50.f);
    circle.setOrigin(circle.getRadius(), circle.getRadius());
    circle.setPosition(sf::Vector2f(window.getSize() / 2u));
    sf::Clock clock;
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        timestep.update(clock.restart().asSeconds()); // (2)
        while (timestep.isTimeToIntegrate()) // (3)
        {
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                circle.move(0.f, -movementSpeed * dt);
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                circle.move(0.f, movementSpeed * dt);
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                circle.move(-movementSpeed * dt, 0.f);
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                circle.move(movementSpeed * dt, 0.f);
            if (circle.getPosition().x < circle.getRadius())
                circle.setPosition(circle.getRadius(), circle.getPosition().y);
            else if (circle.getPosition().x > window.getSize().x - circle.getRadius())
                circle.setPosition(window.getSize().x - circle.getRadius(), circle.getPosition().y);
            if (circle.getPosition().y < circle.getRadius())
                circle.setPosition(circle.getPosition().x, circle.getRadius());
            else if (circle.getPosition().y > window.getSize().y - circle.getRadius())
                circle.setPosition(circle.getPosition().x, window.getSize().y - circle.getRadius());
        }
        window.clear();
        window.draw(circle);
        window.display();
    }
    return EXIT_SUCCESS;
}

This example requires the SFML library.
The clock it uses is also from the SFML library, although Kairos provides other clocks that would suffice (kairos::Stopwatch, for example). They return a double which is the type that TimestepLite expects to be passed in through update() whereas [sf::Clock] returns a float.

For a similar example, see Timestep Lite example

More Power, More Control, More Simplicity

Kairos also provides Timestep, which is a larger class than TimestepLite with more features (it can be paused and reversed, its speed can be changed, it provides the interpolation alpha) while being more simple to use (it uses an internal clock).
For an example of Timestep that is similar to both Timestep Lite examples, see Timestep example

Clone this wiki locally