Alternative to Sleep() ?

Started by
18 comments, last by kburkhart84 13 years, 9 months ago
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?
#include <iostream>#include <string>int main(){ std::cout<<"I will kill your stack"; return main();}
Advertisement
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
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.
Quote:Original post by Hodgman
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


thanks , i'v implemented that , works like clockwork.
#include <iostream>#include <string>int main(){ std::cout<<"I will kill your stack"; return main();}
Quote:Original post by Chadwell
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.


thanks for the reference
#include <iostream>#include <string>int main(){ std::cout<<"I will kill your stack"; return main();}
P.S. NextEpisode1:

Your signature won't compile...
Deep Blue Wave - Brian's Dev Blog.
^ I believe you're missing a return statement :D

Yo dawg, don't even trip.

Quote:Original post by BTownTKD
P.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.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by BTownTKD
P.S. NextEpisode1:

Your signature won't compile...


Yes, it will...
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.

This topic is closed to new replies.

Advertisement