# Alternative to Sleep() ?

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

## Recommended Posts

Hy , i did a small application where a sprite (player) moves around the screen using keys.

The animation is done by successive blit of images, i use Sleep() function to limit the player's speed (if i dont to that , you press a key and u are at the end of the screen in less than a second). The problem is that Sleep() not only delays the animation but also the msg processing and the window is not processing anything until the animation loop is done executing (getting from point A to point B)

Any hints how can i fix that?

##### Share on other sites
You're using a very basic "frame limiter", which just wastes time (sleeps) so the frame rate can't get too high. These systems are fraught with a lot of problems.

It's much better to actually measure the amount of time that each frame has taken, and either:
*Use this time value to run your 'Update' code a variable number of times per frame.
and/or
*Use this time value as a multiplier on your movement speeds -- e.g. position += speed * elapsedTime

##### Share on other sites
As Hodgman said, use the elapsed time to calculate changes in your game objects. Here is a great article about time steps(and physics) if you are interested...

http://gafferongames.com/game-physics/fix-your-timestep/

Also, adding a small sleep call to your loop isn't necessarily bad. A sleep call small enough that it won't inhibit your run time performance will help reduce cpu usage and allow other tasks to get processor time on single processor systems.

##### Share on other sites
Quote:
 Original post by HodgmanYou're using a very basic "frame limiter", which just wastes time (sleeps) so the frame rate can't get too high. These systems are fraught with a lot of problems.It's much better to actually measure the amount of time that each frame has taken, and either:*Use this time value to run your 'Update' code a variable number of times per frame.and/or*Use this time value as a multiplier on your movement speeds -- e.g. position += speed * elapsedTime

thanks , i'v implemented that , works like clockwork.

##### Share on other sites
Quote:
 Original post by ChadwellAs Hodgman said, use the elapsed time to calculate changes in your game objects. Here is a great article about time steps(and physics) if you are interested...http://gafferongames.com/game-physics/fix-your-timestep/Also, adding a small sleep call to your loop isn't necessarily bad. A sleep call small enough that it won't inhibit your run time performance will help reduce cpu usage and allow other tasks to get processor time on single processor systems.

thanks for the reference

##### Share on other sites
P.S. NextEpisode1:

##### Share on other sites
^ I believe you're missing a return statement :D

##### Share on other sites
Quote:
 Original post by BTownTKDP.S. NextEpisode1:Your signature won't compile...
I don't see any errors in his signature. I haven't actually tried compiling it though so I could have missed something.

##### Share on other sites
Quote:
 Original post by BTownTKDP.S. NextEpisode1:Your signature won't compile...

Yes, it will...

##### Share on other sites
Quote:
 Original post by boogyman19946^ I believe you're missing a return statement :D

A return statement in main is not mandatory. There is an implicit return 0;' at the end.

##### Share on other sites
Quote:
 Original post by boogyman19946^ I believe you're missing a return statement :D
main requires no return statement. The standard dictates that in this case the compiler will have main return 0.

##### Share on other sites
hmm looks like you guys are right. I remember I used a compiler that wouldn't let me leave out the "int" before "main()," and it kept complaining about not having a return statement, but GCC doesn't complain about anything. This:

#include <iostream>main(){	std::cout << "test";}`

compiled fine o.o

##### Share on other sites
I've spoke prematurely!

##### Share on other sites
Quote:
 Original post by BTownTKDI've spoke prematurely!

hahaha so much debate regarding my signature ,i will edit it for you :))

##### Share on other sites
^ heh, looks like an endless loop ^.^

##### Share on other sites
Quote:
 Original post by boogyman19946^ heh, looks like an endless loop ^.^

it is, almost. (It will crash eventually since its overflowing the stack)

##### Share on other sites
Couldn't help it. I had to laugh at that signature the first time I saw it.

As the above posters have said, you are better off using the delta time passed to control how fast you move, and you can also use it to control how fast your sprites animation moves. About Sleep(), I had understood that usually in a game, you don't want it because windows will grab the processor for other tasks regardless. But I've seen it also said like above, where you should use it to "help" windows share.

I've also heard of, but never used, the function yield(), which I understand does the same thing, but I don't know much about it. Does anyone know the specifics on Sleep() and Yield(), and maybe have a good article/link on how necesary they really are, if at all????

##### Share on other sites
Another thing games sometimes do is wait for a vertical retrace of the screen. If your game is cheap to run, this will make it use less than 100% CPU and the results will be as good as they can be.

##### Share on other sites
Quote:
 Original post by kburkhart84Couldn't help it. I had to laugh at that signature the first time I saw it.As the above posters have said, you are better off using the delta time passed to control how fast you move, and you can also use it to control how fast your sprites animation moves. About Sleep(), I had understood that usually in a game, you don't want it because windows will grab the processor for other tasks regardless. But I've seen it also said like above, where you should use it to "help" windows share.I've also heard of, but never used, the function yield(), which I understand does the same thing, but I don't know much about it. Does anyone know the specifics on Sleep() and Yield(), and maybe have a good article/link on how necesary they really are, if at all????

in pthreads yield (or sched_yield) will cause the OS to switch to a thread of higher or equal priority if one is ready(If no such thread is ready it does nothing) while sleep (Which isn't a part of the thread library) causes the calling thread or process to go inactive for atleast x milliseconds. (This can allow a lower priority thread to run aswell and also means that your thread is set as inactive for that time)

##### Share on other sites
Quote:
Original post by SimonForsman
Quote:
 Original post by kburkhart84Couldn't help it. I had to laugh at that signature the first time I saw it.As the above posters have said, you are better off using the delta time passed to control how fast you move, and you can also use it to control how fast your sprites animation moves. About Sleep(), I had understood that usually in a game, you don't want it because windows will grab the processor for other tasks regardless. But I've seen it also said like above, where you should use it to "help" windows share.I've also heard of, but never used, the function yield(), which I understand does the same thing, but I don't know much about it. Does anyone know the specifics on Sleep() and Yield(), and maybe have a good article/link on how necesary they really are, if at all????

in pthreads yield (or sched_yield) will cause the OS to switch to a thread of higher or equal priority if one is ready(If no such thread is ready it does nothing) while sleep (Which isn't a part of the thread library) causes the calling thread or process to go inactive for atleast x milliseconds. (This can allow a lower priority thread to run aswell and also means that your thread is set as inactive for that time)

Thanks for the clarification on that. Is there an article or maybe a topic of debate or anything at all, which has information as far as the actual use of the two functions. I'm curious if they are actually any use or not, and if they are, why??? I understand windows does the task switching on the processor on its own, so is there really a reason for your game to do it?