Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » timers

This thread is locked; no one can reply to it. rss feed Print
timers
Money
Member #6,730
December 2005
avatar

can somone point me to a tutorial on timers..and how to use them?

thanks

CGamesPlay
Member #2,559
July 2002
avatar

Have a look at Loomsoft's tutorial. There are others at the site root.

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

Money
Member #6,730
December 2005
avatar

thanks :)

Tobias Dammers
Member #2,604
August 2002
avatar

And just so you know: For smooth animation, you're probably better off not using allegro timers; the best method is QueryPerformanceCounter for windows and gettimeofday for linux. Google should give you enough information about how to use them. Both work similarly and can be wrapped into a uniform timer api which is as simple as it can be. Strong points of both:
+ extremely high accuracy (down to a single cpu clock tick, IIRC, while allegro timers have a ~10 ms granularity on windows)
+ virtually no performance penalty (since the timers you poll are running anyway, while allegro timers need a dedicated timer thread which they switch into and out of)
Only con is that they are platform specific; this can be solved by adding allegro timer support to the wrapper as well (as a fallback), or just limiting the program to windows & linux (there is probably a similar solution for other platforms though).

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Evert
Member #794
November 2000
avatar

Quote:

gettimeofday for linux

Actually,

man gettimeofday said:

CONFORMING TO
SVr4, BSD 4.3. POSIX 1003.1-2001 describes gettimeofday() but not set-
timeofday().

That basically means that it's portable to any UNIX system, including MacOS X (so basically, everything except Windows). Hmm... it might even be portable to MinGW, which makes it sortof portable to Windows too.

HoHo
Member #4,534
April 2004
avatar

Quote:

Hmm... it might even be portable to MinGW, which makes it sortof portable to Windows too.

A quick google search makes me think migw doesn't support it

[edit]

Perhaps I was too hasty. Here is a way to give mingw gettimeofday function. Of cource it involves creating custom mingw build so if it is not in official mingw distribution by now* it is of little use

*)The original "fix" was pruposed around mid-2001 and in late 2004 it wasn't in official release. I doubt it is there today,

__________
In theory, there is no difference between theory and practice. But, in practice, there is - Jan L.A. van de Snepscheut
MMORPG's...Many Men Online Role Playing Girls - Radagar
"Is Java REALLY slower? Does STL really bloat your exes? Find out with your friendly host, HoHo, and his benchmarking machine!" - Jakub Wasilewski

Jakub Wasilewski
Member #3,653
June 2003
avatar

Quote:

while allegro timers have a ~10 ms granularity on windows

This can be easily remedied by using timeBeginPeriod and timeEndPeriod from the WinMM, probably coupled with some querying with timeGetDevCaps to see what is the minimal granularity possible (it'll likely be 10ms under Win98, but newer systems tend to support at least 1ms).

Setting 1ms scheduler granularity will probably allow Allegro timers (they're implemented as threads, right?) to run more accurately, up to 1000 BPS.

I use the more complicated delta time method myself, and it's better in many ways than what I described above, but the timer approach has the advantage of being very simple, and with beefed up granularity it's even quite smooth.

---------------------------
[ ChristmasHack! | My games ] :::: One CSS to style them all, One Javascript to script them, / One HTML to bring them all and in the browser bind them / In the Land of Fantasy where Standards mean something.

Evert
Member #794
November 2000
avatar

Quote:

Setting 1ms scheduler granularity will probably allow Allegro timers (they're implemented as threads, right?) to run more accurately, up to 1000 BPS.

It may and it may not.
This was tested about a year ago (I think) and it was found that the result was unreliable across different Windows versions (no surprise there) and different hardware running the same version of Windows, so no change was made to Allegro to implement this.

Murat AYIK
Member #6,514
October 2005
avatar

Can someone clarify this? I mean what would the error graph would look like on for example my 200 bps timer? Does it ever do "5-10-9-14-19" or "5-10-10-20" ? If we know what goes wrong then it would be easier to counter it, right? Just "knowing that it is bad" doesn't help much!

_____________________________________________________
"The world doesn't care about what storms you sailed through, it is interested in whether you brought the ship to the dock or not!"

Sirocco
Member #88
April 2000
avatar

Quote:

And just so you know: For smooth animation, you're probably better off not using allegro timers; the best method is QueryPerformanceCounter for windows and gettimeofday for linux.

While I agree with that for complex 3D scenes, the vast majority of 2D games (and simple 3D ones, I imagine) can be handled in a smooth and reliable fashion with a few well crafted timers. I've never had a reason to use a timer of greater frequency than 140 BPS, and even that could have been pared down to 70 if I had felt a little more inspired.

Certainly for 3D games I'd go for delta time, but all my games run nicely using Allegro timers.

-->
Graphic file formats used to fascinate me, but now I find them rather satanic.

Frank Drebin
Member #2,987
December 2002
avatar

Quote:

For smooth animation, you're probably better off not using allegro timers

i don't believe so.
for animation they work pretty good!

scriptX
Member #6,574
November 2005
avatar

Quote:

the best method is QueryPerformanceCounter for windows

Just curious, how would you use that?

ImLeftFooted
Member #3,935
October 2003
avatar

Heres how i do it:

1#ifdef WIN32
2#include <windows.h>
3#else
4#include <sys/time.h>
5#endif
6 
7Timer timer;
8 
9Timer::Timer()
10{
11#ifdef WIN32
12 startTime = GetTickCount();
13#else
14 gettimeofday(&startTime, 0);
15#endif
16}
17 
18Timer::time_t Timer::usecs()
19{
20#ifdef WIN32
21 static bool onetime = 0;
22 static unsigned long long freq = 0;
23
24 if(!onetime) {
25
26 onetime = true;
27 QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
28 }
29
30 QueryPerformanceCounter((LARGE_INTEGER*)&currentTime);
31
32 currentTime *= 1000000;
33 currentTime /= freq;
34
35 return currentTime - startTime * 1000;
36#else
37 gettimeofday(&currentTime, 0);
38
39 return (unsigned long)((currentTime.tv_sec - startTime.tv_sec) / 1000)
40 + (unsigned long)((currentTime.tv_usec - startTime.tv_usec));
41#endif
42}
43 
44Timer::time_t Timer::msecs()
45{
46#ifdef WIN32
47 currentTime = GetTickCount();
48
49 return currentTime - startTime;
50#else
51 gettimeofday(&currentTime, 0);
52
53 return (unsigned long)((currentTime.tv_sec - startTime.tv_sec) * 1000)
54 + (unsigned long)((currentTime.tv_usec - startTime.tv_usec) / 1000);
55#endif
56}
57 
58Timer::time_t Timer::secs()
59{
60#ifdef WIN32
61 currentTime = GetTickCount();
62
63 return (currentTime - startTime) / 1000;
64#else
65 gettimeofday(&currentTime, 0);
66
67 return (unsigned long)(currentTime.tv_sec - startTime.tv_sec);
68#endif
69}

GetTickCount is easier if all you need is millisecond precision.

Where you need more accurate timers (in a typical game project) is with FPS counters.

Go to: