Sign in to follow this  
CNBABY

timer ----------timeSetEvent??????

Recommended Posts

i want to creat multiple timers to control different event in my program.could you tell me what is the best way? if i use multimedia timer, how many timers i could creat? is there any limitation? please help me!! regards.

Share this post


Link to post
Share on other sites
Quote:
Original post by CNBABY
i want to creat multiple timers to control different event in my program.could you tell me what is the best way?
if i use multimedia timer, how many timers i could creat? is there any limitation?
please help me!!
regards.


In programming there is no "best way". There are however optimal, preferred, and highly recommended methods to accomplish many tasks. Since you mentioned multimedia timer I assume that your target environment is Windows. Multimedia timers work very well when you need to react in real time (i.e. as a priority above everything else). Depending on the version of windows you may be limited to the number of multimedia timers that you can create.

You also have to consider if you even need timers. If the event MUST be handled immediately then yes you will probably want to use timers. If however you are using timers to trigger some event in the game environment it might be better to create your own timer object. Once that's done you just keep track of all timers that have been created (say in a linked list or array) and check between every frame iteration (i.e. between frames udpates) to see if one or more should fire.

Share this post


Link to post
Share on other sites
Quote:
Original post by Helter Skelter
In programming there is no "best way". There are however optimal, preferred, and highly recommended methods to accomplish many tasks.


What do you think "optimal method" means? [grin]

Share this post


Link to post
Share on other sites
[quote]Original post by Helter Skelter
Depending on the version of windows you may be limited to the number of multimedia timers that you can create.
[quote]


could you explain it in detail?
are there any Specifications to prove it,or any official documents.

thanks

Share this post


Link to post
Share on other sites
Are you sure you definitely need seperate timers? It depends on what you want to do. From your post it sounds to me like you want to have a load of objects that are updated at different time intervals, am I right?

The way I would do it, would only require one timer. Basically you'd have a Scheduler class, which would be responsible for updating schedulable objects. Only the scheduler would need to know the current time. Each frame it updates each schedulable object with the time elapsed since the last update.

Objects that wish to be updated by the scheduler, can be inherited from an ISchedulable interface.


class ISchedulable
{
public:

virtual void UpdateTime( Float timeElapsedInSeconds ) = 0;

virtual Float IntervalInSeconds() = 0;
virtual Float TimeSinceLastScheduledUpdate() = 0;

virtual void OnScheduled() = 0;
};






The UpdateTime method would be called by the scheduler every frame, to update an internal variable which stores the time since the schedulable object was last scheduled.

The Interval method would return a value in seconds, that indicates the time interval on which the schedulable object wants to be updated. For example, if the IntervalInSeconds method returned 2.0, then that would indicate that the schedulable object wants to be updated every 2 seconds.

The TimeSinceLastScheduledUpdate, indicates how long it has been since the schedulable object was last updated.


The scheduler calls the UpdateTime method on each schedulable object each frame. If TimeSinceLastScheduledUpdate is greater than or equal to IntervalInSeconds, then the scheduler calls the OnScheduled method of the schedulable object.


I don't claim that this is the best way to do it, but it's the way I'd do it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
Quote:
Original post by Helter Skelter
In programming there is no "best way". There are however optimal, preferred, and highly recommended methods to accomplish many tasks.


What do you think "optimal method" means? [grin]



LMAO! Ok I'll bite :D

It means "most desirable or satisfactory way" which I consider different from "excelling all others". We could debate it but we both know how pointless that would be :D

Share this post


Link to post
Share on other sites
Quote:
Original post by CNBABY
Quote:
Original post by Helter Skelter
Depending on the version of windows you may be limited to the number of multimedia timers that you can create.



could you explain it in detail?
are there any Specifications to prove it,or any official documents.

thanks



Low priority timers (WM_TIMER):

Win95/98/ME: 32 total timers.
WinNT/2K/XP: 16 timers per process.

High priority timers (Multimedia):

Win95/98/ME: 32 total timers.
WinNT/2K/XP: 16 timers per process.


You can find all the necessary documentation and information that you need concerning timers and the rest of the Windows API at http://msdn.microsoft.com . The specs, usage, limitations, and recommendations are all available there.

Share this post


Link to post
Share on other sites
Helter Skelter,thank you very very very much!:)


and Oxyacetylene,you are so kind.you give me a good advice.thank you.
but if i need accurate intervals,in milliseconds,i don't know the method is suited.what do you think?

Share this post


Link to post
Share on other sites
I reckon a float should be fine for milliseconds. If you find it's not accurate enough, you can use double instead of float. You don't have to use floating point numbers for the method I used, I just used float because I prefer to store time values as floating point numbers. The same principle applies if you're using integers.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this