keeping track of time

Does anyone know how to have it so you can keep track the time from a certain point? Like have it start at 0:00 then 0:01, 0:02, etc. I thought there would be in timer but i didnt see anything in it.

Matthew Leverton

Set up a timer to tick once per second.

Either use its value directly or use it to monitor the current timestamp minus the timestamp you began. In Allegro 5:

double ts = al_get_time();

elapsed_time = al_get_time() - ts;

Edgar Reynaldo

It depends on whether you're using Allegro 4 or 5. If 4, then start a timer with install_int_ex(TickerFunction , SECS_TO_TIMER(1)) and count the number of ticks. If 5, then use al_create_timer, al_start_timer, and al_get_timer_count. With A5, you can also register an event source with the timer and check the events to see when the timer increments.


Are you using AL4 or AL5?

In AL 5 you have to register your timer with the event queue, then you have to check for it in the ALLEGRO_EVENT_TIMER

if( event.timer.source == myTimer) {}

to do dynamic timers, I suggest you use a std::vector<ALLEGRO_TIMER*>


Matthew's method would probably prove to be the most accurate, since you don't have to worry whether your logic stutters if the computer lags. al_get_time() will return the current time now, regardless of the speed of the processor.

That being said, you may also want to create a custom timer that includes the ability to pause. You don't have to use the pause feature, but you don't have to reinvent any wheels as soon as you decide that you would like one. Just tell whichever time you're using to pause itself while the game is showing the menu, and then unpause itself when the game "resumes".


oh yeah I forgot to mention I'm using allegro 4


It all depends on whether you actually want to use that time for any game logic or not. In the latter case, any method works (compare current time to starting time, one-second timer, etc). If the former (for example, a completion countdown), you want keep track of the number of elapsed logic ticks instead.


OnlineCop said:

if the computer lags

Stop misusing that word, people. >:(


its just there to keep track of long it took the player to do the mini game. then im going to save the time in a variable then send it to the fstream, so i can load it and keep track of the shortest time it took

Thread #606052. Printed from