Timer routines

These functions are declared in the main Allegro header file:

 #include <allegro5/allegro.h>

ALLEGRO_TIMER

typedef struct ALLEGRO_TIMER ALLEGRO_TIMER;

Source Code

This is an abstract data type representing a timer object.

Examples:

ALLEGRO_USECS_TO_SECS

#define ALLEGRO_USECS_TO_SECS(x)      ((x) / 1000000.0)

Source Code

Convert microseconds to seconds.

ALLEGRO_MSECS_TO_SECS

#define ALLEGRO_MSECS_TO_SECS(x)      ((x) / 1000.0)

Source Code

Convert milliseconds to seconds.

ALLEGRO_BPS_TO_SECS

#define ALLEGRO_BPS_TO_SECS(x)        (1.0 / (x))

Source Code

Convert beats per second to seconds.

Examples:

ALLEGRO_BPM_TO_SECS

#define ALLEGRO_BPM_TO_SECS(x)        (60.0 / (x))

Source Code

Convert beats per minute to seconds.

al_create_timer

ALLEGRO_TIMER *al_create_timer(double speed_secs)

Source Code

Allocates and initializes a timer. If successful, a pointer to a new timer object is returned, otherwise NULL is returned. speed_secs is in seconds per “tick”, and must be positive. The new timer is initially stopped and needs to be started with al_start_timer before ALLEGRO_EVENT_TIMER events are sent.

Usage note: typical granularity is on the order of microseconds, but with some drivers might only be milliseconds.

See also: al_start_timer, al_destroy_timer

Examples:

al_start_timer

void al_start_timer(ALLEGRO_TIMER *timer)

Source Code

Start the timer specified. From then, the timer’s counter will increment at a constant rate, and it will begin generating events. Starting a timer that is already started does nothing. Starting a timer that was stopped will reset the timer’s counter, effectively restarting the timer from the beginning.

See also: al_stop_timer, al_get_timer_started, al_resume_timer

Examples:

al_resume_timer

void al_resume_timer(ALLEGRO_TIMER *timer)

Source Code

Resume the timer specified. From then, the timer’s counter will increment at a constant rate, and it will begin generating events. Resuming a timer that is already started does nothing. Resuming a stopped timer will not reset the timer’s counter (unlike al_start_timer).

See also: al_start_timer, al_stop_timer, al_get_timer_started

Examples:

al_stop_timer

void al_stop_timer(ALLEGRO_TIMER *timer)

Source Code

Stop the timer specified. The timer’s counter will stop incrementing and it will stop generating events. Stopping a timer that is already stopped does nothing.

See also: al_start_timer, al_get_timer_started, al_resume_timer

Examples:

al_get_timer_started

bool al_get_timer_started(const ALLEGRO_TIMER *timer)

Source Code

Return true if the timer specified is currently started.

al_destroy_timer

void al_destroy_timer(ALLEGRO_TIMER *timer)

Source Code

Uninstall the timer specified. If the timer is started, it will automatically be stopped before uninstallation. It will also automatically unregister the timer with any event queues.

Does nothing if passed the NULL pointer.

See also: al_create_timer

Examples:

al_get_timer_count

int64_t al_get_timer_count(const ALLEGRO_TIMER *timer)

Source Code

Return the timer’s counter value. The timer can be started or stopped.

See also: al_set_timer_count

Examples:

al_set_timer_count

void al_set_timer_count(ALLEGRO_TIMER *timer, int64_t new_count)

Source Code

Set the timer’s counter value. The timer can be started or stopped. The count value may be positive or negative, but will always be incremented by +1 at each tick.

See also: al_get_timer_count, al_add_timer_count

Examples:

al_add_timer_count

void al_add_timer_count(ALLEGRO_TIMER *timer, int64_t diff)

Source Code

Add diff to the timer’s counter value. This is similar to writing:

al_set_timer_count(timer, al_get_timer_count(timer) + diff);

except that the addition is performed atomically, so no ticks will be lost.

See also: al_set_timer_count

al_get_timer_speed

double al_get_timer_speed(const ALLEGRO_TIMER *timer)

Source Code

Return the timer’s speed, in seconds. (The same value passed to al_create_timer or al_set_timer_speed.)

See also: al_set_timer_speed

al_set_timer_speed

void al_set_timer_speed(ALLEGRO_TIMER *timer, double new_speed_secs)

Source Code

Set the timer’s speed, i.e. the rate at which its counter will be incremented when it is started. This can be done when the timer is started or stopped. If the timer is currently running, it is made to look as though the speed change occurred precisely at the last tick.

speed_secs has exactly the same meaning as with al_create_timer.

See also: al_get_timer_speed

Examples:

al_get_timer_event_source

ALLEGRO_EVENT_SOURCE *al_get_timer_event_source(ALLEGRO_TIMER *timer)

Source Code

Retrieve the associated event source. Timers will generate events of type ALLEGRO_EVENT_TIMER.

Examples:

Allegro version 5.2.10 (20231119) - Last updated: 2024-11-26 05:31:14 UTC