#include "KTimer.hpp" ➔ class KTimer

KTimer

A class that manages time.

class KTimer {
    const unsigned ms;
    const unsigned count;
};

Constructor

KTimer(unsigned msPerLoop = 1000/60)

It receives the amount of milliseconds required per loop (msPerLoop).
By default, it receives 16 (1000/60) to ensure 60 FPS.

Member

const unsigned ms;
const unsigned count;

The amount of milliseconds required per loop.
The amount of loop that have been performs.

Method

void start(unsigned msPerLoop);
void start();

Reset the class.

void wait();

Call wait() at the end of a loop to ensure a constant amount of loop per second.
It will wait the remaining time to complete a loop in a precise time gap.

unsigned get_time();

Returns the amount of milliseconds that have elapsed since the initialization of the class.

Static Function

void wait_for(unsigned msToWait);

Waits for a specified amount of milliseconds (msToWait).

std::string date();

Returns the date as YYYY-MM-DD hh:mm:ss.

Example

KTimer timer(1000/25);
bool loop = true;

while (loop) {
    timer.wait();

    std::cout << timer.get_time() << std::endl;

    if (timer.count > 100) {
        loop = false;
    }
}