Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Limit of usage of the timers

This thread is locked; no one can reply to it. rss feed Print
 1   2 
Limit of usage of the timers
CascoOscuro
Member #4,966
August 2004
avatar

By the post you can think that i'm asking about how many timers a can use in a certain moment, but no, don't care.

My question is that i need a timer routine that must be executed each milisecond. The routine is very simple, it only increases a var (very tipical). It's done for animation speed stuff.

Well, i've implemented it and i don't noticed any slow of perfomance, but, there are risks to happen what i say?
Thinking in the code, the game logic routines could be done in one microsecond (more or less) in the worst of cases, i think. Drawing routine should be done in various microseconds, so, at first, looks like there isn't problems.

Am I wrong?

Thanks in advance.

miran
Member #2,407
June 2002

Your timer routine will most likely not be executed each milisecond. Instead it will be executed ten times in a row once every 10 miliseconds...

--
sig used to be here

tobing
Member #5,213
November 2004
avatar

Depends on the platform I would say. On Windows, you can improve accuracy of the timers by calling timeBeginPeriod(1); when the program is starting and timeEndPeriod(1); when the program is ending.

FMC
Member #4,431
March 2004
avatar

tobing said:

Depends on the platform I would say. On Windows, you can improve accuracy of the timers by calling timeBeginPeriod(1); when the program is starting and timeEndPeriod(1); when the program is ending.

It isn't the first time i hear this and it seems to work; if it's so simple why isn't it added to allegro?

[FMC Studios] - [Caries Field] - [Ctris] - [Pman] - [Chess for allegroites]
Written laws are like spiders' webs, and will, like them, only entangle and hold the poor and weak, while the rich and powerful will easily break through them. -Anacharsis
Twenty years from now you will be more disappointed by the things that you didn't do than by the ones you did do. So throw off the bowlines. Sail away from the safe harbor. Catch the trade winds in your sails. Explore. Dream. Discover. -Mark Twain

CascoOscuro
Member #4,966
August 2004
avatar

The platform is for now windows, but i want to port it to Linux, (if fblend lets me do it).
For this purpose i could do the timer routine after every end of main loop execution, because frame rate is limited.
thank you for all.

tobing
Member #5,213
November 2004
avatar

Mithrandir said:

It isn't the first time i hear this and it seems to work; if it's so simple why isn't it added to allegro?

I have submitted that as a change request for the next version. Until now I don't know if they will add it or not...

gnolam
Member #2,030
March 2002
avatar

Quote:

My question is that i need a timer routine that must be executed each milisecond. The routine is very simple, it only increases a var (very tipical). It's done for animation speed stuff.

Actually, I fail to see how you would need a millisecond-accurate timer for animation...

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

tobing
Member #5,213
November 2004
avatar

You might not use such a fast counter for doing animations. I'm using counters of that type for internal performance measurements of certain code sections (and for that I'm acutally using a timer which counts 10000 per sec).

Chris Katko
Member #1,881
January 2002
avatar

Quote:

It isn't the first time i hear this and it seems to work; if it's so simple why isn't it added to allegro?

Because as far as I've heard, it isn't reliable.

-----sig:
“Programs should be written for people to read, and only incidentally for machines to execute.” - Structure and Interpretation of Computer Programs
"Political Correctness is fascism disguised as manners" --George Carlin

CascoOscuro
Member #4,966
August 2004
avatar

Quote:

hy must a trip to Europe be so expensive?

to slow down animations speed applying modular mathematics.

Evert
Member #794
November 2000
avatar

Quote:

Because as far as I've heard, it isn't reliable.

Can you elaborate?
I was planning on adding it, but I'd like to know what I'm adding if there are possible problems with it...

tobing
Member #5,213
November 2004
avatar

Quote:

Because as far as I've heard, it isn't reliable.

That's something I would also like to know. My experiences don't second that...

Bob
Free Market Evangelist
September 2000
avatar

You probably want to call timeGetDevCaps() first, to get the range of periods that are supported on the OS.

--
- Bob
[ -- All my signature links are 404 -- ]

tobing
Member #5,213
November 2004
avatar

