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)
Help me debug my delay function? :D
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
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
Thank you for the quick reply. How do I catch the current FPS though? I dont think currentFPS is a reserved keyword or something ;(
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]
[EDIT] Beaten to it by DarkWing who also had a much simpler way of doing the calculation [/EDIT]
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.
(i think thats right)
while(whatever){ time = GetTime(); Delay(constant_delay_time - GetTime() + time);}
(i think thats right)
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.
For anyone else who is interested, the problem with my function was that I didn't have Winmm.lib in my linker.
Quote:Original post by MrLeap
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.
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
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.]<br><br>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<br><br>Thank you all once again!<br><br>edit: where can I find all the tags? this guessing stuff is getting kind of tough :P
Quote:Original post by MrLeap
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.]<br><br>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<br><br>Thank you all once again!<br><br>edit: where can I find all the tags? this guessing stuff is getting kind of tough :P<!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>In Win32, you can use this function:<br><pre><br>void Sleep(<br> DWORD dwMilliseconds<br>);<br></pre><br>If you're using SDL (which I recommend, to support multiple platforms) you use <br>SDL_Delay(). You don't need to worry about multi-threading to use the sleep functions.<br><br>BTW some of the tags can be found in the 'faq' link in the upper right corner of the page.<br><br>Richard
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement