• Advertisement
Sign in to follow this  

putting a delay into a program

This topic is 3845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, im working on animation for my program but the program cycles through the frames too fast, whats a simple delay/timer to use to slow it down? thanks

Share this post


Link to post
Share on other sites
Advertisement
With what programming language? Are you using any libraries? What OS are you targeting?

Share this post


Link to post
Share on other sites
Oh wow, sorry i totally forgot to put in all that information my bad <.<

ok starting over, i'm using c++ and DirectX for my programming needs and I just need a simple timer to slow down my animations.

Share this post


Link to post
Share on other sites
You've got a number of options. One method is to use SetTimer() to create WM_TIMER messages that will come every so often. If you want a straight up delay in your code you can use Sleep(). You can also use GetTickCount() or QueryPerformanceCounter() to measure how much time has elapsed per frame/update period and incrementally update your program state for the time difference.

Share this post


Link to post
Share on other sites
What you really want to do is have the animation be controlled by the time elapsed since the last frame. So for each animation call you need to be testing it against the time elapsed and then have the animation move a distance related to that time.

So if you are running at 15 fps then the animation would move the same distance as if you are running at 200 fps because one second is still one second.

theTroll

Share this post


Link to post
Share on other sites
You have two options, each with their respective pros and cons.

1: Make a timer that prevents the program from performing another loop until a certain time has passed.

Pros:
Very easy to implement and maintain (~4 function calls total in WinAPI without error checking, no global dependencies).
Minimal, if any, impact on performance.
Only uses as much CPU time as is needed to run at optimal performance, freeing up spare CPU time to be used by other apps and makes switching between them faster.

Cons:
Doesn't scale well if you want to slow time down (bullet time).
In P2P multiplayer apps, it can be difficult to synch in cases of inferior system performance.
If framerate drops below what you set it to due to system load, the game simulation will also run at a slower pace.

Example of implementation:
	LARGE_INTEGER liDueTime;
liDueTime.QuadPart=-166666; //60Hz
hTimer = CreateWaitableTimer(NULL, TRUE, "WaitableTimer");
if (NULL == hTimer)
{
//error handling
}
if (!SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0))
{
//error handling
}

Init();
while(!done){
SetWaitableTimer(hTimer, &liDueTime, 0, NULL, NULL, 0);
Tick();
Render();
WaitForSingleObject(hTimer, INFINITE) != WAIT_OBJECT_0);

}





2: Make a timer that detects how much time has passed since the last loop. Multiply every temporal action by a factor calculated from the time passed.

Pros:
Scalability and resolution. You can speed things up or slow them down as much as you want without change in perceived tick length.
The program is guaranteed to always run at the correct speed, even if framerates drop drastically.
Because of the last point, can make synching in multiplayer easier.

Cons:
Uses 100% CPU (or close to it).
In extreme cases (very many objects and/or actions), it can have a negative effect on performance due to many extra multiplications being carried out.
Needs a global dependency of some sort to keep track of time, decreasing maintainability and simplicity.

In most cases, either technique will do just fine.

Share this post


Link to post
Share on other sites
Quote:
Original post by TheTroll
What you really want to do is have the animation be controlled by the time elapsed since the last frame. So for each animation call you need to be testing it against the time elapsed and then have the animation move a distance related to that time.

So if you are running at 15 fps then the animation would move the same distance as if you are running at 200 fps because one second is still one second.

theTroll


It depends on what kind of "animations" you have. For anything you're likely to be doing with DirectX, this is probably the best approach, but with sprite animation it's totally useless (because you can't interpolate the sprites); you need to set a fixed framerate.

Share this post


Link to post
Share on other sites
Quote:
Original post by Zahlman
It depends on what kind of "animations" you have. For anything you're likely to be doing with DirectX, this is probably the best approach, but with sprite animation it's totally useless (because you can't interpolate the sprites); you need to set a fixed framerate.


The OP did say he was using DirectX...

theTroll

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement