- Online Community Forums » Programming Questions » keeping track of time

This thread is locked; no one can reply to it. rss feed Print
keeping track of time
Member #12,426
December 2010

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.

Software Engineer by day, hacker by night.

Matthew Leverton
Supreme Loser
January 1999

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
Member #8,592
May 2007

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.

Member #11,410
October 2009

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*>

Member #7,919
October 2006

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".

I made a VR game!

Member #12,426
December 2010

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

Software Engineer by day, hacker by night.

Member #2,030
March 2002

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. >:(

Move to the Democratic People's Republic of Vivendi Universal (formerly known as Sweden) - officially democracy- and privacy-free since 2008-06-18!

Member #12,426
December 2010

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

Software Engineer by day, hacker by night.

Go to: