Timer

Overview

class omdi::Timer

Timer class for measuring elapsed time and frame delta time.

The omdi::Timer class provides a simple high-resolution timer suitable for render loops and simulations. It measures:

  • the total elapsed time since construction, and

  • the per-frame delta time between consecutive omdi::Timer::tick() calls.

Internally, it uses std::chrono::high_resolution_clock.

Data members

std::chrono::time_point<std::chrono::high_resolution_clock> omdi::Timer::m_now

Timestamp of the most recent frame (last call to omdi::Timer::tick()).

const std::chrono::time_point<std::chrono::high_resolution_clock> omdi::Timer::m_start

Timestamp when the timer was constructed.

This value is constant for the lifetime of the timer and is used as the reference for computing total elapsed time.

std::size_t omdi::Timer::m_frame

Current frame counter.

Incremented by omdi::Timer::tick() each time it is called.

double omdi::Timer::m_delta

Time in seconds between the last two frames.

Updated on each call to omdi::Timer::tick(). Initialized to 1.0.

double omdi::Timer::m_elapsed

Total elapsed time in seconds since the timer was constructed.

Updated on each call to omdi::Timer::tick().

Constructor

omdi::Timer::Timer()

Construct a timer and initialize timestamps.

Sets omdi::Timer::m_now and omdi::Timer::m_start to the current std::chrono::high_resolution_clock time, with frame counter, delta, and elapsed values initialized accordingly.

Public interface

void omdi::Timer::tick()

Advance the timer by one frame.

This function:

Typically called once per frame in the main loop.

std::size_t omdi::Timer::frame() const

Get the current frame count.

Returns:

Number of times omdi::Timer::tick() has been called since construction.

double omdi::Timer::delta() const

Get the time elapsed between the last two frames.

Returns:

Delta time in seconds (as updated by the most recent omdi::Timer::tick() call).

double omdi::Timer::elapsed() const

Get the total elapsed time since the timer was constructed.

Returns:

Elapsed time in seconds since omdi::Timer::m_start.