Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
Time

High-level and low-level time management utilities, including thread sleeping, time counters, and a simple stopwatch abstraction. More...

Detailed Description

Data Structures

struct  bj_stopwatch
 Structure representing a simple stopwatch. More...
 

Typedefs

typedef struct bj_stopwatch bj_stopwatch
 Structure representing a simple stopwatch.
 

Functions

void bj_sleep (int milliseconds)
 Suspends the current thread for a specified duration.
 
double bj_get_time (void)
 Gets the current time in seconds since Banjo initialization.
 
uint64_t bj_get_time_counter (void)
 Returns the current high-resolution time counter.
 
uint64_t bj_get_time_frequency (void)
 Returns the frequency of the high-resolution counter.
 
void bj_stopwatch_reset (bj_stopwatch *p_stopwatch)
 Resets the stopwatch to the current time.
 
void bj_stopwatch_step (bj_stopwatch *p_stopwatch)
 Records a step/checkpoint in time.
 
double bj_stopwatch_elapsed (const bj_stopwatch *p_stopwatch)
 Returns the elapsed time in seconds since the stopwatch was reset.
 
double bj_stopwatch_delay (const bj_stopwatch *p_stopwatch)
 Returns the time in seconds since the last step.
 
double bj_stopwatch_step_delay (bj_stopwatch *p_stopwatch)
 Steps the stopwatch and returns the delay since the previous step.
 

Data Structure Documentation

◆ bj_stopwatch

struct bj_stopwatch

A stopwatch records the time it was last reset and the time of the last step. It computes elapsed time since reset, and delay since the last step. A zero-initialized stopwatch is valid and will auto-reset on first use.

Examples
stopwatch.c.
Data Fields
uint64_t last_tick Time of last step/checkpoint.
uint64_t start_counter Time when stopwatch was last reset.

Typedef Documentation

◆ bj_stopwatch

typedef struct bj_stopwatch bj_stopwatch

A stopwatch records the time it was last reset and the time of the last step. It computes elapsed time since reset, and delay since the last step. A zero-initialized stopwatch is valid and will auto-reset on first use.

Function Documentation

◆ bj_get_time()

double bj_get_time ( void )

This function returns the time in seconds since bj_begin was called. It is suitable for general-purpose timing, but not for high-resolution use.

Returns
The current time in seconds.
See also
bj_get_time_counter, bj_get_time_frequency
Examples
audio_pcm.c, shaders.c, and time.c.

◆ bj_get_time_counter()

uint64_t bj_get_time_counter ( void )

This value is in platform-dependent ticks and is suitable for precise timing and performance measurements. To convert ticks to seconds, divide by the result of bj_get_time_frequency.

Returns
The current time counter in ticks.
See also
bj_get_time, bj_get_time_frequency

◆ bj_get_time_frequency()

uint64_t bj_get_time_frequency ( void )

This is the number of ticks per second returned by bj_get_time_counter. Use this to convert tick counts to seconds.

Returns
Ticks per second (frequency).
See also
bj_get_time_counter

◆ bj_sleep()

void bj_sleep ( int milliseconds)

This function puts the current thread to sleep for at least the specified number of milliseconds. The actual sleep duration may be longer depending on the system.

Parameters
millisecondsNumber of milliseconds to sleep.
See also
bj_get_time, bj_get_time_counter, bj_get_time_frequency
Examples
bitmap_blit.c, drawing_2d.c, shaders.c, sprite_animation.c, stopwatch.c, and time.c.

◆ bj_stopwatch_delay()

double bj_stopwatch_delay ( const bj_stopwatch * p_stopwatch)

If the stopwatch has never been used, it will be reset automatically on first use. This function does not modify the stopwatch state.

Parameters
p_stopwatchPointer to the stopwatch.
Returns
Time in seconds since the last step.
See also
bj_stopwatch_step
Examples
stopwatch.c.

◆ bj_stopwatch_elapsed()

double bj_stopwatch_elapsed ( const bj_stopwatch * p_stopwatch)

If the stopwatch has never been explicitly reset, it will be reset automatically on first use.

Parameters
p_stopwatchPointer to the stopwatch.
Returns
Elapsed time in seconds.
See also
bj_stopwatch_reset
Examples
stopwatch.c.

◆ bj_stopwatch_reset()

void bj_stopwatch_reset ( bj_stopwatch * p_stopwatch)

Records the current time as both the reset point and last step. Clears any prior timing information.

Parameters
p_stopwatchPointer to the stopwatch.
See also
bj_stopwatch_step, bj_stopwatch_elapsed, bj_stopwatch_delay

◆ bj_stopwatch_step()

void bj_stopwatch_step ( bj_stopwatch * p_stopwatch)

Updates the internal step timestamp. This does not affect the reset time, but is used to measure time deltas via bj_stopwatch_delay or bj_stopwatch_step_delay.

Parameters
p_stopwatchPointer to the stopwatch.
See also
bj_stopwatch_step_delay
Examples
stopwatch.c.

◆ bj_stopwatch_step_delay()

double bj_stopwatch_step_delay ( bj_stopwatch * p_stopwatch)

Equivalent to calling bj_stopwatch_delay followed by bj_stopwatch_step, but more efficient and concise.

Parameters
p_stopwatchPointer to the stopwatch.
Returns
Time in seconds since the previous step.
See also
bj_stopwatch_step, bj_stopwatch_delay