First I thought that would be a good idea. Then, why not simply calling timeBeginPeriod(1); ? If the capabilities are not sufficient (which is not very probable on modern PCs) then the result will be what is closest to timer precision of 1ms. So there's no need to check capabilities. Of course this is different if the application actually depends on precision, but then it's the applications' responsibility to check for that.

Ron Ofir
Member #2,357
May 2002
avatar

CascoOscuro's quote said:

hy must a trip to Europe be so expensive?

What does that have to do with anything??? :P And what program are you making that involves modular mathematics?

CascoOscuro
Member #4,966
August 2004
avatar

Oh, sorry! I forgot the "copy" before "paste":-/. That's a quote from another post.

The quote was about gnolam said above.

Well... imagine that you want a run animation for a enemy, and game has bullet time (for example), with a limited frames.

So the animation routine is such as:

void CCharacter::anim()
{
   if(!(Timer % SlowDowner))
     Frame++;
}

Of course it's more complex than this.

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

First I thought that would be a good idea. Then, why not simply calling timeBeginPeriod(1); ?

It probably effects other apps too. I would imagine it'd make them less responsive if your program is "checked on" more often.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

tobing
Member #5,213
November 2004
avatar

Quote:

I would imagine it'd make them less responsive if your program is "checked on" more often.

I think I don't understand. What do you mean with that?

BTW my proposed change would only affect the rest()-function to be more precise. In other cases the application could call timeBeginTime(1) as needed or applicable. One could think about calling timeBeginTime(1) only if rest() is called with value 1 - or something like rest(n) internally calls timeBeginPeriod(n) and only if n is nonzero. I would think that Evert is already trying this out and playing around with such things...

Evert
Member #794
November 2000
avatar

Quote:

I would think that Evert is already trying this out and playing around with such things...

Evert doesn't use Windows though, so he can't really do much experimentation...

tobing
Member #5,213
November 2004
avatar

Ok. So then I'll do some more investigations, maybe I'll send you some modification then.

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

I think I don't understand. What do you mean with that?

When you call rest/Sleep normally, Windows puts the process to sleep and waits until the next time the scheduler would get back to your program before waking back up. However, by setting timeBeginPeriod(1), it checks your program more often, which means other programs would get interrupted more often. If the scheduler granularity could be more than 10ms, I think it would be.. but it's not for a reason.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

tobing
Member #5,213
November 2004
avatar

I see. Well, my experiments so far haven't shown any difference wrt to this behaviour, calling timeBeginPeriod(1) or not. Only, if I call it, then my own program needs less CPU in total, so there's actually more CPU available for other applications.

Kitty Cat
Member #2,815
October 2002
avatar

Quote:

Only, if I call it, then my own program needs less CPU in total

That contradicts what it does, though. To check on your app more often requires more CPU. Especially as it relates to rest, where I made the analogy about a person waking up every hour for 8 hours while in bed, instead of doing the 8 hours all in one shot.

--
"Do not meddle in the affairs of cats, for they are subtle and will pee on your computer." -- Bruce Graham

tobing
Member #5,213
November 2004
avatar

I don't know - the rest() call is not using the CPU. It is the remainder of my game loop, but I don't know why this is so. The observation is that adding a call to timeBeginTime(1) within the implementation of rest(1) makes the complete loop smoother, and overall less CPU is used. I think it might be because of smoother checking timers, so when timers are not accurate you might spend more time in some loop than required. So this might be some side-effect of calling those functions...

Tobias Dammers
Member #2,604
August 2002
avatar

Quote:

Well... imagine that you want a run animation for a enemy, and game has bullet time (for example), with a limited frames.

Still, you won't need timers faster than a few times the frame rate. 1ms is just insane. If you want to alter the game speed, don't alter the timer rate, but rather the timer delta. Example:
Say, in "normal" time, you have a timer running at 100Hz (10 ms). This is still roughly twice the frequency needed for smooth animation. With each tick, the timer is increased by 10. For each screen update, you update your logic according to the timer delta (either use variable-delta logic, or keep calling the logic update function until the timer delta is back to 0). To produce bullet time, say, with a factor of 10, you increase the timer by 1 instead of 10. The rest of the program remains unchanged. Your logic will now update at 1/10 the speed, without the need for insanely high timer rates.
The thing is, even if all you do in the timer function is increase a variable by 1, calling it 1000 times per second does induce unneeded overhead.

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

 1   2 


Go to: