# Help me debug my delay function? :D

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

## Recommended Posts

void Delay(double delay)
{
double a = timeGetTime();

while (timeGetTime() < (a + delay*1000))
{
}
}

I'm hoping to use this function (once it works :P) to add some speed capping to my program. As of now, it executes as fast as my computer can do it, but, as it's purpose is to be a game, that is too unpredictable. So, if anyone knows a better way to achieve the results i've described, Wahoo ;) Thanks before hand! (I know that this uses the windows library, and isn't technically an openGL question, but a general programming question, but I feel that as it's adaption is for the purpose of openGL, if a better method exists I'd be informed by you all :D)

##### Share on other sites
It's probably a much better idea to have all your character/game movements time based instead of framebased (I'm assuming this is why you want to slow it down)... that way you won't have to slow down your game (what if someone else runs your game slower?). So for example, instead of moving the player 2 units every frame, move it based on how fast the game is running:

movement += 2; // old frame-base way... don't do this

movment += (2*UsualFPS)/CurrentFPS; // run this every frame... time based use this

where UsualFPS is the speed you want the unit to move at (so if you like how everything works at 60FPS, then replace that with 60), 2 is the unit speed, and CurrentFPS is the actual frames per second the game is running at. You can see therefore that the higher CurrentFPS is, the slower the unit will move at per frame (meaning that it will move 2 units at 60 FPS in the same time it would take to move 2 units at 500FPS).

I hope I made some sense here... I'm kind-of out of it right now.

-Walter

##### Share on other sites
Thank you for the quick reply. How do I catch the current FPS though? I dont think currentFPS is a reserved keyword or something ;(

##### Share on other sites
You have to calculate it. curentFPS = 1 / time_of_last_frame

##### Share on other sites
Yeah, you'll have to calculate the FPS yourself.

[EDIT] Beaten to it by DarkWing who also had a much simpler way of doing the calculation [/EDIT]

##### Share on other sites
personally I think loop delays are sometimes needed, such as in an rpg when you're updating a characters walk animation - you can sacrifice that slowdown for smoother animation.
while(whatever){        time = GetTime();        Delay(constant_delay_time - GetTime() + time);}

(i think thats right)

##### Share on other sites
my function served my purposes.. erm.. perfectly :) I'm going to look into the FPS thing though, that might help me out later.

For anyone else who is interested, the problem with my function was that I didn't have Winmm.lib in my linker.

##### Share on other sites
Quote:
 Original post by MrLeapmy function served my purposes.. erm.. perfectly :) I'm going to look into the FPS thing though, that might help me out later.For anyone else who is interested, the problem with my function was that I didn't have Winmm.lib in my linker.

Your function is still going to peg the CPU with that loop.. The more eco-friendly way to do it is to calculate how many milliseconds to wait, then do a sleep so that other processes (or idle) can run. For example:

float fMinTime = 1000.0f / fMaxFPS; // the FPS capwhile (bRunning)  {  float fStartTime = CurrentMilliseconds(); // exact function depends on OS  // ... do your calculations and drawing here  float fEndTIme = CurrentMilliseconds();  if (fEndTime - fStartTime < fMinTime)    Sleep(fMinTime - (fEndTime - fStartTime)); // also OS-dependent  }

Also, to avoid a division (make your code faster) I would make your frame updates dependent on actual TIME, not frequency (FPS) as the previous poster gave you an example. Just calculate the amount of time taken for each frame and feed it into your update function as a value in milliseconds. Then in your update function you can do something like this:

fCharacterY += 0.02f * fFrameMilliseconds;

That way he will move 0.02 units per millisecond, and you avoid having to do that division used in the FPS method above.

Richard

##### Share on other sites
[I wish this all to be scored! CROSSED OUT EVEN!]heh, anyone know a function similer to sleep for windows? I dont know how I'd write that if it didn't exist :p [/End score.]

Never mind! msdn told me that sleep(); does exist within the Kernel32.lib! something about threads though. I'm dont know much about that multi threading jazzery. Time to read I guess :D

Thank you all once again!

edit: where can I find all the tags? this guessing stuff is getting kind of tough :P

##### Share on other sites
Quote:
 Original post by MrLeap[I wish this all to be scored! CROSSED OUT EVEN!]heh, anyone know a function similer to sleep for windows? I dont know how I'd write that if it didn't exist :p [/End score.]Never mind! msdn told me that sleep(); does exist within the Kernel32.lib! something about threads though. I'm dont know much about that multi threading jazzery. Time to read I guess :DThank you all once again!edit: where can I find all the tags? this guessing stuff is getting kind of tough :P

In Win32, you can use this function:
void Sleep(  DWORD dwMilliseconds);

If you're using SDL (which I recommend, to support multiple platforms) you use
SDL_Delay(). You don't need to worry about multi-threading to use the sleep functions.

BTW some of the tags can be found in the 'faq' link in the upper right corner of the page.

Richard

1. 1
Rutin
37
2. 2
3. 3
4. 4
5. 5

• 11
• 10
• 13
• 103
• 11
• ### Forum Statistics

• Total Topics
632976
• Total Posts
3009672
• ### Who's Online (See full list)

There are no registered users currently online

×