How to use multiple timers in allegro?

1.8k Views Asked by At
if( ev.type == ALLEGRO_EVENT_TIMER)

This is the statement in the event loop to check if the incoming event is a timer event.

But all timers would generate this same event, so how do you have multiple timers going at once? How do you differentiate them?

3

There are 3 best solutions below

0
On BEST ANSWER

The event is a union. See all the properties here.

You want ev.timer.source (or ev.any.source).

0
On

I was having an issue with this for the longest time and I found out I just forgot to include al_start_timer(alTimer); in my update. Stupid mistake but it could cost you some time if you forget it.

0
On

Here's a working example, Assuming you have two ALLEGRO_TIMERs(timer_one, timer_two) :

To respond to each timer:

if(ev.timer.source == timer_one) { //Timer one listener
//Code...
}


if(ev.timer.source == timer_two) { //Timer two listener
//Code...
}